turbo_tasks/
completion.rs

1use anyhow::Result;
2
3use crate::{self as turbo_tasks, RawVc, ResolvedVc, TryJoinIterExt, Vc};
4
5/// Just an empty type, but it's never equal to itself.
6///
7/// [`Vc<Completion>`] can be used as return value instead of `()` to have a concrete reference that
8/// can be awaited. It will invalidate the awaiting task everytime the referenced task has been
9/// executed.
10///
11/// Note: [`PartialEq`] is not implemented since it doesn't make sense to compare `Completion` this
12/// way. You probably want to use [`ReadRef::ptr_eq`][crate::ReadRef::ptr_eq] instead.
13#[turbo_tasks::value(cell = "new", eq = "manual")]
14#[derive(Debug)]
15pub struct Completion;
16
17#[turbo_tasks::value_impl]
18impl Completion {
19    /// This will always be the same and never invalidates the reading task.
20    #[turbo_tasks::function]
21    pub fn immutable() -> Vc<Self> {
22        Completion::cell(Completion)
23    }
24}
25
26// no #[turbo_tasks::value_impl] to inline new into the caller task
27// this ensures it's re-created on each execution
28impl Completion {
29    /// This will always be a new completion and invalidates the reading task.
30    pub fn new() -> Vc<Self> {
31        Completion::cell(Completion)
32    }
33
34    /// Uses the previous completion. Can be used to cancel without triggering a
35    /// new invalidation.
36    pub fn unchanged() -> Vc<Self> {
37        // This is the same code that Completion::cell uses except that it
38        // only updates the cell when it is empty (Completion::cell opted-out of
39        // that via `#[turbo_tasks::value(cell = "new")]`)
40        let cell = turbo_tasks::macro_helpers::find_cell_by_type(*COMPLETION_VALUE_TYPE_ID);
41        cell.conditional_update(|old| old.is_none().then_some(Completion));
42        let raw: RawVc = cell.into();
43        raw.into()
44    }
45}
46
47#[turbo_tasks::value(transparent)]
48pub struct Completions(Vec<ResolvedVc<Completion>>);
49
50#[turbo_tasks::value_impl]
51impl Completions {
52    /// Merges multiple completions into one. The passed list will be part of
53    /// the cache key, so this function should not be used with varying lists.
54    ///
55    /// Varying lists should use `Vc::cell(list).completed()`
56    /// instead.
57    #[turbo_tasks::function]
58    pub fn all(completions: Vec<ResolvedVc<Completion>>) -> Vc<Completion> {
59        Vc::<Completions>::cell(completions).completed()
60    }
61
62    /// Merges the list of completions into one.
63    #[turbo_tasks::function]
64    pub async fn completed(&self) -> anyhow::Result<Vc<Completion>> {
65        if self.0.len() > 100 {
66            let mid = self.0.len() / 2;
67            let (left, right) = self.0.split_at(mid);
68            let left = Vc::<Completions>::cell(left.to_vec());
69            let right = Vc::<Completions>::cell(right.to_vec());
70            let left = left.completed();
71            let right = right.completed();
72            left.await?;
73            right.await?;
74            Ok(Completion::new())
75        } else {
76            self.0
77                .iter()
78                .map(|&c| async move {
79                    // Wraps the completion in a new completion. This makes it cheaper to restore
80                    // since it doesn't need to restore the original task resp task chain.
81                    wrap(*c).await?;
82                    Ok(())
83                })
84                .try_join()
85                .await?;
86            Ok(Completion::new())
87        }
88    }
89}
90
91#[turbo_tasks::function]
92pub async fn wrap(completion: Vc<Completion>) -> Result<Vc<Completion>> {
93    completion.await?;
94    Ok(Completion::new())
95}