Skip to main content

Ts

Struct Ts 

#[repr(transparent)]
pub struct Ts<T>(<T as Tsify>::JsType, PhantomData<T>) where T: Tsify;
Expand description

A wrapper for a type implementing [Tsify], to be used in #[wasm_bindgen] function parameters and return types.

Conceptually, this is just a JsValue that has easy conversion methods. The reason it exists is that the #[tsify(into_wasm_abi, from_wasm_abi)] attributes cause memory leaks when (de)serialization fails. This approach is less automatic but doesn’t leak memory.

Exposes two main methods: ts_value.to_rust() and Ts::from_rust to convert. However for the latter you can also use [Tsify::into_ts] as t.into_ts().

This can (and should) be used with the -> Result<_, JsError> pattern from wasm-bindgen to automatically throw any Err value returned. The point of this type is robustness to serialization errors, especially when JavaScript (being a dynamic language) could call your functions with any kind of data at runtime. Panicking defeats this purpose.

§Example

use tsify::Tsify;
use tsify::Ts;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsError;

#[derive(Tsify, serde::Deserialize, serde::Serialize)]
pub struct Vec2 {
   x: f64,
   y: f64,
}

#[wasm_bindgen]
pub fn rotate(v: Ts<Vec2>, theta_rad: f64) -> Result<Ts<Vec2>, JsError> {
    // Deserialize to rust type, throw deserialization error if fails
    let Vec2 { x, y } = v.to_rust()?;
    // Do some maths
    let cos = theta_rad.cos();
    let sin = theta_rad.sin();
    let result = Vec2 {
        x: x * cos - y * sin,
        y: x * sin + y * cos,
    };
    // Serialize back to JsValue, throw serialization error if fails
    Ok(result.into_ts()?)
}

Tuple Fields§

§0: <T as Tsify>::JsType§1: PhantomData<T>

Implementations§

§

impl<T> Ts<T>
where T: Tsify,

pub fn js_value(&self) -> JsValue
where <T as Tsify>::JsType: JsCast,

Returns the inner JsValue representation, cloning the JS handle.

pub fn new_unchecked(js: JsValue) -> Ts<T>

Reinterpret a JsValue as a Ts<T> without any checks. This is a zero cost operation.

If you get this wrong, the worst that can happen is that it will fail to deserialize when you call to_rust() on it.

use tsify::Tsify;
use tsify::Ts;
use wasm_bindgen::prelude::*;

#[derive(tsify::Tsify, serde::Deserialize)]
struct Point {
    x: f64,
    y: f64,
}

fn some_api(_closure: impl Fn(JsValue)) { /* ... */ }

some_api(|js_value: JsValue| {
    let ts_value: Ts<Point> = Ts::new_unchecked(js_value);
    let point = ts_value.to_rust().unwrap();
});
§

impl<T> Ts<T>
where T: Tsify + DeserializeOwned, <T as Tsify>::JsType: Clone,

pub fn to_rust(&self) -> Result<T, Error>

Converts the inner JSType (e.g. JsValue) into T

§

impl<T> Ts<T>
where T: Tsify + Serialize,

pub fn from_rust(rust: &T) -> Result<Ts<T>, Error>

Converts a rust type T into to the inner JSType (e.g. JsValue)

Trait Implementations§

§

impl<T> Clone for Ts<T>
where T: Tsify, <T as Tsify>::JsType: Clone,

§

fn clone(&self) -> Ts<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<T> Debug for Ts<T>
where T: Tsify,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T: Tsify> FromWasm for Ts<T>

Lets a shim name Ts<T> in a signature it was handed already-wrapped, for the hand-written extern "C" blocks that construct the wrapper themselves.

Source§

type Wire = Ts<T>

The type JavaScript actually passes.
Source§

fn from_wire(wire: Self::Wire) -> Result<Self, WireError>

Recovers the Rust value from the wire value.
§

impl<T> FromWasmAbi for Ts<T>
where T: Tsify, <T as Tsify>::JsType: FromWasmAbi,

§

type Abi = <<T as Tsify>::JsType as FromWasmAbi>::Abi

The Wasm ABI type that this converts from when coming back out from the ABI boundary.
§

unsafe fn from_abi(js: <Ts<T> as FromWasmAbi>::Abi) -> Ts<T>

Recover a Self from Self::Abi. Read more
§

impl<T> IntoWasmAbi for Ts<T>
where T: Tsify, <T as Tsify>::JsType: IntoWasmAbi,

§

type Abi = <<T as Tsify>::JsType as IntoWasmAbi>::Abi

The Wasm ABI type that this converts into when crossing the ABI boundary.
§

fn into_abi(self) -> <Ts<T> as IntoWasmAbi>::Abi

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
§

impl<'a, T> IntoWasmAbi for &'a Ts<T>
where T: Tsify, <T as Tsify>::JsType: JsCast + WasmDescribe,

§

type Abi = <&'a JsValue as IntoWasmAbi>::Abi

The Wasm ABI type that this converts into when crossing the ABI boundary.
§

fn into_abi(self) -> <&'a Ts<T> as IntoWasmAbi>::Abi

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
§

impl<T> LongRefFromWasmAbi for Ts<T>
where T: Tsify, <T as Tsify>::JsType: LongRefFromWasmAbi,

§

type Abi = <JsValue as LongRefFromWasmAbi>::Abi

Same as RefFromWasmAbi::Abi
§

