pub trait VcValueTrait:
NonLocalValue
+ Send
+ Sync
+ 'static {
type ValueTrait: ?Sized;
// Required methods
fn get_trait_type_id() -> TraitTypeId;
fn get_impl_vtables() -> &'static VTableRegistry<Self::ValueTrait>;
}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 Types§
type ValueTrait: ?Sized
Required Methods§
Sourcefn get_trait_type_id() -> TraitTypeId
fn get_trait_type_id() -> TraitTypeId
Returns the type id of the trait object.
Sourcefn get_impl_vtables() -> &'static VTableRegistry<Self::ValueTrait>
fn get_impl_vtables() -> &'static VTableRegistry<Self::ValueTrait>
Returns the vtable for an implementation of this trait. Panics if ValueTypeId does not implement the trait.
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.