rustc_data_structures/
marker.rs

1use std::alloc::Allocator;
2use std::marker::PointeeSized;
3
4#[diagnostic::on_unimplemented(message = "`{Self}` doesn't implement `DynSend`. \
5            Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Send`")]
6// This is an auto trait for types which can be sent across threads if `sync::is_dyn_thread_safe()`
7// is true. These types can be wrapped in a `FromDyn` to get a `Send` type. Wrapping a
8// `Send` type in `IntoDynSyncSend` will create a `DynSend` type.
9pub unsafe auto trait DynSend {}
10
11#[diagnostic::on_unimplemented(message = "`{Self}` doesn't implement `DynSync`. \
12            Add it to `rustc_data_structures::marker` or use `IntoDynSyncSend` if it's already `Sync`")]
13// This is an auto trait for types which can be shared across threads if `sync::is_dyn_thread_safe()`
14// is true. These types can be wrapped in a `FromDyn` to get a `Sync` type. Wrapping a
15// `Sync` type in `IntoDynSyncSend` will create a `DynSync` type.
16pub unsafe auto trait DynSync {}
17
18// Same with `Sync` and `Send`.
19unsafe impl<T: DynSync + ?Sized + PointeeSized> DynSend for &T {}
20
21macro_rules! impls_dyn_send_neg {
22    ($([$t1: ty $(where $($generics1: tt)*)?])*) => {
23        $(impl$(<$($generics1)*>)? !DynSend for $t1 {})*
24    };
25}
26
27// Consistent with `std`
28impls_dyn_send_neg!(
29    [std::env::Args]
30    [std::env::ArgsOs]
31    [*const T where T: ?Sized + PointeeSized]
32    [*mut T where T: ?Sized + PointeeSized]
33    [std::ptr::NonNull<T> where T: ?Sized + PointeeSized]
34    [std::rc::Rc<T, A> where T: ?Sized, A: Allocator]
35    [std::rc::Weak<T, A> where T: ?Sized, A: Allocator]
36    [std::sync::MutexGuard<'_, T> where T: ?Sized]
37    [std::sync::RwLockReadGuard<'_, T> where T: ?Sized]
38    [std::sync::RwLockWriteGuard<'_, T> where T: ?Sized]
39    [std::io::StdoutLock<'_>]
40    [std::io::StderrLock<'_>]
41);
42
43#[cfg(any(
44    unix,
45    target_os = "hermit",
46    all(target_vendor = "fortanix", target_env = "sgx"),
47    target_os = "solid_asp3",
48    target_os = "wasi",
49    target_os = "xous"
50))]
51// Consistent with `std`, `env_imp::Env` is `!Sync` in these platforms
52impl !DynSend for std::env::VarsOs {}
53
54macro_rules! already_send {
55    ($([$ty: ty])*) => {
56        $(unsafe impl DynSend for $ty where Self: Send {})*
57    };
58}
59
60// These structures are already `Send`.
61already_send!(
62    [std::backtrace::Backtrace][std::io::Stdout][std::io::Stderr][std::io::Error][std::fs::File][std::panic::Location<'_>]
63        [rustc_arena::DroplessArena][jobserver_crate::Client][jobserver_crate::HelperThread]
64        [crate::memmap::Mmap][crate::profiling::SelfProfiler][crate::owned_slice::OwnedSlice]
65);
66
67macro_rules! impl_dyn_send {
68    ($($($attr: meta)* [$ty: ty where $($generics2: tt)*])*) => {
69        $(unsafe impl<$($generics2)*> DynSend for $ty {})*
70    };
71}
72
73impl_dyn_send!(
74    [std::sync::atomic::AtomicPtr<T> where T]
75    [std::sync::Mutex<T> where T: ?Sized+ DynSend]
76    [std::sync::mpsc::Sender<T> where T: DynSend]
77    [std::sync::Arc<T> where T: ?Sized + DynSync + DynSend]
78    [std::sync::Weak<T> where T: ?Sized + DynSync + DynSend]
79    [std::sync::LazyLock<T, F> where T: DynSend, F: DynSend]
80    [std::collections::HashSet<K, S> where K: DynSend, S: DynSend]
81    [std::collections::HashMap<K, V, S> where K: DynSend, V: DynSend, S: DynSend]
82    [std::collections::BTreeMap<K, V, A> where K: DynSend, V: DynSend, A: std::alloc::Allocator + Clone + DynSend]
83    [Vec<T, A> where T: DynSend, A: std::alloc::Allocator + DynSend]
84    [Box<T, A> where T: ?Sized + DynSend, A: std::alloc::Allocator + DynSend]
85    [crate::sync::RwLock<T> where T: DynSend]
86    [crate::tagged_ptr::TaggedRef<'a, P, T> where 'a, P: Sync, T: Send + crate::tagged_ptr::Tag]
87    [rustc_arena::TypedArena<T> where T: DynSend]
88    [hashbrown::HashTable<T> where T: DynSend]
89    [indexmap::IndexSet<V, S> where V: DynSend, S: DynSend]
90    [indexmap::IndexMap<K, V, S> where K: DynSend, V: DynSend, S: DynSend]
91    [thin_vec::ThinVec<T> where T: DynSend]
92    [smallvec::SmallVec<A> where A: smallvec::Array + DynSend]
93);
94
95macro_rules! impls_dyn_sync_neg {
96    ($([$t1: ty $(where $($generics1: tt)*)?])*) => {
97        $(impl$(<$($generics1)*>)? !DynSync for $t1 {})*
98    };
99}
100
101// Consistent with `std`
102impls_dyn_sync_neg!(
103    [std::env::Args]
104    [std::env::ArgsOs]
105    [*const T where T: ?Sized + PointeeSized]
106    [*mut T where T: ?Sized + PointeeSized]
107    [std::cell::Cell<T> where T: ?Sized]
108    [std::cell::RefCell<T> where T: ?Sized]
109    [std::cell::UnsafeCell<T> where T: ?Sized]
110    [std::ptr::NonNull<T> where T: ?Sized + PointeeSized]
111    [std::rc::Rc<T, A> where T: ?Sized, A: Allocator]
112    [std::rc::Weak<T, A> where T: ?Sized, A: Allocator]
113    [std::cell::OnceCell<T> where T]
114    [std::sync::mpsc::Receiver<T> where T]
115    [std::sync::mpsc::Sender<T> where T]
116);
117
118#[cfg(any(
119    unix,
120    target_os = "hermit",
121    all(target_vendor = "fortanix", target_env = "sgx"),
122    target_os = "solid_asp3",
123    target_os = "wasi",
124    target_os = "xous"
125))]
126// Consistent with `std`, `env_imp::Env` is `!Sync` in these platforms
127impl !DynSync for std::env::VarsOs {}
128
129macro_rules! already_sync {
130    ($([$ty: ty])*) => {
131        $(unsafe impl DynSync for $ty where Self: Sync {})*
132    };
133}
134
135// These structures are already `Sync`.
136already_sync!(
137    [std::sync::atomic::AtomicBool][std::sync::atomic::AtomicUsize][std::sync::atomic::AtomicU8]
138        [std::sync::atomic::AtomicU32][std::backtrace::Backtrace][std::io::Error][std::fs::File][std::panic::Location<'_>]
139        [jobserver_crate::Client][jobserver_crate::HelperThread][crate::memmap::Mmap]
140        [crate::profiling::SelfProfiler][crate::owned_slice::OwnedSlice]
141);
142
143// Use portable AtomicU64 for targets without native 64-bit atomics
144#[cfg(target_has_atomic = "64")]
145already_sync!([std::sync::atomic::AtomicU64]);
146
147#[cfg(not(target_has_atomic = "64"))]
148already_sync!([portable_atomic::AtomicU64]);
149
150macro_rules! impl_dyn_sync {
151    ($($($attr: meta)* [$ty: ty where $($generics2: tt)*])*) => {
152        $(unsafe impl<$($generics2)*> DynSync for $ty {})*
153    };
154}
155
156impl_dyn_sync!(
157    [std::sync::atomic::AtomicPtr<T> where T]
158    [std::sync::OnceLock<T> where T: DynSend + DynSync]
159    [std::sync::Mutex<T> where T: ?Sized + DynSend]
160    [std::sync::Arc<T> where T: ?Sized + DynSync + DynSend]
161    [std::sync::Weak<T> where T: ?Sized + DynSync + DynSend]
162    [std::sync::LazyLock<T, F> where T: DynSend + DynSync, F: DynSend]
163    [std::collections::HashSet<K, S> where K: DynSync, S: DynSync]
164    [std::collections::HashMap<K, V, S> where K: DynSync, V: DynSync, S: DynSync]
165    [std::collections::BTreeMap<K, V, A> where K: DynSync, V: DynSync, A: std::alloc::Allocator + Clone + DynSync]
166    [Vec<T, A> where T: DynSync, A: std::alloc::Allocator + DynSync]
167    [Box<T, A> where T: ?Sized + DynSync, A: std::alloc::Allocator + DynSync]
168    [crate::sync::RwLock<T> where T: DynSend + DynSync]
169    [crate::sync::WorkerLocal<T> where T: DynSend]
170    [crate::intern::Interned<'a, T> where 'a, T: DynSync]
171    [crate::tagged_ptr::TaggedRef<'a, P, T> where 'a, P: Sync, T: Sync + crate::tagged_ptr::Tag]
172    [parking_lot::lock_api::Mutex<R, T> where R: DynSync, T: ?Sized + DynSend]
173    [parking_lot::lock_api::RwLock<R, T> where R: DynSync, T: ?Sized + DynSend + DynSync]
174    [hashbrown::HashTable<T> where T: DynSync]
175    [indexmap::IndexSet<V, S> where V: DynSync, S: DynSync]
176    [indexmap::IndexMap<K, V, S> where K: DynSync, V: DynSync, S: DynSync]
177    [smallvec::SmallVec<A> where A: smallvec::Array + DynSync]
178    [thin_vec::ThinVec<T> where T: DynSync]
179);
180
181pub fn assert_dyn_sync<T: ?Sized + PointeeSized + DynSync>() {}
182pub fn assert_dyn_send<T: ?Sized + PointeeSized + DynSend>() {}
183pub fn assert_dyn_send_val<T: ?Sized + PointeeSized + DynSend>(_t: &T) {}
184pub fn assert_dyn_send_sync_val<T: ?Sized + PointeeSized + DynSync + DynSend>(_t: &T) {}
185
186#[derive(Copy, Clone)]
187pub struct FromDyn<T>(T);
188
189impl<T> FromDyn<T> {
190    #[inline(always)]
191    pub fn from(val: T) -> Self {
192        // Check that `sync::is_dyn_thread_safe()` is true on creation so we can
193        // implement `Send` and `Sync` for this structure when `T`
194        // implements `DynSend` and `DynSync` respectively.
195        assert!(crate::sync::is_dyn_thread_safe());
196        FromDyn(val)
197    }
198
199    #[inline(always)]
200    pub fn derive<O>(&self, val: O) -> FromDyn<O> {
201        // We already did the check for `sync::is_dyn_thread_safe()` when creating `Self`
202        FromDyn(val)
203    }
204
205    #[inline(always)]
206    pub fn into_inner(self) -> T {
207        self.0
208    }
209}
210
211// `FromDyn` is `Send` if `T` is `DynSend`, since it ensures that sync::is_dyn_thread_safe() is true.
212unsafe impl<T: DynSend> Send for FromDyn<T> {}
213
214// `FromDyn` is `Sync` if `T` is `DynSync`, since it ensures that sync::is_dyn_thread_safe() is true.
215unsafe impl<T: DynSync> Sync for FromDyn<T> {}
216
217impl<T> std::ops::Deref for FromDyn<T> {
218    type Target = T;
219
220    #[inline(always)]
221    fn deref(&self) -> &Self::Target {
222        &self.0
223    }
224}
225
226impl<T> std::ops::DerefMut for FromDyn<T> {
227    #[inline(always)]
228    fn deref_mut(&mut self) -> &mut Self::Target {
229        &mut self.0
230    }
231}
232
233// A wrapper to convert a struct that is already a `Send` or `Sync` into
234// an instance of `DynSend` and `DynSync`, since the compiler cannot infer
235// it automatically in some cases. (e.g. Box<dyn Send / Sync>)
236#[derive(Copy, Clone)]
237pub struct IntoDynSyncSend<T: ?Sized + PointeeSized>(pub T);
238
239unsafe impl<T: ?Sized + PointeeSized + Send> DynSend for IntoDynSyncSend<T> {}
240unsafe impl<T: ?Sized + PointeeSized + Sync> DynSync for IntoDynSyncSend<T> {}
241
242impl<T> std::ops::Deref for IntoDynSyncSend<T> {
243    type Target = T;
244
245    #[inline(always)]
246    fn deref(&self) -> &T {
247        &self.0
248    }
249}
250
251impl<T> std::ops::DerefMut for IntoDynSyncSend<T> {
252    #[inline(always)]
253    fn deref_mut(&mut self) -> &mut T {
254        &mut self.0
255    }
256}