Runtime

Trait Runtime 

Source
pub trait Runtime<'a> {
    // Required methods
    fn push_boxed(&mut self, future: Pin<Box<dyn Future<Output = ()>>>);
    fn spawn_boxed(
        &'a mut self,
        future: Pin<Box<dyn Future<Output = ()>>>,
    ) -> SpawnedFuture<'a> ;
    fn sleep<'b>(
        &'a self,
        duration: Duration,
    ) -> Pin<Box<dyn Future<Output = ()> + 'b>>;
    fn stop(&mut self) -> Stop ;

    // Provided methods
    fn push(&mut self, future: impl Future<Output = ()> + 'static) { ... }
    fn spawn(
        &'a mut self,
        future: impl Future<Output = ()> + 'static,
    ) -> SpawnedFuture<'a>  { ... }
    fn sleep_ms<'b>(
        &'a self,
        amount: u64,
    ) -> Pin<Box<dyn Future<Output = ()> + 'b>> { ... }
}
Expand description

Auto-trait with the methods that will actually be called by the users of the runtime.

Required Methods§

Source

fn push_boxed(&mut self, future: Pin<Box<dyn Future<Output = ()>>>)

Adds a new future to the queue to be completed.

Source

fn spawn_boxed( &'a mut self, future: Pin<Box<dyn Future<Output = ()>>>, ) -> SpawnedFuture<'a>

Adds a new future to the queue to be completed and returns a future waiting for the added future’s completion.

Source

fn sleep<'b>( &'a self, duration: Duration, ) -> Pin<Box<dyn Future<Output = ()> + 'b>>

Asynchronously sleeps

Source

fn stop(&mut self) -> Stop

Stops the runtime. This does not exit the process.

Provided Methods§

Source

fn push(&mut self, future: impl Future<Output = ()> + 'static)

Adds a new future to the queue to be completed.

Examples found in repository?
examples/tcp.rs (line 15)
13fn main() {
14    let mut runtime = QueuedRuntime::new();
15    runtime.push(go(("0.0.0.0", 5000)));
16    sync(runtime);
17}
18
19async fn go(addr: (&str, u16)) {
20    let mut listener = TcpListener::bind(addr).unwrap();
21    loop {
22        get_current_runtime()
23            .await
24            .push(handle(accept(&mut listener).await.unwrap()));
25    }
26}
More examples
Hide additional examples
examples/recursion.rs (line 8)
6fn main() {
7    let mut runtime = QueuedRuntime::new();
8    runtime.push(print_something_after_ms(0));
9    sync(runtime);
10}
11
12async fn print_something_after_ms(ms: u64) {
13    get_current_runtime()
14        .await
15        .sleep(Duration::from_millis(ms))
16        .await;
17    println!("something after {ms}ms! :D");
18    get_current_runtime()
19        .await
20        .push(print_something_after_ms(ms + 1));
21}
examples/runtime.rs (line 7)
4fn main() {
5    let mut runtime = QueuedRuntime::new();
6    for _ in 0..50 {
7        runtime.push(print_something_after_ms(2000));
8    }
9    sync(runtime);
10}
Source

fn spawn( &'a mut self, future: impl Future<Output = ()> + 'static, ) -> SpawnedFuture<'a>

Adds a new future to the queue to be completed and returns a future waiting for the added future’s completion.

Source

fn sleep_ms<'b>(&'a self, amount: u64) -> Pin<Box<dyn Future<Output = ()> + 'b>>

Asynchronously sleeps some amount of milliseconds

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'a> Runtime<'a> for RuntimeWrapper<'a>

Source§

impl<'a, T> Runtime<'a> for T
where T: InternalRuntime,