1pub(crate) mod cast;
2mod cell_mode;
3pub(crate) mod default;
4mod keyed_read_ref;
5mod lazy_read_ref;
6mod local;
7pub(crate) mod operation;
8mod ord;
9mod raw;
10mod read;
11pub(crate) mod resolved;
12mod traits;
13
14use std::{
15 any::Any,
16 fmt::Debug,
17 future::Future,
18 hash::{Hash, Hasher},
19 marker::PhantomData,
20 ops::Deref,
21 pin::Pin,
22 task::{Context, Poll},
23};
24
25use anyhow::Result;
26use bincode::{Decode, Encode};
27use serde::{Deserialize, Serialize};
28use shrink_to_fit::ShrinkToFit;
29
30#[cfg(debug_assertions)]
31use crate::debug::{ValueDebug, ValueDebugFormat, ValueDebugFormatString};
32pub use crate::vc::{
33 cast::{VcCast, VcValueTraitCast, VcValueTypeCast},
34 cell_mode::{
35 VcCellCompareMode, VcCellHashedCompareMode, VcCellKeyedCompareMode, VcCellMode,
36 VcCellNewMode,
37 },
38 default::ValueDefault,
39 keyed_read_ref::KeyedReadRef,
40 lazy_read_ref::LazyReadRef,
41 local::NonLocalValue,
42 operation::{OperationValue, OperationVc, ResolveOperationVcFuture},
43 ord::OrdResolvedVc,
44 raw::{CellId, RawVc, RawVcUnpacked, ReadRawVcFuture, ResolveRawVcFuture},
45 read::{
46 ReadKeyedReaderVcFuture, ReadLazyReaderVcFuture, ReadOwnedVcFuture, ReadVcFuture,
47 VcDefaultRead, VcRead, VcTransparentRead,
48 },
49 resolved::ResolvedVc,
50 traits::{Dynamic, Upcast, UpcastStrict, VcValueTrait, VcValueType},
51};
52use crate::{
53 keyed::{KeyedAccess, KeyedEq},
54 registry,
55 vc::read::{ReadContainsKeyedVcFuture, ReadKeyedVcFuture},
56};
57
58#[must_use]
64pub struct ResolveVcFuture<T>
65where
66 T: ?Sized,
67{
68 pub(crate) inner: ResolveRawVcFuture,
69 pub(crate) _t: PhantomData<T>,
70}
71
72impl<T: ?Sized> Future for ResolveVcFuture<T> {
73 type Output = Result<Vc<T>>;
74
75 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
76 let this = unsafe { self.get_unchecked_mut() };
78 Pin::new(&mut this.inner).poll(cx).map(|r| {
80 r.map(|node| Vc {
81 node,
82 _t: PhantomData,
83 })
84 })
85 }
86}
87
88impl<T: ?Sized> Unpin for ResolveVcFuture<T> {}
89
90#[must_use]
94pub struct ToResolvedVcFuture<T>
95where
96 T: ?Sized,
97{
98 inner: ResolveRawVcFuture,
99 _t: PhantomData<T>,
100}
101
102impl<T: ?Sized> ToResolvedVcFuture<T> {
103 pub fn strongly_consistent(mut self) -> Self {
105 self.inner = self.inner.strongly_consistent();
106 self
107 }
108}
109
110impl<T: ?Sized> Future for ToResolvedVcFuture<T> {
111 type Output = Result<ResolvedVc<T>>;
112
113 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
114 let this = unsafe { self.get_unchecked_mut() };
116 Pin::new(&mut this.inner).poll(cx).map(|r| {
118 r.map(|node| ResolvedVc {
119 node: Vc {
120 node,
121 _t: PhantomData,
122 },
123 })
124 })
125 }
126}
127
128impl<T: ?Sized> Unpin for ToResolvedVcFuture<T> {}
129
130type VcReadTarget<T> = <<T as VcValueType>::Read as VcRead<T>>::Target;
131
132#[doc = include_str!("README.md")]
133#[must_use]
134#[derive(Serialize, Deserialize, Encode, Decode)]
135#[serde(transparent, bound = "")]
136#[bincode(bounds = "T: ?Sized")]
137#[repr(transparent)]
138pub struct Vc<T>
139where
140 T: ?Sized,
141{
142 pub(crate) node: RawVc,
143 #[doc(hidden)]
144 pub(crate) _t: PhantomData<T>,
145}
146
147pub struct VcDeref<T>
151where
152 T: ?Sized,
153{
154 _t: PhantomData<T>,
155}
156
157macro_rules! do_not_use_or_you_will_be_fired {
158 ($($name:ident)*) => {
159 impl<T> VcDeref<T>
160 where
161 T: ?Sized,
162 {
163 $(
164 #[doc(hidden)]
165 #[allow(unused)]
166 #[allow(clippy::wrong_self_convention)]
167 #[deprecated = "This is not the method you are looking for."]
168 pub fn $name(self) {}
169 )*
170 }
171 };
172}
173
174do_not_use_or_you_will_be_fired!(
179 add
180 addr
181 align_offset
182 as_mut
183 as_mut_ptr
184 as_ptr
185 as_ref
186 as_uninit_mut
187 as_uninit_ref
188 as_uninit_slice
189 as_uninit_slice_mut
190 byte_add
191 byte_offset
192 byte_offset_from
193 byte_sub
194 cast
195 cast_const
196 cast_mut
197 copy_from
198 copy_from_nonoverlapping
199 copy_to
200 copy_to_nonoverlapping
201 drop_in_place
202 expose_addr
203 from_bits
204 get_unchecked
205 get_unchecked_mut
206 guaranteed_eq
207 guaranteed_ne
208 is_aligned
209 is_aligned_to
210 is_empty
211 is_null
212 len
213 map_addr
214 mask
215 offset
216 offset_from
217 read
218 read_unaligned
219 read_volatile
220 replace
221 split_at_mut
222 split_at_mut_unchecked
223 sub
224 sub_ptr
225 swap
226 to_bits
227 to_raw_parts
228 with_addr
229 with_metadata_of
230 wrapping_add
231 wrapping_byte_add
232 wrapping_byte_offset
233 wrapping_byte_sub
234 wrapping_offset
235 wrapping_sub
236 write
237 write_bytes
238 write_unaligned
239 write_volatile
240);
241
242#[doc(hidden)]
245impl<T> Deref for VcDeref<T>
246where
247 T: ?Sized,
248{
249 type Target = *const *mut *const T;
253
254 fn deref(&self) -> &Self::Target {
255 unsafe extern "C" {
256 #[link_name = "\n\nERROR: you tried to dereference a `Vc<T>`\n"]
257 fn trigger() -> !;
258 }
259
260 unsafe { trigger() };
261 }
262}
263
264#[doc(hidden)]
274impl<T> Deref for Vc<T>
275where
276 T: ?Sized,
277{
278 type Target = VcDeref<T>;
279
280 fn deref(&self) -> &Self::Target {
281 unsafe extern "C" {
282 #[link_name = "\n\nERROR: you tried to dereference a `Vc<T>`\n"]
283 fn trigger() -> !;
284 }
285
286 unsafe { trigger() };
287 }
288}
289
290impl<T> Copy for Vc<T> where T: ?Sized {}
291
292impl<T> Clone for Vc<T>
293where
294 T: ?Sized,
295{
296 fn clone(&self) -> Self {
297 *self
298 }
299}
300
301impl<T> Hash for Vc<T>
302where
303 T: ?Sized,
304{
305 fn hash<H: Hasher>(&self, state: &mut H) {
306 self.node.hash(state);
307 }
308}
309
310impl<T> PartialEq<Vc<T>> for Vc<T>
311where
312 T: ?Sized,
313{
314 fn eq(&self, other: &Self) -> bool {
315 self.node == other.node
316 }
317}
318
319impl<T> Eq for Vc<T> where T: ?Sized {}
320
321impl<T> Debug for Vc<T>
327where
328 T: ?Sized,
329{
330 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
331 f.debug_tuple("Vc").field(&self.node).finish()
332 }
333}
334
335impl<T> Vc<T>
336where
337 T: VcValueType,
338{
339 #[doc(hidden)]
341 pub fn cell_private(mut inner: <T::Read as VcRead<T>>::Target) -> Self {
342 ShrinkToFit::shrink_to_fit(<T::Read as VcRead<T>>::target_to_value_mut_ref(&mut inner));
344 <T::CellMode as VcCellMode<T>>::cell(inner)
345 }
346}
347
348impl<T, Inner> Vc<T>
349where
350 T: VcValueType<Read = VcTransparentRead<T, Inner>>,
351 Inner: Any + Send + Sync,
352{
353 pub fn cell(inner: Inner) -> Self {
354 Self::cell_private(inner)
355 }
356}
357
358impl<T> Vc<T>
359where
360 T: ?Sized,
361{
362 pub async fn debug_identifier(vc: Self) -> Result<String> {
364 let resolved = vc.to_resolved().await?;
365 let raw_vc: RawVc = resolved.node.node;
366 if let Some((task_id, cell_id)) = raw_vc.as_task_cell() {
367 let value_name = registry::get_value_type(cell_id.type_id()).ty.name;
368 Ok(format!(
369 "{value_name}#{index}: {task_id}",
370 index = cell_id.index(),
371 ))
372 } else {
373 unreachable!()
374 }
375 }
376
377 pub fn into_raw(vc: Self) -> RawVc {
379 vc.node
380 }
381
382 #[inline(always)]
386 pub fn upcast<K>(vc: Self) -> Vc<K>
387 where
388 T: UpcastStrict<K>,
389 K: VcValueTrait + ?Sized,
390 {
391 Self::upcast_non_strict(vc)
392 }
393
394 #[inline(always)]
413 pub fn upcast_non_strict<K>(vc: Self) -> Vc<K>
414 where
415 T: Upcast<K>,
416 K: VcValueTrait + ?Sized,
417 {
418 Vc {
419 node: vc.node,
420 _t: PhantomData,
421 }
422 }
423 pub async fn as_side_effect(self) -> Result<()> {
426 self.node.resolve().await?;
427 Ok(())
428 }
429
430 pub fn resolve(self) -> ResolveVcFuture<T> {
433 ResolveVcFuture {
434 inner: self.node.resolve(),
435 _t: PhantomData,
436 }
437 }
438
439 pub fn to_resolved(self) -> ToResolvedVcFuture<T> {
443 ToResolvedVcFuture {
444 inner: self.node.resolve(),
445 _t: PhantomData,
446 }
447 }
448
449 pub async fn drop_content_hint(self) -> Result<()> {
460 let RawVcUnpacked::TaskCell(task, index) = self.node.resolve().await?.unpack() else {
461 unreachable!("resolve always returns a TaskCell");
462 };
463 crate::turbo_tasks().drop_task_cell_content_hint(task, index)
464 }
465
466 pub fn is_resolved(self) -> bool {
474 self.node.is_resolved()
475 }
476
477 pub fn is_local(self) -> bool {
484 self.node.is_local_output()
485 }
486}
487
488impl<T> From<RawVc> for Vc<T>
489where
490 T: ?Sized,
491{
492 fn from(node: RawVc) -> Self {
493 Self {
494 node,
495 _t: PhantomData,
496 }
497 }
498}
499
500#[cfg(debug_assertions)]
501impl<T> ValueDebugFormat for Vc<T>
502where
503 T: UpcastStrict<Box<dyn ValueDebug>> + Send + Sync + ?Sized,
504{
505 fn value_debug_format(&self, depth: usize) -> ValueDebugFormatString<'_> {
506 ValueDebugFormatString::Async(Box::pin(async move {
507 let vc_value_debug = Vc::upcast::<Box<dyn ValueDebug>>(*self);
508 let trait_ref = vc_value_debug.into_trait_ref().await?;
509 trait_ref.dbg_depth(depth).await
510 }))
511 }
512}
513
514macro_rules! into_future {
515 ($ty:ty, |$this:ident| $into_future:expr $(,)?) => {
516 impl<T> ::std::future::IntoFuture for $ty
517 where
518 T: $crate::VcValueType,
519 {
520 type Output = <$crate::ReadVcFuture<T> as ::std::future::Future>::Output;
521 type IntoFuture = $crate::ReadVcFuture<T>;
522 fn into_future(self) -> Self::IntoFuture {
523 let $this = self;
524 $into_future
525 }
526 }
527 };
528}
529pub(crate) use into_future;
530
531into_future!(Vc<T>, |this| this.node.into_read().into());
532into_future!(&Vc<T>, |this| this.node.into_read().into());
533into_future!(&mut Vc<T>, |this| this.node.into_read().into());
534
535impl<T> Vc<T>
536where
537 T: VcValueType,
538{
539 #[cfg(feature = "non_operation_vc_strongly_consistent")]
541 pub fn strongly_consistent(self) -> ReadVcFuture<T> {
542 self.node.into_read().strongly_consistent().into()
543 }
544
545 pub fn untracked(self) -> ReadVcFuture<T> {
548 self.node.into_read().untracked().into()
549 }
550
551 pub fn final_read_hint(self) -> ReadVcFuture<T> {
554 self.node.into_read().final_read_hint().into()
555 }
556}
557
558impl<T> Vc<T>
559where
560 T: VcValueType,
561 VcReadTarget<T>: Clone,
562{
563 pub fn owned(self) -> ReadOwnedVcFuture<T> {
565 let future: ReadVcFuture<T> = self.node.into_read().into();
566 future.owned()
567 }
568}
569
570impl<T> Vc<T>
571where
572 T: VcValueType,
573 VcReadTarget<T>: KeyedEq,
574{
575 pub fn get<'l, Q>(self, key: &'l Q) -> ReadKeyedVcFuture<'l, T, Q>
578 where
579 Q: Hash + ?Sized,
580 VcReadTarget<T>: KeyedAccess<Q>,
581 {
582 let future: ReadVcFuture<T> = self.node.into_read().into();
583 future.get(key)
584 }
585
586 pub fn contains_key<'l, Q>(self, key: &'l Q) -> ReadContainsKeyedVcFuture<'l, T, Q>
589 where
590 Q: Hash + ?Sized,
591 VcReadTarget<T>: KeyedAccess<Q>,
592 {
593 let future: ReadVcFuture<T> = self.node.into_read().into();
594 future.contains_key(key)
595 }
596
597 pub fn keyed_reader(self) -> ReadKeyedReaderVcFuture<T> {
606 let future: ReadVcFuture<T> = self.node.into_read().into();
607 future.read_keyed_reader()
608 }
609}
610
611impl<T> Vc<T>
612where
613 T: VcValueType,
614{
615 pub fn lazy_reader(self) -> ReadLazyReaderVcFuture<T> {
623 let future: ReadVcFuture<T> = self.node.into_read().into();
624 future.read_lazy_reader()
625 }
626}
627
628impl<T> Unpin for Vc<T> where T: ?Sized {}
629
630impl<T> Vc<T>
631where
632 T: VcValueTrait + ?Sized,
633{
634 pub fn into_trait_ref(self) -> ReadVcFuture<T, VcValueTraitCast<T>> {
640 self.node.into_read().into()
641 }
642}
643
644impl<T> Default for Vc<T>
645where
646 T: ValueDefault,
647{
648 fn default() -> Self {
649 T::value_default()
650 }
651}
652
653pub trait OptionVcExt<T>
654where
655 T: VcValueType,
656{
657 fn to_resolved(self) -> impl Future<Output = Result<Option<ResolvedVc<T>>>> + Send;
658}
659
660impl<T> OptionVcExt<T> for Option<Vc<T>>
661where
662 T: VcValueType,
663{
664 async fn to_resolved(self) -> Result<Option<ResolvedVc<T>>> {
665 if let Some(vc) = self {
666 Ok(Some(vc.to_resolved().await?))
667 } else {
668 Ok(None)
669 }
670 }
671}