Trait Upcast

pub trait Upcast<T: ?Sized>

A trait for type-safe generic upcasting.

⚠️ Unstable

This is part of the internal convert module, no stability guarantees are provided. Use at your own risk. See its documentation for more details.

Note

Upcast<T> has a blanket implementation for all types where T: UpcastFrom<Self>. New upcast relationships should typically be defined by implementing FromUpcast rather than Upcast directly, to avoid orphan rule issues.

Provided Methods

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

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

This enables proper nested conversions that obey subtyping rules, supporting strict API type checking.

The common pattern when passing a narrow type is to call upcast() or upcast_into() to obtain the correct type for the function usage, while ensuring safe type checked usage.

For example, if passing Promise<Number> as an argument to a function where Promise<JsValue> is expected, or Function<JsValue> as an argument where Function<Number> is expected.

This is a compile time conversion only by the nature of the erasable generics type system.

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

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

This enables proper nested conversions that obey subtyping rules, supporting strict API type checking.

The common pattern when passing a narrow type is to call upcast() or upcast_into() to obtain the correct type for the function usage, while ensuring safe type checked usage.

For example, if passing Promise<Number> as an argument to a function where Promise<JsValue> is expected, or FunctionArgs<JsValue> as an argument where FunctionArgs<Number> is expected.

This is a compile time conversion only by the nature of the erasable generics type system.

Implementors