rustc_data_structures/
marker.rs1use 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`")]
6pub 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`")]
13pub unsafe auto trait DynSync {}
17
18unsafe 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
27impls_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))]
51impl !DynSend for std::env::VarsOs {}
53
54macro_rules! already_send {
55 ($([$ty: ty])*) => {
56 $(unsafe impl DynSend for $ty where $ty: Send {})*
57 };
58}
59
60already_send!(
62 [std::backtrace::Backtrace][std::io::Stdout][std::io::Stderr][std::io::Error][std::fs::File]
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::LazyLock<T, F> where T: DynSend, F: DynSend]
79 [std::collections::HashSet<K, S> where K: DynSend, S: DynSend]
80 [std::collections::HashMap<K, V, S> where K: DynSend, V: DynSend, S: DynSend]
81 [std::collections::BTreeMap<K, V, A> where K: DynSend, V: DynSend, A: std::alloc::Allocator + Clone + DynSend]
82 [Vec<T, A> where T: DynSend, A: std::alloc::Allocator + DynSend]
83 [Box<T, A> where T: ?Sized + DynSend, A: std::alloc::Allocator + DynSend]
84 [crate::sync::RwLock<T> where T: DynSend]
85 [crate::tagged_ptr::TaggedRef<'a, P, T> where 'a, P: Sync, T: Send + crate::tagged_ptr::Tag]
86 [rustc_arena::TypedArena<T> where T: DynSend]
87 [hashbrown::HashTable<T> where T: DynSend]
88 [indexmap::IndexSet<V, S> where V: DynSend, S: DynSend]
89 [indexmap::IndexMap<K, V, S> where K: DynSend, V: DynSend, S: DynSend]
90 [thin_vec::ThinVec<T> where T: DynSend]
91 [smallvec::SmallVec<A> where A: smallvec::Array + DynSend]
92);
93
94macro_rules! impls_dyn_sync_neg {
95 ($([$t1: ty $(where $($generics1: tt)*)?])*) => {
96 $(impl$(<$($generics1)*>)? !DynSync for $t1 {})*
97 };
98}
99
100impls_dyn_sync_neg!(
102 [std::env::Args]
103 [std::env::ArgsOs]
104 [*const T where T: ?Sized + PointeeSized]
105 [*mut T where T: ?Sized + PointeeSized]
106 [std::cell::Cell<T> where T: ?Sized]
107 [std::cell::RefCell<T> where T: ?Sized]
108 [std::cell::UnsafeCell<T> where T: ?Sized]
109 [std::ptr::NonNull<T> where T: ?Sized + PointeeSized]
110 [std::rc::Rc<T, A> where T: ?Sized, A: Allocator]
111 [std::rc::Weak<T, A> where T: ?Sized, A: Allocator]
112 [std::cell::OnceCell<T> where T]
113 [std::sync::mpsc::Receiver<T> where T]
114 [std::sync::mpsc::Sender<T> where T]
115);
116
117#[cfg(any(
118 unix,
119 target_os = "hermit",
120 all(target_vendor = "fortanix", target_env = "sgx"),
121 target_os = "solid_asp3",
122 target_os = "wasi",
123 target_os = "xous"
124))]
125impl !DynSync for std::env::VarsOs {}
127
128macro_rules! already_sync {
129 ($([$ty: ty])*) => {
130 $(unsafe impl DynSync for $ty where $ty: Sync {})*
131 };
132}
133
134already_sync!(
136 [std::sync::atomic::AtomicBool][std::sync::atomic::AtomicUsize][std::sync::atomic::AtomicU8]
137 [std::sync::atomic::AtomicU32][std::backtrace::Backtrace][std::io::Error][std::fs::File]
138 [jobserver_crate::Client][jobserver_crate::HelperThread][crate::memmap::Mmap]
139 [crate::profiling::SelfProfiler][crate::owned_slice::OwnedSlice]
140);
141
142#[cfg(target_has_atomic = "64")]
144already_sync!([std::sync::atomic::AtomicU64]);
145
146#[cfg(not(target_has_atomic = "64"))]
147already_sync!([portable_atomic::AtomicU64]);
148
149macro_rules! impl_dyn_sync {
150 ($($($attr: meta)* [$ty: ty where $($generics2: tt)*])*) => {
151 $(unsafe impl<$($generics2)*> DynSync for $ty {})*
152 };
153}
154
155impl_dyn_sync!(
156 [std::sync::atomic::AtomicPtr<T> where T]
157 [std::sync::OnceLock<T> where T: DynSend + DynSync]
158 [std::sync::Mutex<T> where T: ?Sized + DynSend]
159 [std::sync::Arc<T> where T: ?Sized + DynSync + DynSend]
160 [std::sync::LazyLock<T, F> where T: DynSend + DynSync, F: DynSend]
161 [std::collections::HashSet<K, S> where K: DynSync, S: DynSync]
162 [std::collections::HashMap<K, V, S> where K: DynSync, V: DynSync, S: DynSync]
163 [std::collections::BTreeMap<K, V, A> where K: DynSync, V: DynSync, A: std::alloc::Allocator + Clone + DynSync]
164 [Vec<T, A> where T: DynSync, A: std::alloc::Allocator + DynSync]
165 [Box<T, A> where T: ?Sized + DynSync, A: std::alloc::Allocator + DynSync]
166 [crate::sync::RwLock<T> where T: DynSend + DynSync]
167 [crate::sync::WorkerLocal<T> where T: DynSend]
168 [crate::intern::Interned<'a, T> where 'a, T: DynSync]
169 [crate::tagged_ptr::TaggedRef<'a, P, T> where 'a, P: Sync, T: Sync + crate::tagged_ptr::Tag]
170 [parking_lot::lock_api::Mutex<R, T> where R: DynSync, T: ?Sized + DynSend]
171 [parking_lot::lock_api::RwLock<R, T> where R: DynSync, T: ?Sized + DynSend + DynSync]
172 [hashbrown::HashTable<T> where T: DynSync]
173 [indexmap::IndexSet<V, S> where V: DynSync, S: DynSync]
174 [indexmap::IndexMap<K, V, S> where K: DynSync, V: DynSync, S: DynSync]
175 [smallvec::SmallVec<A> where A: smallvec::Array + DynSync]
176 [thin_vec::ThinVec<T> where T: DynSync]
177);
178
179pub fn assert_dyn_sync<T: ?Sized + PointeeSized + DynSync>() {}
180pub fn assert_dyn_send<T: ?Sized + PointeeSized + DynSend>() {}
181pub fn assert_dyn_send_val<T: ?Sized + PointeeSized + DynSend>(_t: &T) {}
182pub fn assert_dyn_send_sync_val<T: ?Sized + PointeeSized + DynSync + DynSend>(_t: &T) {}
183
184#[derive(Copy, Clone)]
185pub struct FromDyn<T>(T);
186
187impl<T> FromDyn<T> {
188 #[inline(always)]
189 pub fn from(val: T) -> Self {
190 assert!(crate::sync::is_dyn_thread_safe());
194 FromDyn(val)
195 }
196
197 #[inline(always)]
198 pub fn derive<O>(&self, val: O) -> FromDyn<O> {
199 FromDyn(val)
201 }
202
203 #[inline(always)]
204 pub fn into_inner(self) -> T {
205 self.0
206 }
207}
208
209unsafe impl<T: DynSend> Send for FromDyn<T> {}
211
212unsafe impl<T: DynSync> Sync for FromDyn<T> {}
214
215impl<T> std::ops::Deref for FromDyn<T> {
216 type Target = T;
217
218 #[inline(always)]
219 fn deref(&self) -> &Self::Target {
220 &self.0
221 }
222}
223
224impl<T> std::ops::DerefMut for FromDyn<T> {
225 #[inline(always)]
226 fn deref_mut(&mut self) -> &mut Self::Target {
227 &mut self.0
228 }
229}
230
231#[derive(Copy, Clone)]
235pub struct IntoDynSyncSend<T: ?Sized + PointeeSized>(pub T);
236
237unsafe impl<T: ?Sized + PointeeSized + Send> DynSend for IntoDynSyncSend<T> {}
238unsafe impl<T: ?Sized + PointeeSized + Sync> DynSync for IntoDynSyncSend<T> {}
239
240impl<T> std::ops::Deref for IntoDynSyncSend<T> {
241 type Target = T;
242
243 #[inline(always)]
244 fn deref(&self) -> &T {
245 &self.0
246 }
247}
248
249impl<T> std::ops::DerefMut for IntoDynSyncSend<T> {
250 #[inline(always)]
251 fn deref_mut(&mut self) -> &mut T {
252 &mut self.0
253 }
254}