pub trait TransferableType: Debug + Clone {
// Required methods
fn underlying_transfer_objects(&self) -> Vec<JsValue>;
fn to_js_value(&self) -> JsValue;
fn from_js_value(value: JsValue) -> Self;
}Expand description
A trait for implementing the JS types that can be transferred to other threads without copying.
Such objects utilise the separate second argument of the postMessage webworker API dedicated to transferable objects.
All basic types stable in js-sys/web-sys supporting transfers mentioned in mozilla docs are built-in,
along with the most common derivatives like js_sys::Uint8Array.
Vec<T>, std::collections::HashMap<String, T>, Option<T> are also implemented, as long as T implements TransferableType.
JsValue is implemented as a fallback for downstream unimplemented types that are known to work with transferables.
You can implement TransferableType for custom structs wrapping a js value, as long as the underlying transfer object can be created from the js value synchronously, see the trait implementation of js_sys::Uint8Array for an example.
Transferable mozilla spec: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects#supported_objects
Example:
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct MyTransferable {
#[serde(with = "leptos_workers::transferable")]
arr: js_sys::Uint8Array,
}Required Methods§
Sourcefn underlying_transfer_objects(&self) -> Vec<JsValue>
fn underlying_transfer_objects(&self) -> Vec<JsValue>
Extract the underlying object that needs to be passed separately during the postMessage call.
A vec must be returned, as this trait supports multiple transferable objects, which must be passed to postMessage flat.
Transferable mozilla spec: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects
Sourcefn to_js_value(&self) -> JsValue
fn to_js_value(&self) -> JsValue
Convert into a generic js value.
Sourcefn from_js_value(value: JsValue) -> Self
fn from_js_value(value: JsValue) -> Self
Convert the type from a generic js value back to the specialized type.
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.
Implementations on Foreign Types§
Source§impl TransferableType for ArrayBuffer
impl TransferableType for ArrayBuffer
fn underlying_transfer_objects(&self) -> Vec<JsValue>
fn from_js_value(value: JsValue) -> Self
fn to_js_value(&self) -> JsValue
Source§impl TransferableType for Uint8Array
impl TransferableType for Uint8Array
fn underlying_transfer_objects(&self) -> Vec<JsValue>
fn from_js_value(value: JsValue) -> Self
fn to_js_value(&self) -> JsValue
Source§impl TransferableType for JsValue
If the user had a random type they knew worked with transferables but leptos_workers haven’t implemented a type for,
they can cast it to a JsValue and use this implementation.
impl TransferableType for JsValue
If the user had a random type they knew worked with transferables but leptos_workers haven’t implemented a type for, they can cast it to a JsValue and use this implementation.