From d79f21c29fb2753245ca09d9a5b74332f5dcfe4f Mon Sep 17 00:00:00 2001 From: Benjamin Woodruff Date: Wed, 8 Jan 2025 16:48:40 -0800 Subject: [PATCH] refactor(turbo-tasks): Add common traits (TaskInput, NonLocalValue, etc) to `either::Either` (#74198) I ended up abandoning https://github.com/vercel/next.js/pull/74108, but the `Either` trait implementations in there seemed nice-to-have, so I'm publishing them here instead. --- Cargo.lock | 1 + turbopack/crates/turbo-tasks/Cargo.toml | 1 + .../crates/turbo-tasks/src/marker_trait.rs | 1 + .../crates/turbo-tasks/src/task/task_input.rs | 24 +++++++++++++++++++ turbopack/crates/turbo-tasks/src/trace.rs | 10 ++++++++ 5 files changed, 37 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 27c7e2581df6b..fe647084ac96d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8420,6 +8420,7 @@ dependencies = [ "auto-hash-map", "concurrent-queue", "dashmap 6.1.0", + "either", "erased-serde", "event-listener 2.5.3", "futures", diff --git a/turbopack/crates/turbo-tasks/Cargo.toml b/turbopack/crates/turbo-tasks/Cargo.toml index 2d5ec14702983..fb521d5fb645c 100644 --- a/turbopack/crates/turbo-tasks/Cargo.toml +++ b/turbopack/crates/turbo-tasks/Cargo.toml @@ -22,6 +22,7 @@ async-trait = { workspace = true } auto-hash-map = { workspace = true } concurrent-queue = { workspace = true } dashmap = { workspace = true } +either = { workspace = true } erased-serde = "0.3.20" event-listener = "2.5.3" futures = { workspace = true } diff --git a/turbopack/crates/turbo-tasks/src/marker_trait.rs b/turbopack/crates/turbo-tasks/src/marker_trait.rs index eaf5e5d8f293f..96bbe78e618f2 100644 --- a/turbopack/crates/turbo-tasks/src/marker_trait.rs +++ b/turbopack/crates/turbo-tasks/src/marker_trait.rs @@ -66,6 +66,7 @@ macro_rules! impl_auto_marker_trait { unsafe impl $trait for ::std::sync::Mutex {} unsafe impl $trait for ::std::cell::RefCell {} unsafe impl $trait for ::std::marker::PhantomData {} + unsafe impl $trait for ::either::Either {} unsafe impl $trait for $crate::TraitRef {} unsafe impl $trait for $crate::ReadRef diff --git a/turbopack/crates/turbo-tasks/src/task/task_input.rs b/turbopack/crates/turbo-tasks/src/task/task_input.rs index bb30ca6341c0a..82cca75349343 100644 --- a/turbopack/crates/turbo-tasks/src/task/task_input.rs +++ b/turbopack/crates/turbo-tasks/src/task/task_input.rs @@ -1,6 +1,7 @@ use std::{any::Any, fmt::Debug, future::Future, hash::Hash, time::Duration}; use anyhow::Result; +use either::Either; use serde::{Deserialize, Serialize}; use turbo_rcstr::RcStr; @@ -220,6 +221,29 @@ impl<'de, T> Deserialize<'de> for TransientInstance { } } +impl TaskInput for Either +where + L: TaskInput, + R: TaskInput, +{ + fn resolve(&self) -> impl Future> + Send + '_ { + self.as_ref().map_either( + |l| async move { anyhow::Ok(Either::Left(l.resolve().await?)) }, + |r| async move { anyhow::Ok(Either::Right(r.resolve().await?)) }, + ) + } + + fn is_resolved(&self) -> bool { + self.as_ref() + .either(TaskInput::is_resolved, TaskInput::is_resolved) + } + + fn is_transient(&self) -> bool { + self.as_ref() + .either(TaskInput::is_transient, TaskInput::is_transient) + } +} + macro_rules! tuple_impls { ( $( $name:ident )+ ) => { impl<$($name: TaskInput),+> TaskInput for ($($name,)+) diff --git a/turbopack/crates/turbo-tasks/src/trace.rs b/turbopack/crates/turbo-tasks/src/trace.rs index 01c6704e708df..0621fcf6696cc 100644 --- a/turbopack/crates/turbo-tasks/src/trace.rs +++ b/turbopack/crates/turbo-tasks/src/trace.rs @@ -9,6 +9,7 @@ use std::{ }; use auto_hash_map::{AutoMap, AutoSet}; +use either::Either; use indexmap::{IndexMap, IndexSet}; use turbo_rcstr::RcStr; @@ -253,4 +254,13 @@ impl TraceRawVcs for &mut T { } } +impl TraceRawVcs for Either { + fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) { + match self { + Either::Left(l) => l.trace_raw_vcs(trace_context), + Either::Right(r) => r.trace_raw_vcs(trace_context), + } + } +} + pub use turbo_tasks_macros::TraceRawVcs;