pub struct Ts<T: Tsify>(/* private fields */);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()?)
}Implementations§
Source§impl<T> Ts<T>where
T: Tsify,
impl<T> Ts<T>where
T: Tsify,
Sourcepub fn js_value(&self) -> JsValue
pub fn js_value(&self) -> JsValue
Returns the inner JsValue representation, cloning the JS handle.
Sourcepub fn new_unchecked(js: JsValue) -> Self
pub fn new_unchecked(js: JsValue) -> Self
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();
});Source§impl<T: Tsify + DeserializeOwned> Ts<T>
impl<T: Tsify + DeserializeOwned> Ts<T>
Trait Implementations§
Source§impl<T> FromWasmAbi for Ts<T>
impl<T> FromWasmAbi for Ts<T>
Source§impl<T> IntoWasmAbi for Ts<T>
impl<T> IntoWasmAbi for Ts<T>
Source§impl<'a, T> IntoWasmAbi for &'a Ts<T>
impl<'a, T> IntoWasmAbi for &'a Ts<T>
Source§impl<T> LongRefFromWasmAbi for Ts<T>
impl<T> LongRefFromWasmAbi for Ts<T>
Source§impl<T> OptionFromWasmAbi for Ts<T>
impl<T> OptionFromWasmAbi for Ts<T>
Source§impl<T> OptionIntoWasmAbi for Ts<T>
impl<T> OptionIntoWasmAbi for Ts<T>
Source§impl<T> RefFromWasmAbi for Ts<T>
impl<T> RefFromWasmAbi for Ts<T>
Source§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.Source§impl<T> VectorFromWasmAbi for Ts<T>
impl<T> VectorFromWasmAbi for Ts<T>
Source§impl<T> VectorIntoWasmAbi for Ts<T>
impl<T> VectorIntoWasmAbi for Ts<T>
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> 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.