Skip to content

Commit

Permalink
refactor(turbo-tasks): Add a way to directly construct a ResolvedVc
Browse files Browse the repository at this point in the history
  • Loading branch information
bgw committed Sep 12, 2024
1 parent c011bc9 commit 15f3399
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
9 changes: 9 additions & 0 deletions turbopack/crates/turbo-tasks-macros/src/value_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,15 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream {
turbo_tasks::Vc::cell_private(#cell_access_content)
}

/// Places a value in a cell of the current task. Returns a
/// [`ResolvedVc`][turbo_tasks::ResolvedVc].
///
/// Cell is selected based on the value type and call order of `cell`.
#cell_prefix fn resolved_cell(self) -> turbo_tasks::ResolvedVc<Self> {
let content = self;
turbo_tasks::ResolvedVc::cell_private(#cell_access_content)
}

/// Places a value in a task-local cell stored in the current task.
///
/// Task-local cells are stored in a task-local arena, and do not persist outside the
Expand Down
15 changes: 15 additions & 0 deletions turbopack/crates/turbo-tasks-testing/tests/resolved_vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use turbo_tasks_testing::{register, run, Registration};

static REGISTRATION: Registration = register!();

#[turbo_tasks::value]
struct Wrapper(u32);

#[tokio::test]
async fn test_conversion() -> Result<()> {
run(&REGISTRATION, || async {
Expand All @@ -20,3 +23,15 @@ async fn test_conversion() -> Result<()> {
})
.await
}

#[tokio::test]
async fn test_cell_construction() -> Result<()> {
run(&REGISTRATION, || async {
let a: ResolvedVc<u32> = ResolvedVc::cell(42);
assert_eq!(*a.await?, 42);
let b: ResolvedVc<Wrapper> = Wrapper(42).resolved_cell();
assert_eq!(b.await?.0, 42);
Ok(())
})
.await
}
29 changes: 28 additions & 1 deletion turbopack/crates/turbo-tasks/src/vc/resolved.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
any::Any,
cell::RefCell,
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
future::IntoFuture,
Expand All @@ -23,7 +24,7 @@ use serde::{Deserialize, Serialize};
use crate::{
trace::{TraceRawVcs, TraceRawVcsContext},
vc::Vc,
RcStr, VcValueType,
RcStr, VcRead, VcTransparentRead, VcValueType,
};

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -95,6 +96,32 @@ macro_rules! into_future {
into_future!(ResolvedVc<T>);
into_future!(&ResolvedVc<T>);

impl<T> ResolvedVc<T>
where
T: VcValueType,
{
// called by the `.resolved_cell()` method generated by the `#[turbo_tasks::value]` macro
#[doc(hidden)]
pub fn cell_private(inner: <T::Read as VcRead<T>>::Target) -> Self {
Self {
node: Vc::<T>::cell_private(inner),
}
}
}

impl<T, Inner, Repr> ResolvedVc<T>
where
T: VcValueType<Read = VcTransparentRead<T, Inner, Repr>>,
Inner: Any + Send + Sync,
Repr: VcValueType,
{
pub fn cell(inner: Inner) -> Self {
Self {
node: Vc::<T>::cell(inner),
}
}
}

impl<T> std::fmt::Debug for ResolvedVc<T>
where
T: Send,
Expand Down

0 comments on commit 15f3399

Please sign in to comment.