|
|
use std::{ |
|
|
any::Any, |
|
|
fmt::Debug, |
|
|
future::IntoFuture, |
|
|
hash::{Hash, Hasher}, |
|
|
marker::PhantomData, |
|
|
mem::transmute, |
|
|
ops::Deref, |
|
|
}; |
|
|
|
|
|
use anyhow::Result; |
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
|
|
use crate::{ |
|
|
RawVc, Upcast, VcRead, VcTransparentRead, VcValueTrait, VcValueType, |
|
|
debug::{ValueDebug, ValueDebugFormat, ValueDebugFormatString}, |
|
|
trace::{TraceRawVcs, TraceRawVcsContext}, |
|
|
vc::Vc, |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)] |
|
|
#[serde(transparent, bound = "")] |
|
|
#[repr(transparent)] |
|
|
pub struct ResolvedVc<T> |
|
|
where |
|
|
T: ?Sized, |
|
|
{ |
|
|
pub(crate) node: Vc<T>, |
|
|
} |
|
|
|
|
|
impl<T> ResolvedVc<T> |
|
|
where |
|
|
T: ?Sized, |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
#[deprecated(note = "No point in resolving a vc that is already resolved")] |
|
|
pub async fn to_resolved(self) -> Result<Self> { |
|
|
Ok(self) |
|
|
} |
|
|
#[deprecated(note = "No point in resolving a vc that is already resolved")] |
|
|
pub async fn resolve(self) -> Result<Vc<T>> { |
|
|
Ok(self.node) |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> Copy for ResolvedVc<T> where T: ?Sized {} |
|
|
|
|
|
impl<T> Clone for ResolvedVc<T> |
|
|
where |
|
|
T: ?Sized, |
|
|
{ |
|
|
fn clone(&self) -> Self { |
|
|
*self |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> Deref for ResolvedVc<T> |
|
|
where |
|
|
T: ?Sized, |
|
|
{ |
|
|
type Target = Vc<T>; |
|
|
|
|
|
fn deref(&self) -> &Self::Target { |
|
|
&self.node |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> PartialEq<ResolvedVc<T>> for ResolvedVc<T> |
|
|
where |
|
|
T: ?Sized, |
|
|
{ |
|
|
fn eq(&self, other: &Self) -> bool { |
|
|
self.node == other.node |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> Eq for ResolvedVc<T> where T: ?Sized {} |
|
|
|
|
|
impl<T> Hash for ResolvedVc<T> |
|
|
where |
|
|
T: ?Sized, |
|
|
{ |
|
|
fn hash<H: Hasher>(&self, state: &mut H) { |
|
|
self.node.hash(state); |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T, Inner, Repr> Default for ResolvedVc<T> |
|
|
where |
|
|
T: VcValueType<Read = VcTransparentRead<T, Inner, Repr>>, |
|
|
Inner: Any + Send + Sync + Default, |
|
|
Repr: VcValueType, |
|
|
{ |
|
|
fn default() -> Self { |
|
|
Self::cell(Default::default()) |
|
|
} |
|
|
} |
|
|
|
|
|
macro_rules! into_future { |
|
|
($ty:ty) => { |
|
|
impl<T> IntoFuture for $ty |
|
|
where |
|
|
T: VcValueType, |
|
|
{ |
|
|
type Output = <Vc<T> as IntoFuture>::Output; |
|
|
type IntoFuture = <Vc<T> as IntoFuture>::IntoFuture; |
|
|
fn into_future(self) -> Self::IntoFuture { |
|
|
(*self).into_future() |
|
|
} |
|
|
} |
|
|
}; |
|
|
} |
|
|
|
|
|
into_future!(ResolvedVc<T>); |
|
|
into_future!(&ResolvedVc<T>); |
|
|
into_future!(&mut ResolvedVc<T>); |
|
|
|
|
|
impl<T> ResolvedVc<T> |
|
|
where |
|
|
T: VcValueType, |
|
|
{ |
|
|
|
|
|
#[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> ResolvedVc<T> |
|
|
where |
|
|
T: ?Sized, |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
#[inline(always)] |
|
|
pub fn upcast<K>(this: Self) -> ResolvedVc<K> |
|
|
where |
|
|
T: Upcast<K>, |
|
|
K: VcValueTrait + ?Sized, |
|
|
{ |
|
|
ResolvedVc { |
|
|
node: Vc::upcast(this.node), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn deref_vec(vec: Vec<ResolvedVc<T>>) -> Vec<Vc<T>> { |
|
|
debug_assert!(size_of::<ResolvedVc<T>>() == size_of::<Vc<T>>()); |
|
|
|
|
|
unsafe { transmute::<Vec<ResolvedVc<T>>, Vec<Vc<T>>>(vec) } |
|
|
} |
|
|
|
|
|
|
|
|
pub fn deref_slice(slice: &[ResolvedVc<T>]) -> &[Vc<T>] { |
|
|
debug_assert!(size_of::<ResolvedVc<T>>() == size_of::<Vc<T>>()); |
|
|
|
|
|
unsafe { transmute::<&[ResolvedVc<T>], &[Vc<T>]>(slice) } |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> ResolvedVc<T> |
|
|
where |
|
|
T: VcValueTrait + ?Sized, |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn try_sidecast<K>(this: Self) -> Option<ResolvedVc<K>> |
|
|
where |
|
|
K: VcValueTrait + ?Sized, |
|
|
{ |
|
|
|
|
|
|
|
|
let raw_vc = this.node.node; |
|
|
raw_vc |
|
|
.resolved_has_trait(<K as VcValueTrait>::get_trait_type_id()) |
|
|
.then_some(ResolvedVc { |
|
|
node: Vc { |
|
|
node: raw_vc, |
|
|
_t: PhantomData, |
|
|
}, |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn try_downcast<K>(this: Self) -> Option<ResolvedVc<K>> |
|
|
where |
|
|
K: Upcast<T> + VcValueTrait + ?Sized, |
|
|
{ |
|
|
|
|
|
Self::try_sidecast(this) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn try_downcast_type<K>(this: Self) -> Option<ResolvedVc<K>> |
|
|
where |
|
|
K: Upcast<T> + VcValueType, |
|
|
{ |
|
|
let raw_vc = this.node.node; |
|
|
raw_vc |
|
|
.resolved_is_type(<K as VcValueType>::get_value_type_id()) |
|
|
.then_some(ResolvedVc { |
|
|
node: Vc { |
|
|
node: raw_vc, |
|
|
_t: PhantomData, |
|
|
}, |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<T> Debug for ResolvedVc<T> |
|
|
where |
|
|
T: ?Sized, |
|
|
{ |
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
|
f.debug_struct("ResolvedVc") |
|
|
.field("node", &self.node.node) |
|
|
.finish() |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> TraceRawVcs for ResolvedVc<T> |
|
|
where |
|
|
T: ?Sized, |
|
|
{ |
|
|
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) { |
|
|
TraceRawVcs::trace_raw_vcs(&self.node, trace_context); |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> ValueDebugFormat for ResolvedVc<T> |
|
|
where |
|
|
T: Upcast<Box<dyn ValueDebug>> + Send + Sync + ?Sized, |
|
|
{ |
|
|
fn value_debug_format(&self, depth: usize) -> ValueDebugFormatString<'_> { |
|
|
self.node.value_debug_format(depth) |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> TryFrom<RawVc> for ResolvedVc<T> |
|
|
where |
|
|
T: ?Sized, |
|
|
{ |
|
|
type Error = anyhow::Error; |
|
|
|
|
|
fn try_from(raw: RawVc) -> Result<Self> { |
|
|
if !matches!(raw, RawVc::TaskCell(..)) { |
|
|
anyhow::bail!("Given RawVc {raw:?} is not a TaskCell"); |
|
|
} |
|
|
Ok(Self { |
|
|
node: Vc::from(raw), |
|
|
}) |
|
|
} |
|
|
} |
|
|
|