Skip to main content

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`
28impl !DynSend for std::io::StderrLock<'_> {}impls_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`.
61unsafe impl DynSend for rustc_serialize::opaque::FileEncoder<'_> where
    Self: Send {}already_send!(
62    [std::sync::atomic::AtomicBool][std::sync::atomic::AtomicUsize][std::sync::atomic::AtomicU8]
63        [std::sync::atomic::AtomicU32][std::backtrace::Backtrace][std::io::Stdout][std::io::Stderr]
64        [std::io::Error][std::fs::File][std::panic::Location<'_>][rustc_arena::DroplessArena]
65        [jobserver_crate::Client][jobserver_crate::HelperThread][crate::memmap::Mmap]
66        [crate::profiling::SelfProfiler][crate::owned_slice::OwnedSlice]
67        [rustc_serialize::opaque::FileEncoder<'_>]
68);
69
70#[cfg(target_has_atomic = "64")]
71unsafe impl DynSend for std::sync::atomic::AtomicU64 where Self: Send {}already_send!([std::sync::atomic::AtomicU64]);
72
73macro_rules! impl_dyn_send {
74    ($($($attr: meta)* [$ty: ty where $($generics2: tt)*])*) => {
75        $(unsafe impl<$($generics2)*> DynSend for $ty {})*
76    };
77}
78
79unsafe impl<A: smallvec::Array + DynSend> DynSend for smallvec::SmallVec<A> {}impl_dyn_send!(
80    [std::sync::atomic::AtomicPtr<T> where T]
81    [std::sync::Mutex<T> where T: ?Sized+ DynSend]
82    [std::sync::mpsc::Sender<T> where T: DynSend]
83    [std::sync::Arc<T> where T: ?Sized + DynSync + DynSend]
84    [std::sync::Weak<T> where T: ?Sized + DynSync + DynSend]
85    [std::sync::LazyLock<T, F> where T: DynSend, F: DynSend]
86    [std::collections::HashSet<K, S> where K: DynSend, S: DynSend]
87    [std::collections::HashMap<K, V, S> where K: DynSend, V: DynSend, S: DynSend]
88    [std::collections::BTreeMap<K, V, A> where K: DynSend, V: DynSend, A: std::alloc::Allocator + Clone + DynSend]
89    [Vec<T, A> where T: DynSend, A: std::alloc::Allocator + DynSend]
90    [Box<T, A> where T: ?Sized + DynSend, A: std::alloc::Allocator + DynSend]
91    [crate::sync::RwLock<T> where T: DynSend]
92    [crate::tagged_ptr::TaggedRef<'a, P, T> where 'a, P: Sync, T: Send + crate::tagged_ptr::Tag]
93    [rustc_arena::TypedArena<T> where T: DynSend]
94    [hashbrown::HashTable<T> where T: DynSend]
95    [indexmap::IndexSet<V, S> where V: DynSend, S: DynSend]
96    [indexmap::IndexMap<K, V, S> where K: DynSend, V: DynSend, S: DynSend]
97    [thin_vec::ThinVec<T> where T: DynSend]
98    [smallvec::SmallVec<A> where A: smallvec::Array + DynSend]
99);
100
101macro_rules! impls_dyn_sync_neg {
102    ($([$t1: ty $(where $($generics1: tt)*)?])*) => {
103        $(impl$(<$($generics1)*>)? !DynSync for $t1 {})*
104    };
105}
106
107// Consistent with `std`
108impl<T> !DynSync for std::sync::mpsc::Sender<T> {}impls_dyn_sync_neg!(
109    [std::env::Args]
110    [std::env::ArgsOs]
111    [*const T where T: ?Sized + PointeeSized]
112    [*mut T where T: ?Sized + PointeeSized]
113    [std::cell::Cell<T> where T: ?Sized]
114    [std::cell::RefCell<T> where T: ?Sized]
115    [std::cell::UnsafeCell<T> where T: ?Sized]
116    [std::ptr::NonNull<T> where T: ?Sized + PointeeSized]
117    [std::rc::Rc<T, A> where T: ?Sized, A: Allocator]
118    [std::rc::Weak<T, A> where T: ?Sized, A: Allocator]
119    [std::cell::OnceCell<T> where T]
120    [std::sync::mpsc::Receiver<T> where T]
121    [std::sync::mpsc::Sender<T> where T]
122);
123
124#[cfg(any(
125    unix,
126    target_os = "hermit",
127    all(target_vendor = "fortanix", target_env = "sgx"),
128    target_os = "solid_asp3",
129    target_os = "wasi",
130    target_os = "xous"
131))]
132// Consistent with `std`, `env_imp::Env` is `!Sync` in these platforms
133impl !DynSync for std::env::VarsOs {}
134
135macro_rules! already_sync {
136    ($([$ty: ty])*) => {
137        $(unsafe impl DynSync for $ty where Self: Sync {})*
138    };
139}
140
141// These structures are already `Sync`.
142unsafe impl DynSync for crate::owned_slice::OwnedSlice where Self: Sync {}already_sync!(
143    [std::sync::atomic::AtomicBool][std::sync::atomic::AtomicUsize][std::sync::atomic::AtomicU8]
144        [std::sync::atomic::AtomicU32][std::backtrace::Backtrace][std::io::Error][std::fs::File][std::panic::Location<'_>]
145        [jobserver_crate::Client][jobserver_crate::HelperThread][crate::memmap::Mmap]
146        [crate::profiling::SelfProfiler][crate::owned_slice::OwnedSlice]
147);
148
149// Use portable AtomicU64 for targets without native 64-bit atomics
150#[cfg(target_has_atomic = "64")]
151unsafe impl DynSync for std::sync::atomic::AtomicU64 where Self: Sync {}already_sync!([std::sync::atomic::AtomicU64]);
152
153#[cfg(not(target_has_atomic = "64"))]
154already_sync!([portable_atomic::AtomicU64]);
155
156macro_rules! impl_dyn_sync {
157    ($($($attr: meta)* [$ty: ty where $($generics2: tt)*])*) => {
158        $(unsafe impl<$($generics2)*> DynSync for $ty {})*
159    };
160}
161
162unsafe impl<T: DynSync> DynSync for thin_vec::ThinVec<T> {}impl_dyn_sync!(
163    [std::sync::atomic::AtomicPtr<T> where T]
164    [std::sync::OnceLock<T> where T: DynSend + DynSync]
165    [std::sync::Mutex<T> where T: ?Sized + DynSend]
166    [std::sync::Arc<T> where T: ?Sized + DynSync + DynSend]
167    [std::sync::Weak<T> where T: ?Sized + DynSync + DynSend]
168    [std::sync::LazyLock<T, F> where T: DynSend + DynSync, F: DynSend]
169    [std::collections::HashSet<K, S> where K: DynSync, S: DynSync]
170    [std::collections::HashMap<K, V, S> where K: DynSync, V: DynSync, S: DynSync]
171    [std::collections::BTreeMap<K, V, A> where K: DynSync, V: DynSync, A: std::alloc::Allocator + Clone + DynSync]
172    [Vec<T, A> where T: DynSync, A: std::alloc::Allocator + DynSync]
173    [Box<T, A> where T: ?Sized + DynSync, A: std::alloc::Allocator + DynSync]
174    [crate::sync::RwLock<T> where T: DynSend + DynSync]
175    [crate::sync::WorkerLocal<T> where T: DynSend]
176    [crate::intern::Interned<'a, T> where 'a, T: DynSync]
177    [crate::tagged_ptr::TaggedRef<'a, P, T> where 'a, P: Sync, T: Sync + crate::tagged_ptr::Tag]
178    [parking_lot::lock_api::Mutex<R, T> where R: DynSync, T: ?Sized + DynSend]
179    [parking_lot::lock_api::RwLock<R, T> where R: DynSync, T: ?Sized + DynSend + DynSync]
180    [hashbrown::HashTable<T> where T: DynSync]
181    [indexmap::IndexSet<V, S> where V: DynSync, S: DynSync]
182    [indexmap::IndexMap<K, V, S> where K: DynSync, V: DynSync, S: DynSync]
183    [smallvec::SmallVec<A> where A: smallvec::Array + DynSync]
184    [thin_vec::ThinVec<T> where T: DynSync]
185);
186
187pub fn assert_dyn_sync<T: ?Sized + PointeeSized + DynSync>() {}
188pub fn assert_dyn_send<T: ?Sized + PointeeSized + DynSend>() {}
189pub fn assert_dyn_send_val<T: ?Sized + PointeeSized + DynSend>(_t: &T) {}
190pub fn assert_dyn_send_sync_val<T: ?Sized + PointeeSized + DynSync + DynSend>(_t: &T) {}
191
192// A wrapper to convert a struct that is already a `Send` or `Sync` into
193// an instance of `DynSend` and `DynSync`, since the compiler cannot infer
194// it automatically in some cases. (e.g. Box<dyn Send / Sync>)
195#[derive(#[automatically_derived]
impl<T: ::core::marker::Copy + ?Sized + PointeeSized> ::core::marker::Copy for
    IntoDynSyncSend<T> {
}Copy, #[automatically_derived]
impl<T: ::core::clone::Clone + ?Sized + PointeeSized> ::core::clone::Clone for
    IntoDynSyncSend<T> {
    #[inline]
    fn clone(&self) -> IntoDynSyncSend<T> {
        IntoDynSyncSend(::core::clone::Clone::clone(&self.0))
    }
}Clone)]
196pub struct IntoDynSyncSend<T: ?Sized + PointeeSized>(pub T);
197
198unsafe impl<T: ?Sized + PointeeSized + Send> DynSend for IntoDynSyncSend<T> {}
199unsafe impl<T: ?Sized + PointeeSized + Sync> DynSync for IntoDynSyncSend<T> {}
200
201impl<T> std::ops::Deref for IntoDynSyncSend<T> {
202    type Target = T;
203
204    #[inline(always)]
205    fn deref(&self) -> &T {
206        &self.0
207    }
208}
209
210impl<T> std::ops::DerefMut for IntoDynSyncSend<T> {
211    #[inline(always)]
212    fn deref_mut(&mut self) -> &mut T {
213        &mut self.0
214    }
215}