pub trait TimeSource:
Send
+ Sync
+ Clone
+ 'static {
// Required methods
fn now_nanos(&self) -> u64;
fn sleep(
&self,
duration: Duration,
) -> Pin<Box<dyn Future<Output = ()> + Send>>;
fn sleep_until(
&self,
deadline_nanos: u64,
) -> Pin<Box<dyn Future<Output = ()> + Send>>;
fn timeout<F, T>(
&self,
duration: Duration,
future: F,
) -> Pin<Box<dyn Future<Output = Option<T>> + Send>>
where F: Future<Output = T> + Send + 'static,
T: Send + 'static;
// Provided methods
fn now(&self) -> Duration { ... }
fn connection_idle_timeout(&self) -> Duration { ... }
fn supports_keepalive(&self) -> bool { ... }
}Expand description
Abstraction over time operations supporting both real and virtual time.
This trait allows code to be written once and run in both production (with real time) and simulation (with virtual time) contexts.
Required Methods§
Sourcefn sleep(&self, duration: Duration) -> Pin<Box<dyn Future<Output = ()> + Send>>
fn sleep(&self, duration: Duration) -> Pin<Box<dyn Future<Output = ()> + Send>>
Creates a future that completes after the given duration.
Provided Methods§
Sourcefn connection_idle_timeout(&self) -> Duration
fn connection_idle_timeout(&self) -> Duration
Returns the duration after which an idle connection should be considered dead.
VirtualTime uses a very long timeout (1 hour) to avoid premature disconnections when time is advanced rapidly by auto-advance tasks. RealTime uses the standard 120 seconds.
Sourcefn supports_keepalive(&self) -> bool
fn supports_keepalive(&self) -> bool
Returns whether keepalive pings should be enabled for connections using this time source.
VirtualTime returns false because:
- The interaction between auto-advance and keepalive timing causes spurious failures
- Aborting the keepalive task with VirtualTime causes hangs (abort() + VirtualSleep issue)
- Keepalive is meaningless for simulated connections anyway
See issue #2695 for details.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".