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