Skip to main content

turbo_tasks/
mapped_read_ref.rs

1use std::fmt::{Debug, Display};
2
3use serde::Serialize;
4
5#[cfg(debug_assertions)]
6use crate::debug::{ValueDebugFormat, ValueDebugFormatString};
7
8pub struct MappedReadRef<A, T> {
9    value: *const T,
10    arc: triomphe::Arc<A>,
11}
12
13unsafe impl<A: Send + Sync, T: Sync> Send for MappedReadRef<A, T> {}
14unsafe impl<A: Send + Sync, T: Sync> Sync for MappedReadRef<A, T> {}
15
16impl<A, T> MappedReadRef<A, T> {
17    /// # Safety
18    /// The caller must ensure that the `arc` keeps the value pointed to by `value` alive.
19    pub unsafe fn new(arc: triomphe::Arc<A>, value: *const T) -> Self {
20        Self { value, arc }
21    }
22}
23
24impl<A, T> MappedReadRef<A, T> {
25    pub fn ptr_eq(&self, other: &Self) -> bool {
26        std::ptr::eq(self.value, other.value)
27    }
28
29    pub fn ptr(&self) -> *const T {
30        self.value
31    }
32}
33
34impl<A, T> Clone for MappedReadRef<A, T> {
35    fn clone(&self) -> Self {
36        Self {
37            value: self.value,
38            arc: self.arc.clone(),
39        }
40    }
41}
42
43impl<A, T> std::ops::Deref for MappedReadRef<A, T> {
44    type Target = T;
45
46    fn deref(&self) -> &Self::Target {
47        unsafe { &*self.value }
48    }
49}
50
51impl<A, T> Debug for MappedReadRef<A, T>
52where
53    T: Debug,
54{
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        (**self).fmt(f)
57    }
58}
59
60impl<A, T> Display for MappedReadRef<A, T>
61where
62    T: Display,
63{
64    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        Display::fmt(&**self, f)
66    }
67}
68
69impl<A, T> PartialEq for MappedReadRef<A, T>
70where
71    T: PartialEq,
72{
73    fn eq(&self, other: &Self) -> bool {
74        **self == **other
75    }
76}
77
78impl<A, T> Eq for MappedReadRef<A, T> where T: Eq {}
79
80impl<A, T> PartialOrd for MappedReadRef<A, T>
81where
82    T: PartialOrd,
83{
84    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
85        (**self).partial_cmp(&**other)
86    }
87}
88
89impl<A, T> Ord for MappedReadRef<A, T>
90where
91    T: Ord,
92{
93    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
94        (**self).cmp(&**other)
95    }
96}
97
98impl<A, T> std::hash::Hash for MappedReadRef<A, T>
99where
100    T: std::hash::Hash,
101{
102    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
103        (**self).hash(state);
104    }
105}
106
107#[cfg(debug_assertions)]
108impl<A, T> ValueDebugFormat for MappedReadRef<A, T>
109where
110    T: ValueDebugFormat,
111{
112    fn value_debug_format(&self, depth: usize) -> ValueDebugFormatString<'_> {
113        let value = &**self;
114        value.value_debug_format(depth)
115    }
116}
117
118impl<A, T, I, J: Iterator<Item = I>> IntoIterator for &MappedReadRef<A, T>
119where
120    for<'b> &'b T: IntoIterator<Item = I, IntoIter = J>,
121{
122    type Item = I;
123
124    type IntoIter = J;
125
126    fn into_iter(self) -> Self::IntoIter {
127        (&**self).into_iter()
128    }
129}
130
131impl<A, T> Serialize for MappedReadRef<A, T>
132where
133    T: Serialize,
134{
135    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
136    where
137        S: serde::Serializer,
138    {
139        (**self).serialize(serializer)
140    }
141}