mirror of
https://github.com/OMGeeky/tarpc.git
synced 2025-12-30 16:18:56 +01:00
Add a method to util::FirstSocketAddr that returns a Result rather than panicking
This commit is contained in:
@@ -531,15 +531,7 @@ macro_rules! service {
|
||||
-> ::std::io::Result<$crate::tokio_proto::server::ServerHandle>
|
||||
where L: ::std::net::ToSocketAddrs
|
||||
{
|
||||
let addr = if let ::std::option::Option::Some(a) =
|
||||
::std::iter::Iterator::next(
|
||||
&mut ::std::net::ToSocketAddrs::to_socket_addrs(&addr)?) {
|
||||
a
|
||||
} else {
|
||||
return Err(::std::io::Error::new(::std::io::ErrorKind::AddrNotAvailable,
|
||||
"`ToSocketAddrs::to_socket_addrs` returned an empty iterator."));
|
||||
};
|
||||
|
||||
let addr = $crate::util::FirstSocketAddr::try_first_socket_addr(&addr)?;
|
||||
let __tarpc_service_service = __SyncServer {
|
||||
service: self,
|
||||
};
|
||||
@@ -603,17 +595,7 @@ macro_rules! service {
|
||||
fn connect<A>(addr: A) -> ::std::result::Result<Self, ::std::io::Error>
|
||||
where A: ::std::net::ToSocketAddrs,
|
||||
{
|
||||
let addr = if let ::std::option::Option::Some(a) =
|
||||
::std::iter::Iterator::next(
|
||||
&mut ::std::net::ToSocketAddrs::to_socket_addrs(&addr)?)
|
||||
{
|
||||
a
|
||||
} else {
|
||||
return ::std::result::Result::Err(
|
||||
::std::io::Error::new(
|
||||
::std::io::ErrorKind::AddrNotAvailable,
|
||||
"`ToSocketAddrs::to_socket_addrs` returned an empty iterator."));
|
||||
};
|
||||
let addr = $crate::util::FirstSocketAddr::try_first_socket_addr(&addr)?;
|
||||
let client = <FutureClient as $crate::future::Connect>::connect(&addr);
|
||||
let client = SyncClient($crate::futures::Future::wait(client)?);
|
||||
::std::result::Result::Ok(client)
|
||||
|
||||
19
src/util.rs
19
src/util.rs
@@ -7,10 +7,9 @@ use futures::{self, Future, Poll};
|
||||
use futures::stream::Stream;
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::{fmt, io, thread};
|
||||
use std::net::{SocketAddr, ToSocketAddrs};
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
use tokio_core::reactor;
|
||||
|
||||
/// A bottom type that impls `Error`, `Serialize`, and `Deserialize`. It is impossible to
|
||||
@@ -103,12 +102,22 @@ impl<S: Into<String>> From<S> for Message {
|
||||
}
|
||||
|
||||
|
||||
/// Provides a utility method for more ergonomically parsing a `SocketAddr` when panicking is
|
||||
/// acceptable.
|
||||
/// Provides a utility method for more ergonomically parsing a `SocketAddr` when only one is
|
||||
/// needed.
|
||||
pub trait FirstSocketAddr: ToSocketAddrs {
|
||||
/// Returns the first resolved `SocketAddr`, if one exists.
|
||||
fn try_first_socket_addr(&self) -> io::Result<SocketAddr> {
|
||||
if let Some(a) = self.to_socket_addrs()?.next() {
|
||||
Ok(a)
|
||||
} else {
|
||||
Err(io::Error::new(io::ErrorKind::AddrNotAvailable,
|
||||
"`ToSocketAddrs::to_socket_addrs` returned an empty iterator."))
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the first resolved `SocketAddr` or panics otherwise.
|
||||
fn first_socket_addr(&self) -> SocketAddr {
|
||||
self.to_socket_addrs().unwrap().next().unwrap()
|
||||
self.try_first_socket_addr().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user