pub struct Runtime { /* private fields */ }
madsim
only.Expand description
The madsim runtime.
The runtime provides basic components for deterministic simulation, including a random number generator, timer, task scheduler, and simulated network and file system.
Implementations
sourceimpl Runtime
impl Runtime
sourcepub fn with_seed_and_config(seed: u64, config: Config) -> Self
pub fn with_seed_and_config(seed: u64, config: Config) -> Self
Create a new runtime instance with given seed and config.
sourcepub fn add_simulator<S: Simulator>(&self)
pub fn add_simulator<S: Simulator>(&self)
Register a simulator.
sourcepub fn handle(&self) -> &Handle
pub fn handle(&self) -> &Handle
Return a handle to the runtime.
The returned handle can be used by the supervisor (future in block_on) to control the whole system. For example, kill a node or disconnect the network.
sourcepub fn create_node(&self) -> NodeBuilder<'_>
pub fn create_node(&self) -> NodeBuilder<'_>
Create a node.
The returned handle can be used to spawn tasks that run on this node.
sourcepub fn block_on<F: Future>(&self, future: F) -> F::Output
pub fn block_on<F: Future>(&self, future: F) -> F::Output
Run a future to completion on the runtime. This is the runtime’s entry point.
This runs the given future on the current thread until it is complete.
Example
use madsim::runtime::Runtime;
let rt = Runtime::new();
let ret = rt.block_on(async { 1 });
assert_eq!(ret, 1);
Unlike usual async runtime, when there is no runnable task, it will panic instead of blocking.
use madsim::runtime::Runtime;
use std::future::pending;
Runtime::new().block_on(pending::<()>());
sourcepub fn set_time_limit(&mut self, limit: Duration)
pub fn set_time_limit(&mut self, limit: Duration)
Set a time limit of the execution.
The runtime will panic when time limit exceeded.
Example
use madsim::{runtime::Runtime, time::{sleep, Duration}};
let mut rt = Runtime::new();
rt.set_time_limit(Duration::from_secs(1));
rt.block_on(async {
sleep(Duration::from_secs(2)).await;
});
sourcepub fn check_determinism<F>(seed: u64, config: Config, f: fn() -> F) -> F::Output where
F: Future + 'static,
F::Output: Send,
pub fn check_determinism<F>(seed: u64, config: Config, f: fn() -> F) -> F::Output where
F: Future + 'static,
F::Output: Send,
Check determinism of the future.
Example
use madsim::{Config, runtime::Runtime, time::{Duration, sleep}};
use std::io::Read;
use rand::Rng;
Runtime::check_determinism(0, Config::default(), || async {
// read a real random number from OS
let mut file = std::fs::File::open("/dev/urandom").unwrap();
let mut buf = [0u8; 8];
file.read_exact(&mut buf).unwrap();
sleep(Duration::from_nanos(u64::from_ne_bytes(buf))).await;
});
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Runtime
impl !Send for Runtime
impl !Sync for Runtime
impl Unpin for Runtime
impl !UnwindSafe for Runtime
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
sourcefn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Convert Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more
sourcefn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
sourcefn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
sourcefn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert &mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more