Trait TryCastFromJs

Source
pub trait TryCastFromJs:
    CastFromJs<Abi = u32, Abi = u32>
    + RefFromWasmAbi
    + LongRefFromWasmAbi
    + Clone {
    type Error: Display + From<Error>;

    // Required method
    fn try_cast_from<'a, R>(value: &'a R) -> Result<Cast<'a, Self>, Self::Error>
       where R: AsRef<JsValue> + 'a;

    // Provided methods
    fn try_owned_from(value: impl AsRef<JsValue>) -> Result<Self, Self::Error> { ... }
    fn try_captured_cast_from(
        js_value: impl AsRef<JsValue>,
    ) -> Result<Cast<'static, Self>, Self::Error> { ... }
    fn resolve<'a, R>(
        js: &'a R,
        create: impl FnOnce() -> Result<Self, Self::Error>,
    ) -> Result<Cast<'a, Self>, Self::Error>
       where R: AsRef<JsValue> + 'a { ... }
    fn resolve_cast<'a, R>(
        js: &'a R,
        create: impl FnOnce() -> Result<Cast<'a, Self>, Self::Error>,
    ) -> Result<Cast<'a, Self>, Self::Error>
       where R: AsRef<JsValue> + 'a { ... }
}
Expand description

TryCastFromJs trait is meant to be implemented by the developer on any struct implementing CastFromJs trait. This trait provides a way to attempt to cast a JsValue into a Rust object or interpret the source data and create a temporary struct owned by by the Cast.

Required Associated Types§

Required Methods§

Source

fn try_cast_from<'a, R>(value: &'a R) -> Result<Cast<'a, Self>, Self::Error>
where R: AsRef<JsValue> + 'a,

Try to cast a JsValue into a Rust object. This should be user-defined function that attempts to cast a JsValue into a Rust object or interpret a source data and create a temporary struct owned by by the Cast.

Provided Methods§

Source

fn try_owned_from(value: impl AsRef<JsValue>) -> Result<Self, Self::Error>

Perform a user cast and consume the Cast container. This function will return a temporary user-created object created during [try_cast_from] or a clone of the casted reference.

Source

fn try_captured_cast_from( js_value: impl AsRef<JsValue>, ) -> Result<Cast<'static, Self>, Self::Error>

Source

fn resolve<'a, R>( js: &'a R, create: impl FnOnce() -> Result<Self, Self::Error>, ) -> Result<Cast<'a, Self>, Self::Error>
where R: AsRef<JsValue> + 'a,

Try to cast a JsValue into a Rust object, in cast of failure invoke a user-supplied closure that can try to create an instance of the object based on the supplied JsValue.

Source

fn resolve_cast<'a, R>( js: &'a R, create: impl FnOnce() -> Result<Cast<'a, Self>, Self::Error>, ) -> Result<Cast<'a, Self>, Self::Error>
where R: AsRef<JsValue> + 'a,

Try to cast a JsValue into a Rust object, in cast of failure invoke a user-supplied closure that can try to create an instance of the object based on the supplied JsValue. Unlike the [resolve] function, this function expects create closure to return a Cast. This is useful when routing the creation of the object to another function that is capable of creating a compatible Cast wrapper.

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.

Implementors§