Skip to main content

VcValueTrait

Trait VcValueTrait 

Source
pub trait VcValueTrait:
    NonLocalValue
    + Send
    + Sync
    + 'static {
    type ValueTrait: ?Sized + Pointee<Metadata = DynMetadata<Self::ValueTrait>>;

    const IMPL_VTABLES: &'static VTableRegistry<Self::ValueTrait>;

    // Required method
    fn get_trait_type_id() -> TraitTypeId;
}
Expand description

A trait implemented on all values trait object references that can be used with a value cell (Vc<Box<dyn Trait>>).

You should not create subtraits of this trait manually, but instead use the #[turbo_tasks::value_trait] macro. Implementations of VcValueTraits should use the #[turbo_tasks::value_impl] macro.

§Upcasting

A concrete Vc of a VcValueType can be converted to a Vc of a VcValueTrait with an upcast:

let something_vc: Vc<ConcreteType> = ...;
let trait_vc: Vc<Box<dyn MyTrait>> = Vc::upcast(something_vc);

// there is an equivalent API for ResolvedVc
let something_resolved_vc: ResolvedVc<ConcreteType> = ...;
let trait_resolved_vc: ResolvedVc<Box<dyn MyTrait>> = ResolvedVc::upcast(something_resolved_vc);

Upcast safety is enforced at compile-time with the Upcast and UpcastStrict traits. Upcasts always succeed.

§Downcasting

A ResolvedVc containing a VcValueTrait subtrait can be downcast to a concrete type with ResolvedVc::try_downcast_type:

let trait_vc: Vc<Box<dyn MyTrait>> = ...;
if let Some(something_vc) = ResolvedVc::try_downcast_type::<Something>(trait_vc) {
    // ...
}

A supertrait can be cast to a subtrait with ResolvedVc::try_downcast:

let trait_vc: Vc<Box<dyn SubTrait>> = ...;
if let Some(something_vc) = ResolvedVc::try_downcast::<Box<dyn SuperTrait>>(trait_vc) {
    // ...
}

If you have an unresolved Vc that you’d like to downcast, you should resolve it first.

A compile-time check using the Upcast and UpcastStrict traits ensures that a downcast is possible (the target type or trait implements the source trait), but it may still return None at runtime if the concrete value does not implement the trait.

§Sidecasting

In some cases, you may want to convert between two traits that do not have a supertrait/subtrait relationship:

let trait_vc: Vc<Box<dyn MyTrait>> = ...;
if let Some(something_vc) = ResolvedVc::try_sidecast::<Box<dyn UnrelatedTrait>>(trait_vc) {
    // ...
}

If you have an unresolved Vc that you’d like to sidecast, you should resolve it first.

This won’t do any compile-time checks, so downcasting should be preferred if possible. It will return None at runtime if the cast fails.

§Reading

Trait object Vcs can be read by converting them to a TraitRef, which allows non-turbo-tasks functions defined on the trait to be called.

let trait_vc: Vc<Box<dyn MyTrait>> = ...;
let trait_ref: TraitRef<Box<dyn MyTrait>> = trait_vc.into_trait_ref().await?;

trait_ref.non_turbo_tasks_function();

Required Associated Constants§

Source

const IMPL_VTABLES: &'static VTableRegistry<Self::ValueTrait>

The per-trait vtable registry, populated at program load by #[ctor::ctor] functions emitted by each #[turbo_tasks::value_impl] expansion.

[VTableRegistry::cast] panics if the value type being cast doesn’t implement this trait.

Required Associated Types§

Required Methods§

Source

fn get_trait_type_id() -> TraitTypeId

Returns the type id of the trait object.

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.

Implementations on Foreign Types§

Source§

impl VcValueTrait for Box<dyn ValueDebug>

Source§

impl VcValueTrait for Box<dyn ValueToString>

Source§

impl VcValueTrait for Box<dyn ValueDefault>

Source§

impl VcValueTrait for Box<dyn EffectCollectible>

Source§

const IMPL_VTABLES: &'static VTableRegistry<Self::ValueTrait>

Source§

type ValueTrait = dyn EffectCollectible

Source§

fn get_trait_type_id() -> TraitTypeId

Implementors§