turbo_tasks/vc/
traits.rs

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