type Anchor = Ts<T>

Same as RefFromWasmAbi::Anchor
§

unsafe fn long_ref_from_abi( js: <Ts<T> as LongRefFromWasmAbi>::Abi, ) -> <Ts<T> as LongRefFromWasmAbi>::Anchor

Same as RefFromWasmAbi::ref_from_abi
§

impl<T> OptionFromWasmAbi for Ts<T>
where T: Tsify, <T as Tsify>::JsType: OptionFromWasmAbi,

§

fn is_none(abi: &<Ts<T> as FromWasmAbi>::Abi) -> bool

Tests whether the argument is a “none” instance. If so it will be deserialized as None, and otherwise it will be passed to FromWasmAbi.
§

impl<T> OptionIntoWasmAbi for Ts<T>
where T: Tsify, <T as Tsify>::JsType: OptionIntoWasmAbi,

§

fn none() -> <Ts<T> as IntoWasmAbi>::Abi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
§

impl<T> RefFromWasmAbi for Ts<T>
where T: Tsify, <T as Tsify>::JsType: RefFromWasmAbi,

§

type Anchor = ManuallyDrop<Ts<T>>

The type that holds the reference to Self for the duration of the invocation of the function that has an &Self parameter. This is required to ensure that the lifetimes don’t persist beyond one function call, and so that they remain anonymous.
§

type Abi = <JsValue as RefFromWasmAbi>::Abi

The Wasm ABI type references to Self are recovered from.
§

unsafe fn ref_from_abi( js: <Ts<T> as RefFromWasmAbi>::Abi, ) -> <Ts<T> as RefFromWasmAbi>::Anchor

Recover a Self::Anchor from Self::Abi. Read more
Source§

impl<T: Tsify> ToWasm for Ts<T>

Source§

type Wire = Ts<T>

The type JavaScript actually receives.
Source§

fn to_wire(self) -> Result<Self::Wire, WireError>

Converts the Rust value into the wire value.
§

impl<T> VectorFromWasmAbi for Ts<T>
where T: Tsify, <T as Tsify>::JsType: VectorFromWasmAbi,

§

type Abi = <<T as Tsify>::JsType as VectorFromWasmAbi>::Abi

§

unsafe fn vector_from_abi(js: <Ts<T> as VectorFromWasmAbi>::Abi) -> Box<[Ts<T>]>

§

impl<T> VectorIntoWasmAbi for Ts<T>
where T: Tsify, <T as Tsify>::JsType: VectorIntoWasmAbi,

§

type Abi = <<T as Tsify>::JsType as VectorIntoWasmAbi>::Abi

§

fn vector_into_abi(vector: Box<[Ts<T>]>) -> <Ts<T> as VectorIntoWasmAbi>::Abi

Auto Trait Implementations§

§

impl<T> Freeze for Ts<T>
where <T as Tsify>::JsType: Freeze, PhantomData<T>: Freeze,

§

impl<T> RefUnwindSafe for Ts<T>
where <T as Tsify>::JsType: RefUnwindSafe, PhantomData<T>: RefUnwindSafe,

§

impl<T> Send for Ts<T>
where <T as Tsify>::JsType: Send, PhantomData<T>: Send,

§

impl<T> Sync for Ts<T>
where <T as Tsify>::JsType: Sync, PhantomData<T>: Sync,

§

impl<T> Unpin for Ts<T>
where <T as Tsify>::JsType: Unpin, PhantomData<T>: Unpin,

§

impl<T> UnsafeUnpin for Ts<T>
where <T as Tsify>::JsType: UnsafeUnpin, PhantomData<T>: UnsafeUnpin,

§

impl<T> UnwindSafe for Ts<T>
where <T as Tsify>::JsType: UnwindSafe, PhantomData<T>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeFromWasmAbi for T

Source§

type MaybeAbi = <T as FromWasmAbi>::Abi

The ABI of Option<Self>.
Source§

unsafe fn from_maybe_abi(js: <T as FromWasmAbi>::Abi) -> T

Recovers a value from the Option<Self> ABI, once is_none has ruled None out. Read more
Source§

unsafe fn try_from_maybe_abi( js: <T as FromWasmAbi>::Abi, ) -> Result<T, &'static str>

Source§

fn is_none(js: &<T as FromWasmAbi>::Abi) -> bool

Tests the ABI value for None.
Source§

impl<T> MaybeIntoWasmAbi for T

Source§

type MaybeAbi = <T as IntoWasmAbi>::Abi

The ABI of Option<Self>.
Source§

fn into_maybe_abi(self) -> <T as IntoWasmAbi>::Abi

Converts a value into the Option<Self> ABI.
Source§

fn none() -> <T as IntoWasmAbi>::Abi

Returns the ABI value for None, which JS reads as undefined. Read more
Source§

impl<T, A> RefIntoWasmAbi for T
where A: WasmAbi, &'a T: for<'a> IntoWasmAbi<Abi = A>, T: ?Sized,

Source§

type Abi = A

The Wasm ABI type that &Self converts into.
Source§

fn describe_ref()

Describe &Self.
Source§

fn ref_into_abi(&self) -> A

Convert &self into Self::Abi.
Source§

impl<T> ReturnWasmAbi for T
where T: IntoWasmAbi,

Source§

type Abi = <T as IntoWasmAbi>::Abi

Same as IntoWasmAbi::Abi
Source§

fn return_abi(self) -> <T as ReturnWasmAbi>::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more