Skip to main content

turbo_tasks/
mapped_read_ref.rs

1use std::fmt::{Debug, Display};
2
3use serde::Serialize;
4
5use crate::{
6    debug::{ValueDebugFormat, ValueDebugFormatString},
7    trace::{TraceRawVcs, TraceRawVcsContext},
8};
9
10pub struct MappedReadRef<A, T> {
11    value: *const T,
12    arc: triomphe::Arc<A>,
13}
14
15unsafe impl<A: Send + Sync, T: Sync> Send for MappedReadRef<A, T> {}
16unsafe impl<A: Send + Sync, T: Sync> Sync for MappedReadRef<A, T> {}
17
18impl<A, T> MappedReadRef<A, T> {
19    /// # Safety
20    /// The caller must ensure that the `arc` keeps the value pointed to by `value` alive.
21    pub unsafe fn new(arc: triomphe::Arc<A>, value: *const T) -> Self {
22        Self { value, arc }
23    }
24}
25
26impl<A, T> MappedReadRef<A, T> {
27    pub fn ptr_eq(&self, other: &Self) -> bool {
28        std::ptr::eq(self.value, other.value)
29    }
30
31    pub fn ptr(&self) -> *const T {
32        self.value
33    }
34}
35
36impl<A, T> Clone for MappedReadRef<A, T> {
37    fn clone(&self) -> Self {
38        Self {
39            value: self.value,
40            arc: self.arc.clone(),
41        }
42    }
43}
44
45impl<A, T> std::ops::Deref for MappedReadRef<A, T> {
46    type Target = T;
47
48    fn deref(&self) -> &Self::Target {
49        unsafe { &*self.value }
50    }
51}
52
53impl<A, T> Debug for MappedReadRef<A, T>
54where
55    T: Debug,
56{
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        (**self).fmt(f)
59    }
60}
61
62impl<A, T> Display for MappedReadRef<A, T>
63where
64    T: Display,
65{
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        Display::fmt(&**self, f)
68    }
69}
70
71impl<A, T> PartialEq for MappedReadRef<A, T>
72where
73    T: PartialEq,
74{
75    fn eq(&self, other: &Self) -> bool {
76        **self == **other
77    }
78}
79
80impl<A, T> Eq for MappedReadRef<A, T> where T: Eq {}
81
82impl<A, T> PartialOrd for MappedReadRef<A, T>
83where
84    T: PartialOrd,
85{
86    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
87        (**self).partial_cmp(&**other)
88    }
89}
90
91impl<A, T> Ord for MappedReadRef<A, T>
92where
93    T: Ord,
94{
95    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
96        (**self).cmp(&**other)
97    }
98}
99
100impl<A, T> std::hash::Hash for MappedReadRef<A, T>
101where
102    T: std::hash::Hash,
103{
104    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
105        (**self).hash(state);
106    }
107}
108
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}