Skip to content

Commit ecb9fb4

Browse files
authored
Allow tuples as TaskInputs (vercel/turborepo#3692)
(I would have created a Linear issue for this specifically but Linear is down) This PR allows the use of `(A, B, ...)` as `TaskInput`s. This means that the following are now valid `turbo_tasks::function` signatures: ```rust fn do_the_thing(path: (FileSystemPathVc, BoolVc)) -> SomeVc; fn parse_headers(headers: Vec<(StringVc, StringVc)>) -> HeadersVc; ```
1 parent 84b3070 commit ecb9fb4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

crates/turbo-tasks/src/task_input.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,3 +860,49 @@ impl TryFrom<&TaskInput> for RawVc {
860860
}
861861
}
862862
}
863+
864+
macro_rules! tuple_impls {
865+
( $( $name:ident )+ ) => {
866+
impl<$($name: Into<TaskInput>),+> From<($($name,)+)> for TaskInput {
867+
#[allow(non_snake_case)]
868+
fn from(s: ($($name,)+)) -> Self {
869+
let ($($name,)+) = s;
870+
let ($($name,)+) = ($($name.into(),)+);
871+
TaskInput::List(vec![ $($name,)+ ])
872+
}
873+
}
874+
875+
impl<'a, $($name: FromTaskInput<'a, Error = anyhow::Error>,)+> FromTaskInput<'a> for ($($name,)+) {
876+
type Error = anyhow::Error;
877+
878+
#[allow(non_snake_case)]
879+
fn try_from(value: &'a TaskInput) -> Result<Self, Self::Error> {
880+
match value {
881+
TaskInput::List(value) => {
882+
let mut iter = value.iter();
883+
$(
884+
let $name = iter.next().ok_or_else(|| anyhow!("missing tuple element"))?;
885+
let $name = FromTaskInput::try_from($name)?;
886+
)+
887+
Ok(($($name,)+))
888+
},
889+
_ => Err(anyhow!("invalid task input type, expected list")),
890+
}
891+
}
892+
}
893+
};
894+
895+
}
896+
897+
tuple_impls! { A }
898+
tuple_impls! { A B }
899+
tuple_impls! { A B C }
900+
tuple_impls! { A B C D }
901+
tuple_impls! { A B C D E }
902+
tuple_impls! { A B C D E F }
903+
tuple_impls! { A B C D E F G }
904+
tuple_impls! { A B C D E F G H }
905+
tuple_impls! { A B C D E F G H I }
906+
tuple_impls! { A B C D E F G H I J }
907+
tuple_impls! { A B C D E F G H I J K }
908+
tuple_impls! { A B C D E F G H I J K L }

0 commit comments

Comments
 (0)