File size: 8,662 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | 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;
/// Trait that controls [`crate::Vc`]'s read representation.
///
/// Has two implementations:
/// * [`VcDefaultRead`]
/// * [`VcTransparentRead`]
///
/// This trait must remain sealed within this crate.
pub trait VcRead<T>
where
T: VcValueType,
{
/// The read target type. This is the type that will be returned when
/// `.await`ing a `Vc` of a value type.
///
/// For instance, the target of `.await`ing a `Vc<Completion>` will be a
/// `Completion`. When using `#[turbo_tasks::value(transparent)]`, the
/// target will be different than the value type.
type Target;
/// The representation type. This is what will be used to
/// serialize/deserialize the value, and this determines the
/// type that the value will be upcasted to for storage.
///
/// For instance, when storing generic collection types such as
/// `Vec<Vc<ValueType>>`, we first cast them to a shared `Vec<Vc<()>>`
/// type instead, which has an equivalent memory representation to any
/// `Vec<Vc<T>>` type. This allows sharing implementations of methods and
/// traits between all `Vec<Vc<T>>`.
type Repr: VcValueType;
/// Convert a reference to a value to a reference to the target type.
fn value_to_target_ref(value: &T) -> &Self::Target;
/// Convert a value to the target type.
fn value_to_target(value: T) -> Self::Target;
/// Convert the value type to the repr.
fn value_to_repr(value: T) -> Self::Repr;
/// Convert the target type to the value.
fn target_to_value(target: Self::Target) -> T;
/// Convert a reference to a target type to a reference to a value.
fn target_to_value_ref(target: &Self::Target) -> &T;
/// Convert a mutable reference to a target type to a reference to a value.
fn target_to_value_mut_ref(target: &mut Self::Target) -> &mut T;
/// Convert the target type to the repr.
fn target_to_repr(target: Self::Target) -> Self::Repr;
/// Convert a reference to a repr type to a reference to a value.
fn repr_to_value_ref(repr: &Self::Repr) -> &T;
}
/// Representation for standard `#[turbo_tasks::value]`, where a read return a
/// reference to the value type[]
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
}
}
/// Representation for `#[turbo_tasks::value(transparent)]` types, where reads
/// return a reference to the target type.
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 {
// Safety: the `VcValueType` implementor must guarantee that both `T` and
// `Target` are #[repr(transparent)]. This is guaranteed by the
// `#[turbo_tasks::value(transparent)]` macro.
// We can't use `std::mem::transmute` here as it doesn't support generic types.
// See https://users.rust-lang.org/t/transmute-doesnt-work-on-generic-types/87272/9
unsafe {
std::mem::transmute_copy::<ManuallyDrop<&T>, &Self::Target>(&ManuallyDrop::new(value))
}
}
fn value_to_target(value: T) -> Self::Target {
// Safety: see `Self::value_to_target_ref` above.
unsafe {
std::mem::transmute_copy::<ManuallyDrop<T>, Self::Target>(&ManuallyDrop::new(value))
}
}
fn value_to_repr(value: T) -> Self::Repr {
// Safety: see `Self::value_to_target_ref` above.
unsafe {
std::mem::transmute_copy::<ManuallyDrop<T>, Self::Repr>(&ManuallyDrop::new(value))
}
}
fn target_to_value(target: Self::Target) -> T {
// Safety: see `Self::value_to_target_ref` above.
unsafe {
std::mem::transmute_copy::<ManuallyDrop<Self::Target>, T>(&ManuallyDrop::new(target))
}
}
fn target_to_value_ref(target: &Self::Target) -> &T {
// Safety: see `Self::value_to_target_ref` above.
unsafe {
std::mem::transmute_copy::<ManuallyDrop<&Self::Target>, &T>(&ManuallyDrop::new(target))
}
}
fn target_to_value_mut_ref(target: &mut Self::Target) -> &mut T {
// Safety: see `Self::value_to_target_ref` above.
unsafe {
std::mem::transmute_copy::<ManuallyDrop<&mut Self::Target>, &mut T>(&ManuallyDrop::new(
target,
))
}
}
fn target_to_repr(target: Self::Target) -> Self::Repr {
// Safety: see `Self::value_to_target_ref` above.
unsafe {
std::mem::transmute_copy::<ManuallyDrop<Self::Target>, Self::Repr>(&ManuallyDrop::new(
target,
))
}
}
fn repr_to_value_ref(repr: &Self::Repr) -> &T {
// Safety: see `Self::value_to_target_ref` above.
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> {
// Safety: We never move the contents of `self`
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> {
// Safety: We never move the contents of `self`
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,
}
}
}
|