pub struct WasmtimeEngineProviderAsync { /* private fields */ }
Available on crate feature
async
only.Expand description
A waPC engine provider that encapsulates the Wasmtime WebAssembly runtime. This can be used inside of async contexts.
Refer to
WasmtimeEngineProviderBuilder::build_async
to create an instance of this struct.
§Example
use wasmtime_provider::WasmtimeEngineProviderBuilder;
use wapc::WapcHostAsync;
use std::error::Error;
// Sample host callback that prints the operation a WASM module requested.
async fn host_callback(
id: u64,
bd: String,
ns: String,
op: String,
payload: Vec<u8>,
) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
println!(
"Guest {} invoked '{}->{}:{}' on the host with a payload of '{}'",
id,
bd,
ns,
op,
::std::str::from_utf8(&payload).unwrap()
);
Ok(vec![])
}
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error>> {
let callback: Box<wapc::HostCallbackAsync> = Box::new(move |id, bd, ns, op, payload| {
let fut = host_callback(id, bd, ns, op, payload);
Box::pin(fut)
});
let file = "../../wasm/crates/wasm-basic/build/wasm_basic.wasm";
let module_bytes = std::fs::read(file)?;
let engine = WasmtimeEngineProviderBuilder::new()
.module_bytes(&module_bytes)
.build_async()?;
let host = WapcHostAsync::new(Box::new(engine), Some(callback)).await?;
let res = host.call("ping", b"payload bytes").await?;
assert_eq!(res, b"payload bytes");
Ok(())
}
Trait Implementations§
Source§impl Clone for WasmtimeEngineProviderAsync
impl Clone for WasmtimeEngineProviderAsync
Source§impl WebAssemblyEngineProviderAsync for WasmtimeEngineProviderAsync
impl WebAssemblyEngineProviderAsync for WasmtimeEngineProviderAsync
Source§fn init<'life0, 'async_trait>(
&'life0 mut self,
host: Arc<ModuleStateAsync>,
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn init<'life0, 'async_trait>(
&'life0 mut self,
host: Arc<ModuleStateAsync>,
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Tell the engine provider that it can do whatever processing it needs to do for
initialization and give it access to the module state
Source§fn call<'life0, 'async_trait>(
&'life0 mut self,
op_length: i32,
msg_length: i32,
) -> Pin<Box<dyn Future<Output = Result<i32, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn call<'life0, 'async_trait>(
&'life0 mut self,
op_length: i32,
msg_length: i32,
) -> Pin<Box<dyn Future<Output = Result<i32, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Trigger the waPC function call. Engine provider is responsible for execution and using the appropriate methods
on the module host. When this function is complete, the guest response and optionally the guest
error must be set to represent the high-level call result
Source§fn replace<'life0, 'life1, 'async_trait>(
&'life0 mut self,
module: &'life1 [u8],
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn replace<'life0, 'life1, 'async_trait>(
&'life0 mut self,
module: &'life1 [u8],
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called by the host to replace the WebAssembly module bytes of the previously initialized module. Engine must return an
error if it does not support bytes replacement.
Auto Trait Implementations§
impl Freeze for WasmtimeEngineProviderAsync
impl !RefUnwindSafe for WasmtimeEngineProviderAsync
impl Send for WasmtimeEngineProviderAsync
impl Sync for WasmtimeEngineProviderAsync
impl Unpin for WasmtimeEngineProviderAsync
impl !UnwindSafe for WasmtimeEngineProviderAsync
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> GetSetFdFlags for T
impl<T> GetSetFdFlags for T
Source§fn get_fd_flags(&self) -> Result<FdFlags, Error>where
T: AsFilelike,
fn get_fd_flags(&self) -> Result<FdFlags, Error>where
T: AsFilelike,
Query the “status” flags for the
self
file descriptor.Source§fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>where
T: AsFilelike,
fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>where
T: AsFilelike,
Source§fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>where
T: AsFilelike,
fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>where
T: AsFilelike,
Set the “status” flags for the
self
file descriptor. Read moreSource§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more