Skip to main content

turbo_tasks/
lib.rs

1#![doc = include_str!("../README.md")]
2#![feature(trivial_bounds)]
3#![feature(min_specialization)]
4#![deny(unsafe_op_in_unsafe_fn)]
5#![feature(error_generic_member_access)]
6#![feature(arbitrary_self_types)]
7#![feature(arbitrary_self_types_pointers)]
8#![feature(ptr_metadata)]
9#![feature(sync_unsafe_cell)]
10#![feature(async_fn_traits)]
11#![feature(impl_trait_in_assoc_type)]
12#![feature(const_type_name)]
13
14pub mod backend;
15mod capture_future;
16mod collectibles;
17mod completion;
18pub mod debug;
19#[doc = include_str!("../FORMATTING.md")]
20pub mod display;
21pub mod duration_span;
22mod dyn_task_inputs;
23mod effect;
24mod error;
25pub mod event;
26pub mod graph;
27mod id;
28mod id_factory;
29mod invalidation;
30mod join_iter_ext;
31pub mod keyed;
32mod local_task_tracker;
33#[doc(hidden)]
34pub mod macro_helpers;
35mod manager;
36pub mod mapped_read_ref;
37mod marker_trait;
38pub mod message_queue;
39mod native_function;
40mod once_map;
41mod output;
42pub mod panic_hooks;
43pub mod parallel;
44pub mod primitives;
45mod priority_runner;
46mod read_options;
47mod read_ref;
48pub mod registry;
49pub mod scope;
50mod serialization_invalidation;
51pub mod small_duration;
52mod spawn;
53mod state;
54pub mod task;
55#[cfg(feature = "task_dirty_cause")]
56mod task_dirty_cause;
57mod task_execution_reason;
58pub mod task_statistics;
59mod tiny_vec;
60pub mod trace;
61mod trait_ref;
62mod triomphe_utils;
63pub mod util;
64mod value;
65mod value_type;
66mod vc;
67
68use std::hash::BuildHasherDefault;
69
70pub use anyhow::{Error, Result};
71use auto_hash_map::AutoSet;
72use rustc_hash::FxHasher;
73pub use shrink_to_fit::ShrinkToFit;
74pub use turbo_tasks_macros::{DeterministicHash, turbobail, turbofmt};
75
76#[cfg(feature = "task_dirty_cause")]
77pub use crate::task_dirty_cause::TaskDirtyCause;
78pub use crate::{
79    capture_future::TurboTasksPanic,
80    collectibles::CollectiblesSource,
81    completion::{Completion, Completions},
82    display::{ValueToString, ValueToStringRef},
83    dyn_task_inputs::{
84        DynTaskInputs, OwnedStackDynTaskInputs, StackDynTaskInputs, StackDynTaskInputsSlot,
85    },
86    effect::{Effect, EffectError, EffectStateStorage, Effects, emit_effect, take_effects},
87    error::PrettyPrintError,
88    id::{
89        ExecutionId, FunctionId, LocalTaskId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId,
90    },
91    invalidation::{
92        InvalidationReason, InvalidationReasonKind, InvalidationReasonSet, Invalidator,
93        get_invalidator,
94    },
95    join_iter_ext::{JoinIterExt, TryFlatJoinIterExt, TryJoinIterExt},
96    manager::{
97        CurrentCellRef, ReadCellTracking, ReadConsistency, ReadTracking, TaskPersistence,
98        TaskPriority, TurboTasks, TurboTasksApi, TurboTasksCallApi, Unused, UpdateInfo,
99        dynamic_call, emit, get_serialization_invalidator, mark_finished, mark_stateful,
100        mark_top_level_task, prevent_gc, run, run_once, run_once_with_reason, trait_call,
101        turbo_tasks, turbo_tasks_scope, turbo_tasks_weak,
102        unmark_top_level_task_may_leak_eventually_consistent_state, with_turbo_tasks,
103    },
104    mapped_read_ref::MappedReadRef,
105    output::OutputContent,
106    read_options::{ReadCellOptions, ReadOutputOptions},
107    read_ref::ReadRef,
108    serialization_invalidation::SerializationInvalidator,
109    spawn::{JoinHandle, block_for_future, block_in_place, spawn, spawn_blocking, spawn_thread},
110    state::{State, parking_lot_mutex_bincode},
111    task::{
112        SharedReference, TypedSharedReference,
113        task_input::{EitherTaskInput, TaskInput},
114    },
115    task_execution_reason::TaskExecutionReason,
116    tiny_vec::TinyVec,
117    trait_ref::TraitRef,
118    value::{TransientInstance, TransientValue},
119    value_type::{Evictability, TraitMethod, TraitType, ValueType, ValueTypePersistence},
120    vc::{
121        CellId, Dynamic, NonLocalValue, OperationValue, OperationVc, OptionVcExt, RawVc,
122        ReadRawVcFuture, ReadVcFuture, ResolveOperationVcFuture, ResolveRawVcFuture,
123        ResolveVcFuture, ResolvedVc, ToResolvedVcFuture, Upcast, UpcastStrict, ValueDefault, Vc,
124        VcCast, VcCellCompareMode, VcCellHashedCompareMode, VcCellKeyedCompareMode, VcCellNewMode,
125        VcDefaultRead, VcRead, VcTransparentRead, VcValueTrait, VcValueTraitCast, VcValueType,
126        VcValueTypeCast,
127    },
128};
129
130pub type FxIndexSet<T> = indexmap::IndexSet<T, BuildHasherDefault<FxHasher>>;
131pub type FxIndexMap<K, V> = indexmap::IndexMap<K, V, BuildHasherDefault<FxHasher>>;
132pub type FxDashMap<K, V> = dashmap::DashMap<K, V, BuildHasherDefault<FxHasher>>;
133
134// Copied from indexmap! and indexset!
135#[macro_export]
136macro_rules! fxindexmap {
137    (@single $($x:tt)*) => (());
138    (@count $($rest:expr),*) => (<[()]>::len(&[$($crate::fxindexmap!(@single $rest)),*]));
139
140    ($($key:expr => $value:expr,)+) => { $crate::fxindexmap!($($key => $value),+) };
141    ($($key:expr => $value:expr),*) => {
142        {
143            let _cap = $crate::fxindexmap!(@count $($key),*);
144            let mut _map = $crate::FxIndexMap::with_capacity_and_hasher(_cap, Default::default());
145            $(
146                _map.insert($key, $value);
147            )*
148            _map
149        }
150    };
151}
152#[macro_export]
153macro_rules! fxindexset {
154    (@single $($x:tt)*) => (());
155    (@count $($rest:expr),*) => (<[()]>::len(&[$($crate::fxindexset!(@single $rest)),*]));
156
157    ($($value:expr,)+) => { $crate::fxindexset!($($value),+) };
158    ($($value:expr),*) => {
159        {
160            let _cap = $crate::fxindexset!(@count $($value),*);
161            let mut _set = $crate::FxIndexSet::with_capacity_and_hasher(_cap, Default::default());
162            $(
163                _set.insert($value);
164            )*
165            _set
166        }
167    };
168}
169
170#[doc = include_str!("../singleton_pattern.md")]
171pub mod _singleton_pattern {}
172
173#[doc = include_str!("../function.md")]
174#[rustfmt::skip]
175pub use turbo_tasks_macros::function;
176
177/// Implements [`VcValueType`] for the given `struct` or `enum`. These value types can be used
178/// inside of a "value cell" as [`Vc<...>`][Vc].
179///
180/// A [`Vc`] represents the result of a computation. Each [`Vc`]'s value is placed into a cell
181/// associated with the current [`TaskId`]. That [`Vc`] object can be `await`ed to get [a read-only
182/// reference to the value contained in the cell][ReadRef].
183///
184/// This macro accepts multiple comma-separated arguments. For example:
185///
186/// ```
187/// # #![feature(arbitrary_self_types)]
188//  # #![feature(arbitrary_self_types_pointers)]
189/// #[turbo_tasks::value(transparent, shared)]
190/// struct Foo(Vec<u32>);
191/// ```
192///
193/// ## `cell = "..."`
194///
195/// Controls when a cell is invalidated upon recomputation of a task. Internally, this is performed
196/// by setting the [`VcValueType::CellMode`] associated type.
197///
198/// - **`"new"`:** Always overrides the value in the cell, invalidating all dependent tasks.
199/// - **`"compare"` *(default)*:** Compares with the existing value in the cell, before overriding it.
200///   Requires the value to implement [`Eq`].
201/// - **`"keyed"`:** Like `"compare"`, but uses per-key invalidation for transparent map types.
202///
203/// Avoiding unnecessary invalidation is important to reduce downstream recomputation of tasks that
204/// depend on this cell's value.
205///
206/// Use `"new"` only if a correct implementation of [`Eq`] is not possible, would be expensive (e.g.
207/// would require comparing a large collection), or if you're implementing a low-level primitive
208/// that intentionally forces recomputation.
209///
210/// ## `eq = "..."`
211///
212/// By default, we `#[derive(PartialEq, Eq)]`. [`Eq`] is required by `cell = "compare"`. This
213/// argument allows overriding that default implementation behavior.
214///
215/// - **`"manual"`:** Prevents deriving [`Eq`] and [`PartialEq`] so you can do it manually.
216///
217/// ## `serialization = "..."`
218///
219/// Affects serialization via [`bincode::Encode`] and [`bincode::Decode`]. Serialization is required
220/// for the filesystem cache of tasks.
221///
222/// - **`"auto"` *(default)*:** Derives the bincode traits and enables serialization.
223/// - **`"custom"`:** Prevents deriving the bincode traits, but still enables serialization
224///   (you must manually implement [`bincode::Encode`] and [`bincode::Decode`]).
225/// - **`"hash"`:** Like `"none"` (no bincode serialization), but instead stores a hash of the cell
226///   value so that changes can be detected even when the transient cell data has been evicted
227///   from memory or was never stored in the cache—avoiding unnecessary downstream invalidation.
228///   Only valid with `cell = "compare"`.
229///   Requires the value to implement both [`Eq`] and [`DeterministicHash`][turbo_tasks_hash::DeterministicHash].
230/// - **`"none"`:** Disables serialization and prevents deriving the traits.
231///
232/// ## `hash = "..."`
233///
234/// By default, when using `serialization = "hash"`, we `#[derive(DeterministicHash)]`. This argument allows
235/// overriding that default implementation behavior.
236///
237/// - **`"manual"`:** Prevents deriving [`DeterministicHash`][turbo_tasks_hash::DeterministicHash] so you can do it manually.
238///   Only valid with `serialization = "hash"`.
239///
240/// ## `shared`
241///
242/// This flag makes the macro-generated `.cell()` method public so everyone can use it.
243///
244/// Non-transparent types are given a `.cell()` method. That method returns a `Vc` of the type.
245///
246/// This option does not apply to wrapper types that use `transparent`. Those use the public
247/// [`Vc::cell`] function for construction.
248///
249/// ## `transparent`
250///
251/// This attribute is only valid on single-element unit structs. When this value is set:
252///
253/// 1. The struct will use [`#[repr(transparent)]`][repr-transparent].
254/// 1. Read operations (`vc.await?`) return a [`ReadRef`] containing the inner type, rather than the
255///    outer struct. Internally, this is accomplished using [`VcTransparentRead`] for the
256///    [`VcValueType::Read`] associated type.
257/// 1. Construction of the type must be performed using [`Vc::cell(inner)`][Vc::cell], rather than
258///    using the `.cell()` method on the outer type (`outer.cell()`).
259/// 1. The [`ValueDebug`][crate::debug::ValueDebug] implementation will defer to the inner type.
260///
261/// This is commonly used to create [`VcValueType`] wrappers for foreign or generic types, such as
262/// [`Vec`] or [`Option`].
263///
264/// [repr-transparent]: https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent
265///
266/// ## `local`
267///
268/// Skip the implementation of [`NonLocalValue`] for this type.
269///
270/// If not specified, we apply the [`#[derive(NonLocalValue)]`][macro@NonLocalValue] macro, which
271/// asserts that this struct has no fields containing [`Vc`] by implementing the [`NonLocalValue`]
272/// marker trait. Compile-time assertions are generated on every field, checking that they are also
273/// [`NonLocalValue`]s.
274#[rustfmt::skip]
275pub use turbo_tasks_macros::value;
276
277/// Allows this trait to be used as part of a trait object inside of a value cell, in the form of
278/// `Vc<Box<dyn MyTrait>>`. The annotated trait is made into a subtrait of [`VcValueTrait`].
279///
280/// ```ignore
281/// #[turbo_tasks::value_trait]
282/// pub trait MyTrait {
283///
284///     #[turbo_tasks::function]
285///     fn method(self: Vc<Self>, a: i32) -> Vc<Something>;
286///
287///     // External signature: fn method(self: Vc<Self>, a: i32) -> Vc<Something>
288///     #[turbo_tasks::function]
289///     async fn method2(&self, a: i32) -> Result<Vc<Something>> {
290///         // Default implementation
291///     }
292///
293///     // A normal trait item, not a turbo-task
294///     fn normal(&self) -> SomethingElse;
295/// }
296///
297/// #[turbo_tasks::value_trait]
298/// pub trait OtherTrait: MyTrait + ValueToString {
299///     // ...
300/// }
301///
302/// #[turbo_tasks::value_impl]
303/// impl MyTrait for MyValue {
304///     // only the external signature must match (see the docs for #[turbo_tasks::function])
305///     #[turbo_tasks::function]
306///     fn method(&self, a: i32) -> Vc<Something> {
307///         todo!()
308///     }
309///
310///     fn normal(&self) -> SomethingElse {
311///         todo!()
312///     }
313/// }
314/// ```
315///
316/// The `#[turbo_tasks::value_trait]` annotation derives [`VcValueTrait`] and registers the trait
317/// and its methods.
318///
319/// All methods annotated with [`#[turbo_tasks::function]`][function] are cached, and
320/// the external signature rewriting rules defined on that macro are applied.
321///
322/// Default implementation are supported.
323///
324/// ## Arguments
325///
326/// Example: `#[turbo_tasks::value_trait(no_debug, operation)]`
327///
328/// ### `no_debug`
329///
330/// Disables the automatic implementation of [`ValueDebug`][debug::ValueDebug].
331///
332/// Example: `#[turbo_tasks::value_trait(no_debug)]`
333///
334/// ### `Operation`
335///
336/// Adds [`OperationValue`] as a supertrait of this trait.
337///
338/// Example: `#[turbo_tasks::value_trait(operation)]`
339#[rustfmt::skip]
340pub use turbo_tasks_macros::value_trait;
341
342/// A macro used on any `impl` block for a [`VcValueType`]. This can either be an inherent
343/// implementation or a trait implementation (see [`turbo_tasks::value_trait`][value_trait] and
344/// [`VcValueTrait`]).
345///
346/// Methods should be annotated with the [`#[turbo_tasks::function]`][function] macro.
347///
348/// ```ignore
349/// #[turbo_tasks::value_impl]
350/// impl MyTrait for MyValue {
351///     #[turbo_tasks::function]
352///     fn method(&self, a: i32) -> Vc<Something> {
353///         todo!()
354///     }
355/// }
356/// ```
357#[rustfmt::skip]
358pub use turbo_tasks_macros::value_impl;
359
360/// Derives the TaskStorage struct and generates optimized storage structures.
361///
362/// This macro analyzes `field` annotations and generates:
363/// 1. A unified TaskStorage struct
364/// 2. LazyField enum for lazy_vec fields
365/// 3. Typed accessor methods on TaskStorage
366/// 4. TaskStorageAccessors trait with accessor methods
367/// 5. TaskFlags bitfield for boolean flags
368///
369/// # Field Attributes
370///
371/// All fields require two attributes:
372///
373/// ## `storage = "..."` (required)
374///
375/// Specifies how the field is stored:
376/// - `direct` - Direct field access (e.g., `Option<OutputValue>`)
377/// - `auto_set` - Uses AutoSet for small collections
378/// - `auto_map` - Uses AutoMap for key-value pairs
379/// - `counter_map` - Uses CounterMap for reference counting
380/// - `flag` - Boolean flag stored in a compact TaskFlags bitfield (field type must be `bool`)
381///
382/// ## `category = "..."` (required)
383///
384/// Specifies the data category for persistence and access:
385/// - `data` - Frequently changed, bulk I/O
386/// - `meta` - Rarely changed, small I/O
387/// - `transient` - Field is not serialized (in-memory only)
388///
389/// ## Optional Modifiers
390///
391/// - `inline` - Field is stored inline on TaskStorage (default is lazy). Only use for hot-path
392///   fields that are frequently accessed.
393/// - `default` - Use `Default::default()` semantics instead of `Option` for inline direct fields.
394/// - `filter_transient` - Filter out transient values during serialization.
395/// - Serialization methods
396#[rustfmt::skip]
397pub use turbo_tasks_macros::task_storage;
398
399/// Refer to [the trait documentation][trait@TaskInput] for usage.
400#[rustfmt::skip]
401pub use turbo_tasks_macros::TaskInput;
402
403pub type TaskIdSet = AutoSet<TaskId, BuildHasherDefault<FxHasher>, 2>;