TransferableType

Trait TransferableType 

Source
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§

Source

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

Source

fn to_js_value(&self) -> JsValue

Convert into a generic js value.

Source

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

Source§

impl TransferableType for Uint8Array

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.

Source§

impl TransferableType for ImageBitmap

Source§

impl TransferableType for MessagePort

Source§

impl TransferableType for OffscreenCanvas

Source§

impl TransferableType for ReadableStream

Source§

impl TransferableType for RtcDataChannel

Source§

impl TransferableType for TransformStream

Source§

impl TransferableType for WritableStream

Source§

impl<T: TransferableType> TransferableType for Option<T>

Source§

impl<T: TransferableType> TransferableType for Vec<T>

Source§

impl<T: TransferableType, S: BuildHasher + Clone + Default> TransferableType for HashMap<String, T, S>

Implementors§