turbo_tasks/
mapped_read_ref.rs

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