Skip to main content

Upcast

Trait Upcast 

Source
pub trait Upcast<T: ?Sized> {
    // Provided methods
    fn upcast(&self) -> &T
       where Self: ErasableGeneric,
             T: Sized + ErasableGeneric<Repr = <Self as ErasableGeneric>::Repr> { ... }
    fn upcast_into(self) -> T
       where Self: Sized + ErasableGeneric,
             T: Sized + ErasableGeneric<Repr = <Self as ErasableGeneric>::Repr> { ... }
}
Expand description

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§

Source

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.

Source

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.

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§

Source§

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