use std::{fmt::Display, future::Future, pin::Pin, task::Poll};
use anyhow::Result;
use auto_hash_map::AutoSet;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::{
CollectiblesSource, ReadCellOptions, ReadConsistency, ResolvedVc, TaskId, TaskPersistence,
TraitTypeId, ValueType, ValueTypeId, VcValueTrait,
backend::{CellContent, TypedCellContent},
event::EventListener,
id::{ExecutionId, LocalTaskId},
manager::{read_local_output, read_task_cell, read_task_output, with_turbo_tasks},
registry::{self, get_value_type},
turbo_tasks,
};
#[derive(Error, Debug)]
pub enum ResolveTypeError {
#[error("no content in the cell")]
NoContent,
#[error("the content in the cell has no type")]
UntypedContent,
#[error("content is not available as task execution failed")]
TaskError { source: anyhow::Error },
#[error("reading the cell content failed")]
ReadError { source: anyhow::Error },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CellId {
pub type_id: ValueTypeId,
pub index: u32,
}
impl Display for CellId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}#{}",
registry::get_value_type(self.type_id).name,
self.index
)
}
}
/// A type-erased representation of [`Vc`][crate::Vc].
///
/// Type erasure reduces the [monomorphization] (and therefore binary size and compilation time)
/// required to support [`Vc`][crate::Vc].
///
/// This type is heavily used within the [`Backend`][crate::backend::Backend] trait, but should
/// otherwise be treated as an internal implementation detail of `turbo-tasks`.
///
/// [monomorphization]: https://doc.rust-lang.org/book/ch10-01-syntax.html#performance-of-code-using-generics
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RawVc {
/// The synchronous return value of a task (after argument resolution). This is the
/// representation used by [`OperationVc`][crate::OperationVc].
TaskOutput(TaskId),
/// A pointer to a specific [`Vc::cell`][crate::Vc::cell] or `.cell()` call within a task. This
/// is the representation used by [`ResolvedVc`].
///
/// [`CellId`] contains the [`ValueTypeId`], which can be useful for efficient downcasting.
TaskCell(TaskId, CellId),
/// The synchronous return value of a local task. This is created when a function is called
/// with unresolved arguments or more explicitly with
/// [`#[turbo_tasks::function(local)]`][crate::function].
///
/// Local outputs are only valid within the context of their parent "non-local" task. Turbo
/// Task's APIs are designed to prevent escapes of local [`Vc`]s, but [`ExecutionId`] is used
/// for a fallback runtime assertion.
LocalOutput(ExecutionId, LocalTaskId, TaskPersistence),
}
impl RawVc {
pub fn is_resolved(&self) -> bool {
match self {
RawVc::TaskOutput(..) => false,
RawVc::TaskCell(..) => true,
RawVc::LocalOutput(..) => false,
}
}
pub fn is_local(&self) -> bool {
match self {
RawVc::TaskOutput(..) => false,
RawVc::TaskCell(..) => false,
RawVc::LocalOutput(..) => true,
}
}
/// Returns `true` if the task this `RawVc` reads from cannot be serialized and will not be
/// stored in the persistent cache.
///
/// See [`TaskPersistence`] for more details.
pub fn is_transient(&self) -> bool {
match self {
RawVc::TaskOutput(task) | RawVc::TaskCell(task, ..) => task.is_transient(),
RawVc::LocalOutput(_, _, persistence) => *persistence == TaskPersistence::Transient,
}
}
pub(crate) fn into_read(self) -> ReadRawVcFuture {
// returns a custom future to have something concrete and sized
// this avoids boxing in IntoFuture
ReadRawVcFuture::new(self)
}
pub(crate) async fn resolve_trait(
self,
trait_type: TraitTypeId,
) -> Result