Unverified Commit d79f21c2 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

refactor(turbo-tasks): Add common traits (TaskInput, NonLocalValue, etc) to…

refactor(turbo-tasks): Add common traits (TaskInput, NonLocalValue, etc) to `either::Either<L, R>` (#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.
parent 5487a038
...@@ -8420,6 +8420,7 @@ dependencies = [ ...@@ -8420,6 +8420,7 @@ dependencies = [
"auto-hash-map", "auto-hash-map",
"concurrent-queue", "concurrent-queue",
"dashmap 6.1.0", "dashmap 6.1.0",
"either",
"erased-serde", "erased-serde",
"event-listener 2.5.3", "event-listener 2.5.3",
"futures", "futures",
......
...@@ -22,6 +22,7 @@ async-trait = { workspace = true } ...@@ -22,6 +22,7 @@ async-trait = { workspace = true }
auto-hash-map = { workspace = true } auto-hash-map = { workspace = true }
concurrent-queue = { workspace = true } concurrent-queue = { workspace = true }
dashmap = { workspace = true } dashmap = { workspace = true }
either = { workspace = true }
erased-serde = "0.3.20" erased-serde = "0.3.20"
event-listener = "2.5.3" event-listener = "2.5.3"
futures = { workspace = true } futures = { workspace = true }
......
...@@ -66,6 +66,7 @@ macro_rules! impl_auto_marker_trait { ...@@ -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::sync::Mutex<T> {}
unsafe impl<T: $trait + ?Sized> $trait for ::std::cell::RefCell<T> {} unsafe impl<T: $trait + ?Sized> $trait for ::std::cell::RefCell<T> {}
unsafe impl<T: ?Sized> $trait for ::std::marker::PhantomData<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 + ?Sized> $trait for $crate::TraitRef<T> {}
unsafe impl<T> $trait for $crate::ReadRef<T> unsafe impl<T> $trait for $crate::ReadRef<T>
......
use std::{any::Any, fmt::Debug, future::Future, hash::Hash, time::Duration}; use std::{any::Any, fmt::Debug, future::Future, hash::Hash, time::Duration};
use anyhow::Result; use anyhow::Result;
use either::Either;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
...@@ -220,6 +221,29 @@ impl<'de, T> Deserialize<'de> for TransientInstance<T> { ...@@ -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 { macro_rules! tuple_impls {
( $( $name:ident )+ ) => { ( $( $name:ident )+ ) => {
impl<$($name: TaskInput),+> TaskInput for ($($name,)+) impl<$($name: TaskInput),+> TaskInput for ($($name,)+)
......
...@@ -9,6 +9,7 @@ use std::{ ...@@ -9,6 +9,7 @@ use std::{
}; };
use auto_hash_map::{AutoMap, AutoSet}; use auto_hash_map::{AutoMap, AutoSet};
use either::Either;
use indexmap::{IndexMap, IndexSet}; use indexmap::{IndexMap, IndexSet};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
...@@ -253,4 +254,13 @@ impl<T: TraceRawVcs + ?Sized> TraceRawVcs for &mut T { ...@@ -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; pub use turbo_tasks_macros::TraceRawVcs;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment