turbo_tasks/vc/
traits.rs

1use crate::{
2    NonLocalValue, ShrinkToFit, TraitTypeId, ValueTypeId, VcRead, macro_helpers::VTableRegistry,
3    vc::cell_mode::VcCellMode,
4};
5
6/// A trait implemented on all values types that can be put into a Value Cell
7/// ([`Vc<T>`][crate::Vc]).
8///
9/// # Safety
10///
11/// The implementor of this trait must ensure that the read and cell mode
12/// implementations are correct for the value type. Otherwise, it is possible to
13/// generate invalid reads, for instance by using
14/// [`VcTransparentRead`][crate::VcTransparentRead] for a value type that is not
15/// `#[repr(transparent)]`.
16pub unsafe trait VcValueType: ShrinkToFit + Sized + Send + Sync + 'static {
17    /// How to read the value.
18    type Read: VcRead<Self>;
19
20    /// How to update cells of this value type.
21    type CellMode: VcCellMode<Self>;
22
23    /// Returns the type id of the value type.
24    fn get_value_type_id() -> ValueTypeId;
25}
26
27/// A trait implemented on all values trait object references that can be put
28/// into a Value Cell ([`Vc<Box<dyn Trait>>`][crate::Vc]).
29pub trait VcValueTrait: NonLocalValue + Send + Sync + 'static {
30    // The concrete type of the value_trait implementing VcValueTrait
31    type ValueTrait: ?Sized;
32
33    /// Returns the type id of the trait object.
34    fn get_trait_type_id() -> TraitTypeId;
35
36    /// Returns the vtable for an implementation of this trait.
37    /// Panics if ValueTypeId does not implement the trait.
38    fn get_impl_vtables() -> &'static VTableRegistry<Self::ValueTrait>;
39}
40
41/// Marker trait that indicates that a [`Vc<Self>`][crate::Vc] can be upcasted
42/// to a [`Vc<T>`][crate::Vc].
43///
44/// # Safety
45///
46/// The implementor of this trait must ensure that `Self` implements the
47/// trait `T`.
48pub unsafe trait Upcast<T>
49where
50    T: VcValueTrait + ?Sized,
51{
52}
53
54/// Marker trait that indicates that a [`Vc<Self>`][crate::Vc] can accept all
55/// methods declared on a [`Vc<T>`][crate::Vc].
56///
57/// # Safety
58///
59/// The implementor of this trait must ensure that `Self` implements the
60/// trait `T`.
61pub unsafe trait Dynamic<T>
62where
63    T: VcValueTrait + ?Sized,
64{
65}