Skip to content

Commit

Permalink
refactor(turbo-tasks): Add common traits (TaskInput, NonLocalValue, e…
Browse files Browse the repository at this point in the history
…tc) to `either::Either<L, R>` (vercel#74198)

I ended up abandoning vercel#74108, but the `Either` trait implementations in there seemed nice-to-have, so I'm publishing them here instead.
  • Loading branch information
bgw authored Jan 9, 2025
1 parent 5487a03 commit d79f21c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions turbopack/crates/turbo-tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions turbopack/crates/turbo-tasks/src/marker_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ macro_rules! impl_auto_marker_trait {
unsafe impl<T: $trait + ?Sized> $trait for ::std::sync::Mutex<T> {}
unsafe impl<T: $trait + ?Sized> $trait for ::std::cell::RefCell<T> {}
unsafe impl<T: ?Sized> $trait for ::std::marker::PhantomData<T> {}
unsafe impl<L: $trait, R: $trait> $trait for ::either::Either<L, R> {}

unsafe impl<T: $trait + ?Sized> $trait for $crate::TraitRef<T> {}
unsafe impl<T> $trait for $crate::ReadRef<T>
Expand Down
24 changes: 24 additions & 0 deletions turbopack/crates/turbo-tasks/src/task/task_input.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -220,6 +221,29 @@ impl<'de, T> Deserialize<'de> for TransientInstance<T> {
}
}

impl<L, R> TaskInput for Either<L, R>
where
L: TaskInput,
R: TaskInput,
{
fn resolve(&self) -> impl Future<Output = Result<Self>> + 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,)+)
Expand Down
10 changes: 10 additions & 0 deletions turbopack/crates/turbo-tasks/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
};

use auto_hash_map::{AutoMap, AutoSet};
use either::Either;
use indexmap::{IndexMap, IndexSet};
use turbo_rcstr::RcStr;

Expand Down Expand Up @@ -253,4 +254,13 @@ impl<T: TraceRawVcs + ?Sized> TraceRawVcs for &mut T {
}
}

impl<L: TraceRawVcs, R: TraceRawVcs> TraceRawVcs for Either<L, R> {
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;

0 comments on commit d79f21c

Please sign in to comment.