turbo_tasks/vc/
operation.rs1use std::{
2 fmt::Debug,
3 future::Future,
4 hash::Hash,
5 marker::PhantomData,
6 pin::Pin,
7 task::{Context, Poll},
8};
9
10use anyhow::Result;
11use auto_hash_map::AutoSet;
12use bincode::{Decode, Encode};
13use serde::{Deserialize, Serialize};
14pub use turbo_tasks_macros::OperationValue;
15
16use crate::{
17 CollectiblesSource, RawVc, ReadVcFuture, ResolvedVc, TaskId, TaskInput, UpcastStrict, Vc,
18 VcValueTrait, VcValueTraitCast, VcValueType, marker_trait::impl_auto_marker_trait,
19 trace::TraceRawVcs, turbo_tasks,
20};
21
22#[must_use]
27pub struct ResolveOperationVcFuture<T>
28where
29 T: ?Sized,
30{
31 inner: super::ResolveVcFuture<T>,
32}
33
34impl<T: ?Sized> ResolveOperationVcFuture<T> {
35 pub fn strongly_consistent(mut self) -> Self {
37 self.inner.inner = self.inner.inner.strongly_consistent();
38 self
39 }
40}
41
42impl<T: ?Sized> Future for ResolveOperationVcFuture<T> {
43 type Output = anyhow::Result<ResolvedVc<T>>;
44
45 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
46 let this = unsafe { self.get_unchecked_mut() };
48 Pin::new(&mut this.inner)
50 .poll(cx)
51 .map(|r| r.map(|node| ResolvedVc { node }))
52 }
53}
54
55impl<T: ?Sized> Unpin for ResolveOperationVcFuture<T> {}
56
57#[must_use]
95#[derive(Serialize, Deserialize, Encode, Decode)]
96#[bincode(bounds = "T: ?Sized")]
97pub struct OperationVc<T>
98where
99 T: ?Sized,
100{
101 task: TaskId,
102
103 _t: PhantomData<T>,
104}
105
106impl<T: ?Sized> OperationVc<T> {
107 #[doc(hidden)]
111 #[deprecated = "This is an internal function. Use #[turbo_tasks::function(operation)] instead."]
112 pub fn cell_private(node: Vc<T>) -> Self {
113 let task = node.node.as_task_output().expect(
114 "OperationVc::cell_private must be called on the immediate return value of a task \
115 function",
116 );
117
118 Self {
119 task,
120 _t: PhantomData,
121 }
122 }
123
124 pub fn connect(self) -> Vc<T> {
132 let tt = turbo_tasks();
133 tt.connect_task(self.task);
134 Self::into_raw(self).into()
135 }
136
137 fn into_raw(vc: Self) -> RawVc {
140 RawVc::task_output(vc.task)
141 }
142
143 #[inline(always)]
147 pub fn upcast<K>(vc: Self) -> OperationVc<K>
148 where
149 T: UpcastStrict<K>,
150 K: VcValueTrait + ?Sized,
151 {
152 OperationVc {
153 task: vc.task,
154 _t: PhantomData,
155 }
156 }
157
158 pub fn resolve(self) -> ResolveOperationVcFuture<T> {
169 ResolveOperationVcFuture {
170 inner: self.connect().resolve(),
171 }
172 }
173
174 pub fn read_strongly_consistent(self) -> ReadVcFuture<T>
179 where
180 T: VcValueType,
181 {
182 self.connect().node.into_read().strongly_consistent().into()
183 }
184
185 pub fn read_trait_strongly_consistent(self) -> ReadVcFuture<T, VcValueTraitCast<T>>
190 where
191 T: VcValueTrait,
192 {
193 self.connect().into_trait_ref().strongly_consistent()
194 }
195}
196
197impl<T> Copy for OperationVc<T> where T: ?Sized {}
198
199impl<T> Clone for OperationVc<T>
200where
201 T: ?Sized,
202{
203 fn clone(&self) -> Self {
204 *self
205 }
206}
207
208impl<T> Hash for OperationVc<T>
209where
210 T: ?Sized,
211{
212 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
213 self.task.hash(state);
214 }
215}
216
217impl<T> PartialEq<OperationVc<T>> for OperationVc<T>
218where
219 T: ?Sized,
220{
221 fn eq(&self, other: &Self) -> bool {
222 self.task == other.task
223 }
224}
225
226impl<T> Eq for OperationVc<T> where T: ?Sized {}
227
228impl<T> Debug for OperationVc<T>
229where
230 T: ?Sized,
231{
232 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
233 f.debug_struct("OperationVc")
234 .field("task", &self.task)
235 .finish()
236 }
237}
238
239impl<T> TaskInput for OperationVc<T>
242where
243 T: ?Sized + Send + Sync,
244{
245 fn is_transient(&self) -> bool {
246 self.task.is_transient()
247 }
248}
249
250impl<T> TryFrom<RawVc> for OperationVc<T>
251where
252 T: ?Sized,
253{
254 type Error = anyhow::Error;
255
256 fn try_from(raw: RawVc) -> Result<Self> {
257 let Some(task) = raw.as_task_output() else {
258 anyhow::bail!("Given RawVc {raw:?} is not a TaskOutput");
259 };
260 Ok(Self {
261 task,
262 _t: PhantomData,
263 })
264 }
265}
266
267impl<T> TraceRawVcs for OperationVc<T>
268where
269 T: ?Sized,
270{
271 fn trace_raw_vcs(&self, trace_context: &mut crate::trace::TraceRawVcsContext) {
272 Self::into_raw(*self).trace_raw_vcs(trace_context);
273 }
274}
275
276impl<T> CollectiblesSource for OperationVc<T>
277where
278 T: ?Sized,
279{
280 fn drop_collectibles<Vt: VcValueTrait>(self) {
281 let tt = turbo_tasks();
282 let map = tt.read_task_collectibles(self.task, Vt::get_trait_type_id());
283 tt.unemit_collectibles(Vt::get_trait_type_id(), &map);
284 }
285
286 fn take_collectibles<Vt: VcValueTrait>(self) -> AutoSet<ResolvedVc<Vt>> {
287 let tt = turbo_tasks();
288 let map = tt.read_task_collectibles(self.task, Vt::get_trait_type_id());
289 tt.unemit_collectibles(Vt::get_trait_type_id(), &map);
290 map.into_iter()
291 .filter_map(|(raw, count)| (count > 0).then_some(raw.try_into().unwrap()))
292 .collect()
293 }
294
295 fn peek_collectibles<Vt: VcValueTrait>(self) -> AutoSet<ResolvedVc<Vt>> {
296 let tt = turbo_tasks();
297 let map = tt.read_task_collectibles(self.task, Vt::get_trait_type_id());
298 map.into_iter()
299 .filter_map(|(raw, count)| (count > 0).then_some(raw.try_into().unwrap()))
300 .collect()
301 }
302}
303
304pub unsafe trait OperationValue {}
312
313unsafe impl<T: ?Sized + Send> OperationValue for OperationVc<T> {}
314
315impl_auto_marker_trait!(OperationValue);