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,
impl<T> Ts<T>where
T: Tsify,
pub fn js_value(&self) -> JsValuewhere
<T as Tsify>::JsType: JsCast,
pub fn js_value(&self) -> JsValuewhere
<T as Tsify>::JsType: JsCast,
Returns the inner JsValue representation, cloning the JS handle.
pub fn new_unchecked(js: JsValue) -> Ts<T>
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,
impl<T> Ts<T>where
T: Tsify + DeserializeOwned,
<T as Tsify>::JsType: Clone,
Trait Implementations§
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.
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.
§impl<T> FromWasmAbi for Ts<T>where
T: Tsify,
<T as Tsify>::JsType: FromWasmAbi,
impl<T> FromWasmAbi for Ts<T>where
T: Tsify,
<T as Tsify>::JsType: FromWasmAbi,
§impl<T> IntoWasmAbi for Ts<T>where
T: Tsify,
<T as Tsify>::JsType: IntoWasmAbi,
impl<T> IntoWasmAbi for Ts<T>where
T: Tsify,
<T as Tsify>::JsType: IntoWasmAbi,
§type Abi = <<T as Tsify>::JsType as IntoWasmAbi>::Abi
type Abi = <<T as Tsify>::JsType as IntoWasmAbi>::Abi
§fn into_abi(self) -> <Ts<T> as IntoWasmAbi>::Abi
fn into_abi(self) -> <Ts<T> as IntoWasmAbi>::Abi
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,
impl<'a, T> IntoWasmAbi for &'a Ts<T>where
T: Tsify,
<T as Tsify>::JsType: JsCast + WasmDescribe,
§type Abi = <&'a JsValue as IntoWasmAbi>::Abi
type Abi = <&'a JsValue as IntoWasmAbi>::Abi
§fn into_abi(self) -> <&'a Ts<T> as IntoWasmAbi>::Abi
fn into_abi(self) -> <&'a Ts<T> as IntoWasmAbi>::Abi
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,
impl<T> LongRefFromWasmAbi for Ts<T>where
T: Tsify,
<T as Tsify>::JsType: LongRefFromWasmAbi,
§type Abi = <JsValue as LongRefFromWasmAbi>::Abi
type Abi = <JsValue as LongRefFromWasmAbi>::Abi
RefFromWasmAbi::Abi§unsafe fn long_ref_from_abi(
js: <Ts<T> as LongRefFromWasmAbi>::Abi,
) -> <Ts<T> as LongRefFromWasmAbi>::Anchor
unsafe fn long_ref_from_abi( js: <Ts<T> as LongRefFromWasmAbi>::Abi, ) -> <Ts<T> as LongRefFromWasmAbi>::Anchor
RefFromWasmAbi::ref_from_abi§impl<T> OptionFromWasmAbi for Ts<T>where
T: Tsify,
<T as Tsify>::JsType: OptionFromWasmAbi,
impl<T> OptionFromWasmAbi for Ts<T>where
T: Tsify,
<T as Tsify>::JsType: OptionFromWasmAbi,
§impl<T> OptionIntoWasmAbi for Ts<T>where
T: Tsify,
<T as Tsify>::JsType: OptionIntoWasmAbi,
impl<T> OptionIntoWasmAbi for Ts<T>where
T: Tsify,
<T as Tsify>::JsType: OptionIntoWasmAbi,
§impl<T> RefFromWasmAbi for Ts<T>where
T: Tsify,
<T as Tsify>::JsType: RefFromWasmAbi,
impl<T> RefFromWasmAbi for Ts<T>where
T: Tsify,
<T as Tsify>::JsType: RefFromWasmAbi,
§type Anchor = ManuallyDrop<Ts<T>>
type Anchor = ManuallyDrop<Ts<T>>
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
type Abi = <JsValue as RefFromWasmAbi>::Abi
Self are recovered from.§unsafe fn ref_from_abi(
js: <Ts<T> as RefFromWasmAbi>::Abi,
) -> <Ts<T> as RefFromWasmAbi>::Anchor
unsafe fn ref_from_abi( js: <Ts<T> as RefFromWasmAbi>::Abi, ) -> <Ts<T> as RefFromWasmAbi>::Anchor
§impl<T> VectorFromWasmAbi for Ts<T>where
T: Tsify,
<T as Tsify>::JsType: VectorFromWasmAbi,
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,
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>
impl<T> RefUnwindSafe for Ts<T>
impl<T> Send for Ts<T>
impl<T> Sync for Ts<T>
impl<T> Unpin for Ts<T>
impl<T> UnsafeUnpin for Ts<T>
impl<T> UnwindSafe for Ts<T>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> MaybeFromWasmAbi for Twhere
T: OptionFromWasmAbi,
impl<T> MaybeFromWasmAbi for Twhere
T: OptionFromWasmAbi,
Source§type MaybeAbi = <T as FromWasmAbi>::Abi
type MaybeAbi = <T as FromWasmAbi>::Abi
Option<Self>.Source§unsafe fn from_maybe_abi(js: <T as FromWasmAbi>::Abi) -> T
unsafe fn from_maybe_abi(js: <T as FromWasmAbi>::Abi) -> T
unsafe fn try_from_maybe_abi( js: <T as FromWasmAbi>::Abi, ) -> Result<T, &'static str>
Source§impl<T> MaybeIntoWasmAbi for Twhere
T: OptionIntoWasmAbi,
impl<T> MaybeIntoWasmAbi for Twhere
T: OptionIntoWasmAbi,
Source§type MaybeAbi = <T as IntoWasmAbi>::Abi
type MaybeAbi = <T as IntoWasmAbi>::Abi
Option<Self>.Source§fn into_maybe_abi(self) -> <T as IntoWasmAbi>::Abi
fn into_maybe_abi(self) -> <T as IntoWasmAbi>::Abi
Option<Self> ABI.Source§impl<T, A> RefIntoWasmAbi for T
impl<T, A> RefIntoWasmAbi for T
Source§fn describe_ref()
fn describe_ref()
&Self.Source§fn ref_into_abi(&self) -> A
fn ref_into_abi(&self) -> A
&self into Self::Abi.Source§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
Source§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
IntoWasmAbi::AbiSource§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
IntoWasmAbi::into_abi, except that it may throw and never
return in the case of Err.