This commit is contained in:
John Smith
2022-07-13 09:51:56 -04:00
parent 007150c818
commit b90d453cef
13 changed files with 340 additions and 374 deletions

View File

@@ -32,6 +32,49 @@ cfg_if! {
}
//////////////////////////////////////////////////////////////////
// Non-fallible timeout conversions
pub trait TimeoutOrExt<T> {
fn into_timeout_or(self) -> TimeoutOr<T>;
}
impl<T> TimeoutOrExt<T> for Result<T, TimeoutError> {
fn into_timeout_or(self) -> TimeoutOr<T> {
self.ok().map(|v| TimeoutOr::<T>::Value(v)).unwrap_or(TimeoutOr::<T>::Timeout)
}
}
pub trait IoTimeoutOrExt<T> {
fn into_timeout_or(self) -> io::Result<TimeoutOr<T>>;
}
impl<T> IoTimeoutOrExt<T> for io::Result<T> {
fn into_timeout_or(self) -> io::Result<TimeoutOr<T>> {
match self {
Ok(v) => Ok(TimeoutOr::<T>::Value(v)),
Err(e) if e.kind() == io::ErrorKind::TimedOut => Ok(TimeoutOr::<T>::Timeout),
Err(e) => Err(e),
}
}
}
pub trait TimeoutOrResultExt<T, E> {
fn into_result(self) -> Result<TimeoutOr<T>, E>;
}
impl<T,E> TimeoutOrResultExt<T, E> for TimeoutOr<Result<T,E>> {
fn into_result(self) -> Result<TimeoutOr<T>, E> {
match self {
TimeoutOr::<Result::<T,E>>::Timeout => Ok(TimeoutOr::<T>::Timeout),
TimeoutOr::<Result::<T,E>>::Value(Ok(v)) => Ok(TimeoutOr::<T>::Value(v)),
TimeoutOr::<Result::<T,E>>::Value(Err(e)) => Err(e),
}
}
}
//////////////////////////////////////////////////////////////////
// Non-fallible timeout
pub enum TimeoutOr<T> {
Timeout,

View File

@@ -146,6 +146,13 @@ where
}
}
pub fn compatible_unspecified_socket_addr(socket_addr: &SocketAddr) -> SocketAddr {
match socket_addr {
SocketAddr::V4(_) => SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0),
SocketAddr::V6(_) => SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), 0),
}
}
pub fn listen_address_to_socket_addrs(listen_address: &str) -> EyreResult<Vec<SocketAddr>> {
// If no address is specified, but the port is, use ipv4 and ipv6 unspecified
// If the address is specified, only use the specified port and fail otherwise