wasmtime_wasi

Trait Subscribe

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

A trait used internally within a Pollable 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.

use tokio::time::{self, Duration, Instant};
use wasmtime_wasi::{WasiView, Subscribe, subscribe, Pollable, async_trait};
use wasmtime::component::Resource;
use wasmtime::Result;

fn sleep(cx: &mut dyn WasiView, dur: Duration) -> Result<Resource<Pollable>> {
    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 Subscribe 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 Self: 'async_trait, 'life0: '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 Subscribe 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 Subscribe for Box<dyn HostInputStream>

source§

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

source§

impl Subscribe for Box<dyn HostOutputStream>

source§

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

Implementors§