use std::future::Future;
use std::io;
use std::net::SocketAddr;
use std::time::Duration;
pub trait RuntimeCompletion: 'static {
type Sleep: Future<Output = ()>;
fn sleep(duration: Duration) -> Self::Sleep;
fn block_on<F: Future>(future: F) -> Result<F::Output, crate::error::Error>;
}
pub trait RuntimeLocal: RuntimeCompletion {
fn spawn_local<F: Future<Output = ()> + 'static>(future: F);
}
pub trait RuntimePoll: RuntimeCompletion<Sleep: Send> + Send + Sync {
fn spawn_send<F: Future<Output = ()> + Send + 'static>(future: F);
}
pub trait SocketConfig {
fn set_keepalive(
&self,
time: Duration,
interval: Option<Duration>,
retries: Option<u32>,
) -> io::Result<()>;
fn set_fast_open(&self) -> io::Result<()> {
Ok(())
}
#[cfg(target_os = "linux")]
fn bind_device(&self, _interface: &str) -> io::Result<()> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"interface binding not supported by this connector",
))
}
}
#[allow(async_fn_in_trait)]
pub trait ConnectorLocal: 'static {
type Stream: hyper::rt::Read + hyper::rt::Write + Unpin + SocketConfig + 'static;
async fn connect(&self, addr: SocketAddr) -> io::Result<Self::Stream>;
async fn connect_bound(
&self,
_addr: SocketAddr,
_local: std::net::IpAddr,
) -> io::Result<Self::Stream> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"connect_bound not supported by this connector",
))
}
#[allow(clippy::wrong_self_convention)]
fn from_std_tcp(&self, stream: std::net::TcpStream) -> io::Result<Self::Stream> {
let _ = stream;
Err(io::Error::new(
io::ErrorKind::Unsupported,
"from_std_tcp not supported by this connector",
))
}
#[allow(clippy::wrong_self_convention)]
fn into_std_tcp(&self, stream: Self::Stream) -> io::Result<std::net::TcpStream> {
let _ = stream;
Err(io::Error::new(
io::ErrorKind::Unsupported,
"into_std_tcp not supported by this connector",
))
}
}
pub trait ConnectorSend: Clone + Send + Sync + 'static {
type Stream: hyper::rt::Read + hyper::rt::Write + Send + Unpin + SocketConfig + 'static;
fn connect(&self, addr: SocketAddr) -> impl Future<Output = io::Result<Self::Stream>> + Send;
fn connect_bound(
&self,
_addr: SocketAddr,
_local: std::net::IpAddr,
) -> impl Future<Output = io::Result<Self::Stream>> + Send {
async {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"connect_bound not supported by this connector",
))
}
}
#[allow(clippy::wrong_self_convention)]
fn from_std_tcp(&self, stream: std::net::TcpStream) -> io::Result<Self::Stream> {
let _ = stream;
Err(io::Error::new(
io::ErrorKind::Unsupported,
"from_std_tcp not supported by this connector",
))
}
#[allow(clippy::wrong_self_convention)]
fn into_std_tcp(&self, stream: Self::Stream) -> io::Result<std::net::TcpStream> {
let _ = stream;
Err(io::Error::new(
io::ErrorKind::Unsupported,
"into_std_tcp not supported by this connector",
))
}
}