|
|
use std::{any::Any, marker::PhantomData, mem::ManuallyDrop, pin::Pin, task::Poll}; |
|
|
|
|
|
use anyhow::Result; |
|
|
use futures::Future; |
|
|
|
|
|
use super::traits::VcValueType; |
|
|
use crate::{ReadRawVcFuture, ReadRef, VcCast, VcValueTrait, VcValueTraitCast, VcValueTypeCast}; |
|
|
|
|
|
type VcReadTarget<T> = <<T as VcValueType>::Read as VcRead<T>>::Target; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub trait VcRead<T> |
|
|
where |
|
|
T: VcValueType, |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Target; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Repr: VcValueType; |
|
|
|
|
|
|
|
|
fn value_to_target_ref(value: &T) -> &Self::Target; |
|
|
|
|
|
|
|
|
fn value_to_target(value: T) -> Self::Target; |
|
|
|
|
|
|
|
|
fn value_to_repr(value: T) -> Self::Repr; |
|
|
|
|
|
|
|
|
fn target_to_value(target: Self::Target) -> T; |
|
|
|
|
|
|
|
|
fn target_to_value_ref(target: &Self::Target) -> &T; |
|
|
|
|
|
|
|
|
fn target_to_value_mut_ref(target: &mut Self::Target) -> &mut T; |
|
|
|
|
|
|
|
|
fn target_to_repr(target: Self::Target) -> Self::Repr; |
|
|
|
|
|
|
|
|
fn repr_to_value_ref(repr: &Self::Repr) -> &T; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub struct VcDefaultRead<T> { |
|
|
_phantom: PhantomData<T>, |
|
|
} |
|
|
|
|
|
impl<T> VcRead<T> for VcDefaultRead<T> |
|
|
where |
|
|
T: VcValueType, |
|
|
{ |
|
|
type Target = T; |
|
|
type Repr = T; |
|
|
|
|
|
fn value_to_target_ref(value: &T) -> &Self::Target { |
|
|
value |
|
|
} |
|
|
|
|
|
fn value_to_target(value: T) -> Self::Target { |
|
|
value |
|
|
} |
|
|
|
|
|
fn value_to_repr(value: T) -> Self::Repr { |
|
|
value |
|
|
} |
|
|
|
|
|
fn target_to_value(target: Self::Target) -> T { |
|
|
target |
|
|
} |
|
|
|
|
|
fn target_to_value_ref(target: &Self::Target) -> &T { |
|
|
target |
|
|
} |
|
|
|
|
|
fn target_to_value_mut_ref(target: &mut Self::Target) -> &mut T { |
|
|
target |
|
|
} |
|
|
|
|
|
fn target_to_repr(target: Self::Target) -> Self::Repr { |
|
|
target |
|
|
} |
|
|
|
|
|
fn repr_to_value_ref(repr: &Self::Repr) -> &T { |
|
|
repr |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub struct VcTransparentRead<T, Target, Repr> { |
|
|
_phantom: PhantomData<(T, Target, Repr)>, |
|
|
} |
|
|
|
|
|
impl<T, Target, Repr> VcRead<T> for VcTransparentRead<T, Target, Repr> |
|
|
where |
|
|
T: VcValueType, |
|
|
Target: Any + Send + Sync, |
|
|
Repr: VcValueType, |
|
|
{ |
|
|
type Target = Target; |
|
|
type Repr = Repr; |
|
|
|
|
|
fn value_to_target_ref(value: &T) -> &Self::Target { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsafe { |
|
|
std::mem::transmute_copy::<ManuallyDrop<&T>, &Self::Target>(&ManuallyDrop::new(value)) |
|
|
} |
|
|
} |
|
|
|
|
|
fn value_to_target(value: T) -> Self::Target { |
|
|
|
|
|
unsafe { |
|
|
std::mem::transmute_copy::<ManuallyDrop<T>, Self::Target>(&ManuallyDrop::new(value)) |
|
|
} |
|
|
} |
|
|
|
|
|
fn value_to_repr(value: T) -> Self::Repr { |
|
|
|
|
|
unsafe { |
|
|
std::mem::transmute_copy::<ManuallyDrop<T>, Self::Repr>(&ManuallyDrop::new(value)) |
|
|
} |
|
|
} |
|
|
|
|
|
fn target_to_value(target: Self::Target) -> T { |
|
|
|
|
|
unsafe { |
|
|
std::mem::transmute_copy::<ManuallyDrop<Self::Target>, T>(&ManuallyDrop::new(target)) |
|
|
} |
|
|
} |
|
|
|
|
|
fn target_to_value_ref(target: &Self::Target) -> &T { |
|
|
|
|
|
unsafe { |
|
|
std::mem::transmute_copy::<ManuallyDrop<&Self::Target>, &T>(&ManuallyDrop::new(target)) |
|
|
} |
|
|
} |
|
|
|
|
|
fn target_to_value_mut_ref(target: &mut Self::Target) -> &mut T { |
|
|
|
|
|
unsafe { |
|
|
std::mem::transmute_copy::<ManuallyDrop<&mut Self::Target>, &mut T>(&ManuallyDrop::new( |
|
|
target, |
|
|
)) |
|
|
} |
|
|
} |
|
|
|
|
|
fn target_to_repr(target: Self::Target) -> Self::Repr { |
|
|
|
|
|
unsafe { |
|
|
std::mem::transmute_copy::<ManuallyDrop<Self::Target>, Self::Repr>(&ManuallyDrop::new( |
|
|
target, |
|
|
)) |
|
|
} |
|
|
} |
|
|
|
|
|
fn repr_to_value_ref(repr: &Self::Repr) -> &T { |
|
|
|
|
|
unsafe { |
|
|
std::mem::transmute_copy::<ManuallyDrop<&Self::Repr>, &T>(&ManuallyDrop::new(repr)) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
pub struct ReadVcFuture<T, Cast = VcValueTypeCast<T>> |
|
|
where |
|
|
T: ?Sized, |
|
|
Cast: VcCast, |
|
|
{ |
|
|
raw: ReadRawVcFuture, |
|
|
_phantom_t: PhantomData<T>, |
|
|
_phantom_cast: PhantomData<Cast>, |
|
|
} |
|
|
|
|
|
impl<T, Cast> ReadVcFuture<T, Cast> |
|
|
where |
|
|
T: ?Sized, |
|
|
Cast: VcCast, |
|
|
{ |
|
|
pub fn strongly_consistent(mut self) -> Self { |
|
|
self.raw = self.raw.strongly_consistent(); |
|
|
self |
|
|
} |
|
|
|
|
|
pub fn untracked(mut self) -> Self { |
|
|
self.raw = self.raw.untracked(); |
|
|
self |
|
|
} |
|
|
|
|
|
pub fn final_read_hint(mut self) -> Self { |
|
|
self.raw = self.raw.final_read_hint(); |
|
|
self |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> ReadVcFuture<T, VcValueTypeCast<T>> |
|
|
where |
|
|
T: VcValueType, |
|
|
VcReadTarget<T>: Clone, |
|
|
{ |
|
|
pub fn owned(self) -> ReadOwnedVcFuture<T> { |
|
|
ReadOwnedVcFuture { future: self } |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> From<ReadRawVcFuture> for ReadVcFuture<T, VcValueTypeCast<T>> |
|
|
where |
|
|
T: VcValueType, |
|
|
{ |
|
|
fn from(raw: ReadRawVcFuture) -> Self { |
|
|
Self { |
|
|
raw, |
|
|
_phantom_t: PhantomData, |
|
|
_phantom_cast: PhantomData, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> From<ReadRawVcFuture> for ReadVcFuture<T, VcValueTraitCast<T>> |
|
|
where |
|
|
T: VcValueTrait + ?Sized, |
|
|
{ |
|
|
fn from(raw: ReadRawVcFuture) -> Self { |
|
|
Self { |
|
|
raw, |
|
|
_phantom_t: PhantomData, |
|
|
_phantom_cast: PhantomData, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T, Cast> Future for ReadVcFuture<T, Cast> |
|
|
where |
|
|
T: ?Sized, |
|
|
Cast: VcCast, |
|
|
{ |
|
|
type Output = Result<Cast::Output>; |
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> { |
|
|
|
|
|
let raw = unsafe { self.map_unchecked_mut(|this| &mut this.raw) }; |
|
|
Poll::Ready(std::task::ready!(raw.poll(cx)).and_then(Cast::cast)) |
|
|
} |
|
|
} |
|
|
|
|
|
pub struct ReadOwnedVcFuture<T> |
|
|
where |
|
|
T: VcValueType, |
|
|
VcReadTarget<T>: Clone, |
|
|
{ |
|
|
future: ReadVcFuture<T, VcValueTypeCast<T>>, |
|
|
} |
|
|
|
|
|
impl<T> Future for ReadOwnedVcFuture<T> |
|
|
where |
|
|
T: VcValueType, |
|
|
VcReadTarget<T>: Clone, |
|
|
{ |
|
|
type Output = Result<VcReadTarget<T>>; |
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> { |
|
|
|
|
|
let future = unsafe { self.map_unchecked_mut(|this| &mut this.future) }; |
|
|
match future.poll(cx) { |
|
|
Poll::Ready(Ok(result)) => Poll::Ready(Ok(ReadRef::into_owned(result))), |
|
|
Poll::Ready(Err(err)) => Poll::Ready(Err(err)), |
|
|
Poll::Pending => Poll::Pending, |
|
|
} |
|
|
} |
|
|
} |
|
|
|