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