use derive_deftly::define_derive_deftly;
define_derive_deftly! {
SomeMockRuntime for struct, expect items:
$(
${when fmeta(mock(task))}
impl <$tgens> Spawn for $ttype {
fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
self.$fname.spawn_obj(future)
}
}
impl <$tgens> BlockOn for $ttype {
fn block_on<F: Future>(&self, future: F) -> F::Output {
self.$fname.block_on(future)
}
}
)
$(
${when fmeta(mock(net))}
#[async_trait]
impl <$tgens> TcpProvider for $ttype {
type TcpStream = <$ftype as TcpProvider>::TcpStream;
type TcpListener = <$ftype as TcpProvider>::TcpListener;
async fn connect(&self, addr: &SocketAddr) -> IoResult<Self::TcpStream> {
self.$fname.connect(addr).await
}
async fn listen(&self, addr: &SocketAddr) -> IoResult<Self::TcpListener> {
self.$fname.listen(addr).await
}
}
impl <$tgens> TlsProvider<<$ftype as TcpProvider>::TcpStream> for $ttype {
type Connector = <$ftype as TlsProvider<
<$ftype as TcpProvider>::TcpStream
>>::Connector;
type TlsStream = <$ftype as TlsProvider<
<$ftype as TcpProvider>::TcpStream
>>::TlsStream;
fn tls_connector(&self) -> Self::Connector {
self.$fname.tls_connector()
}
fn supports_keying_material_export(&self) -> bool {
self.$fname.supports_keying_material_export()
}
}
#[async_trait]
impl <$tgens> UdpProvider for $ttype {
type UdpSocket = <$ftype as UdpProvider>::UdpSocket;
#[inline]
async fn bind(&self, addr: &SocketAddr) -> IoResult<Self::UdpSocket> {
self.$fname.bind(addr).await
}
}
)
$(
${when fmeta(mock(sleep))}
impl <$tgens> SleepProvider for $ttype {
type SleepFuture = <$ftype as SleepProvider>::SleepFuture;
fn sleep(&self, dur: Duration) -> Self::SleepFuture {
self.$fname.sleep(dur)
}
fn now(&self) -> Instant {
self.$fname.now()
}
fn wallclock(&self) -> SystemTime {
self.$fname.wallclock()
}
fn block_advance<T: Into<String>>(&self, reason: T) {
self.$fname.block_advance(reason);
}
fn release_advance<T: Into<String>>(&self, reason: T) {
self.$fname.release_advance(reason);
}
fn allow_one_advance(&self, dur: Duration) {
self.$fname.allow_one_advance(dur);
}
}
impl <$tgens> CoarseTimeProvider for $ttype {
fn now_coarse(&self) -> CoarseInstant {
self.$fname.now_coarse()
}
}
)
const _: fn() = || {
fn x(_: impl Runtime) { }
fn check_impl_runtime<$tgens>(t: $ttype) { x(t) }
};
}
pub(crate) mod impl_runtime_prelude {
pub(crate) use async_trait::async_trait;
pub(crate) use derive_deftly::Deftly;
pub(crate) use futures::task::{FutureObj, Spawn, SpawnError};
pub(crate) use futures::Future;
pub(crate) use std::io::Result as IoResult;
pub(crate) use std::net::SocketAddr;
pub(crate) use std::time::{Duration, Instant, SystemTime};
pub(crate) use tor_rtcompat::{
BlockOn, CoarseInstant, CoarseTimeProvider, Runtime, SleepProvider, TcpProvider,
TlsProvider, UdpProvider,
};
}