wasmtime_wasi

Trait Pollable

Source
pub trait Pollable: Send + 'static {
    // Required method
    fn ready<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: 'async_trait;
}
Expand description

The trait used to implement DynPollable to create a pollable resource in wasi:io/poll.

This trait is the internal implementation detail of any pollable resource in this crate’s implementation of WASI. The ready function is an async fn which resolves when the implementation is ready. Using native async Rust enables this type’s readiness to compose with other types’ readiness throughout the WASI implementation.

This trait is used in conjunction with subscribe to create a pollable resource.

§Example

This is a simple example of creating a Pollable resource from a few parameters.

Instant) {} } }
use tokio::time::{self, Duration, Instant};
use wasmtime_wasi_io::{IoView, poll::{Pollable, subscribe, DynPollable}, async_trait};
use wasmtime::component::Resource;
use wasmtime::Result;

fn sleep(cx: &mut dyn IoView, dur: Duration) -> Result<Resource<DynPollable>> {
    let end = Instant::now() + dur;
    let sleep = MySleep { end };
    let sleep_resource = cx.table().push(sleep)?;
    subscribe(cx.table(), sleep_resource)
}

struct MySleep {
    end: Instant,
}

#[async_trait]
impl Pollable for MySleep {
    async fn ready(&mut self) {
        tokio::time::sleep_until(self.end).await;
    }
}

Required Methods§

Source

fn ready<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

An asynchronous function which resolves when this object’s readiness operation is ready.

This function is invoked as part of poll in wasi:io/poll. The meaning of when this function Returns depends on what object this Pollable is attached to. When the returned future resolves then the corresponding call to wasi:io/poll will return.

Note that this method does not return an error. Returning an error should be done through accessors on the object that this pollable is connected to. The call to wasi:io/poll itself does not return errors, only a list of ready objects.

Implementations on Foreign Types§

Source§

impl Pollable for Box<dyn InputStream>

Source§

fn ready<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait, Box<dyn InputStream>: 'async_trait,

Source§

impl Pollable for Box<dyn OutputStream>

Source§

fn ready<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait, Box<dyn OutputStream>: 'async_trait,

Implementors§