#![doc(issue_tracker_base_url = "https://github.com/chainbound/msg-rs/issues/")]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
time::SystemTime,
};
use futures::future::BoxFuture;
mod channel;
pub use channel::{Channel, channel};
mod task;
pub use task::JoinMap;
pub mod span;
#[inline]
pub fn unix_micros() -> u64 {
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_micros() as u64
}
pub fn async_error<E: std::error::Error + Send + 'static, T>(
e: E,
) -> BoxFuture<'static, Result<T, E>> {
Box::pin(async move { Err(e) })
}
#[allow(non_upper_case_globals)]
pub mod constants {
pub const KiB: u32 = 1024;
pub const MiB: u32 = 1024 * KiB;
pub const GiB: u32 = 1024 * MiB;
}
pub trait SocketAddrExt: Sized {
fn unspecified_v4() -> Self;
fn unspecified_v6() -> Self;
fn as_unspecified(&self) -> Self;
}
impl SocketAddrExt for SocketAddr {
#[inline]
fn unspecified_v4() -> Self {
Self::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0))
}
#[inline]
fn unspecified_v6() -> Self {
Self::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0))
}
#[inline]
fn as_unspecified(&self) -> Self {
match self {
Self::V4(_) => Self::unspecified_v4(),
Self::V6(_) => Self::unspecified_v6(),
}
}
}
pub trait IpAddrExt: Sized {
fn as_localhost(&self) -> Self;
}
impl IpAddrExt for IpAddr {
#[inline]
fn as_localhost(&self) -> Self {
match self {
Self::V4(_) => Self::V4(Ipv4Addr::LOCALHOST),
Self::V6(_) => Self::V6(Ipv6Addr::LOCALHOST),
}
}
}