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 fn has_serialization() -> bool;
27}
28
29/// A trait implemented on all values trait object references that can be put
30/// into a Value Cell ([`Vc<Box<dyn Trait>>`][crate::Vc]).
31pub trait VcValueTrait: NonLocalValue + Send + Sync + 'static {
32 // The concrete type of the value_trait implementing VcValueTrait
33 type ValueTrait: ?Sized;
34
35 /// Returns the type id of the trait object.
36 fn get_trait_type_id() -> TraitTypeId;
37
38 /// Returns the vtable for an implementation of this trait.
39 /// Panics if ValueTypeId does not implement the trait.
40 fn get_impl_vtables() -> &'static VTableRegistry<Self::ValueTrait>;
41}
42
43/// Marker trait that indicates that a [`Vc<Self>`][crate::Vc] can be upcasted
44/// to a [`Vc<T>`][crate::Vc].
45///
46/// # Safety
47///
48/// The implementor of this trait must ensure that `Self` implements the
49/// trait `T`.
50pub unsafe trait Upcast<T>
51where
52 T: VcValueTrait + ?Sized,
53{
54}
55
56/// A speialization of [`Upcast`] that ensures that the upcast is strict meaning that T !== Self
57///
58/// # Safety
59///
60/// The implementor of this trait must ensure that `Self` implements the
61/// trait `T` and that `Self` is not equal to `T`.
62pub unsafe trait UpcastStrict<T>: Upcast<T>
63where
64 T: VcValueTrait + ?Sized,
65{
66}
67
68/// Marker trait that indicates that a [`Vc<Self>`][crate::Vc] can accept all
69/// methods declared on a [`Vc<T>`][crate::Vc].
70///
71/// # Safety
72///
73/// The implementor of this trait must ensure that `Self` implements the
74/// trait `T`.
75pub unsafe trait Dynamic<T>
76where
77 T: VcValueTrait + ?Sized,
78{
79}