Skip to main content

Module wrap

Module wrap 

Source
Expand description

Explicit routing wrappers.

A value in an RPC signature crosses the channel either as postcard bytes inside the payload, or as a Javascript value in the message array. The wrapper picks the route: Post and Transfer take the Javascript path, everything else is postcard-encoded and must implement postcard_schema::Schema.

Transfer additionally puts the value on the postMessage transfer list, so it moves rather than being copied by the structured clone algorithm.

No trait constrains what may be transferred. T must be a transferable object as defined by the structured clone algorithm (ArrayBuffer, MessagePort, OffscreenCanvas, ImageBitmap, the stream types, …); anything else is a DataCloneError thrown by the browser at the moment of sending. A typed array is not transferable: send Transfer<ArrayBuffer> and rebuild the view on the other side. A view over wasm linear memory can never be transferred, so Post it instead.

A bare Javascript type in a signature does not compile:

#[web_rpc::service]
pub trait Echo {
    fn echo(&self, value: js_sys::JsString) -> js_sys::JsString;
}

A payload type without #[derive(Schema)] is rejected at the argument that uses it:

#[derive(serde::Serialize, serde::Deserialize)]
pub struct Point { x: u32 }

#[web_rpc::service]
pub trait Plot {
    fn plot(&self, point: Point);
}

Structs§

Post
Wrapper that routes T across the channel as a Javascript value.
Transfer
Wrapper that routes T across the channel as a Javascript value and puts it on the transfer list, moving it out of the sending context.