pub struct Runtime { /* private fields */ }
Available on 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

Create a new runtime instance with default seed and config.

Create a new runtime instance with given seed and config.

Register a simulator.

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.

Create a node.

The returned handle can be used to spawn tasks that run on this node.

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::<()>());

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;
});

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

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

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

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

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more

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

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more