rustc_data_structures/
marker.rs

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