turbo_tasks/task/
from_task_input.rs

1use crate::{ResolvedVc, TaskInput, Vc};
2
3// NOTE: If you add new implementations of this trait, you'll need to modify
4// `expand_task_input_type` in `turbo-tasks-macros/src/func.rs`.
5pub trait FromTaskInput: private::Sealed {
6    type TaskInput: TaskInput;
7    fn from_task_input(from: Self::TaskInput) -> Self;
8}
9
10mod private {
11    use super::*;
12    /// Implements the sealed trait pattern:
13    /// <https://rust-lang.github.io/api-guidelines/future-proofing.html>
14    pub trait Sealed {}
15    impl<T> Sealed for ResolvedVc<T> where T: ?Sized {}
16    impl<T> Sealed for Vec<T> where T: FromTaskInput {}
17    impl<T> Sealed for Option<T> where T: FromTaskInput {}
18}
19
20impl<T> FromTaskInput for ResolvedVc<T>
21where
22    T: Send + Sync + ?Sized,
23{
24    type TaskInput = Vc<T>;
25    fn from_task_input(from: Vc<T>) -> ResolvedVc<T> {
26        debug_assert!(
27            from.is_resolved(),
28            "Outer `Vc`s are always resolved before this is called"
29        );
30        ResolvedVc { node: from }
31    }
32}
33
34impl<T> FromTaskInput for Vec<T>
35where
36    T: FromTaskInput,
37{
38    type TaskInput = Vec<T::TaskInput>;
39    fn from_task_input(from: Vec<T::TaskInput>) -> Vec<T> {
40        let mut converted = Vec::with_capacity(from.len());
41        for value in from {
42            converted.push(T::from_task_input(value));
43        }
44        converted
45    }
46}
47
48impl<T> FromTaskInput for Option<T>
49where
50    T: FromTaskInput,
51{
52    type TaskInput = Option<T::TaskInput>;
53    fn from_task_input(from: Option<T::TaskInput>) -> Option<T> {
54        from.map(T::from_task_input)
55    }
56}