xref: /linux/rust/kernel/types.rs (revision 64fb810bce03a4e2b4d3ecbba04bb97da3536dd8)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Kernel types.
4 
5 use crate::ffi::c_void;
6 use core::{
7     cell::UnsafeCell,
8     marker::{PhantomData, PhantomPinned},
9     mem::{ManuallyDrop, MaybeUninit},
10     ops::{Deref, DerefMut},
11     ptr::NonNull,
12 };
13 use pin_init::{PinInit, Zeroable};
14 
15 /// Used to transfer ownership to and from foreign (non-Rust) languages.
16 ///
17 /// Ownership is transferred from Rust to a foreign language by calling [`Self::into_foreign`] and
18 /// later may be transferred back to Rust by calling [`Self::from_foreign`].
19 ///
20 /// This trait is meant to be used in cases when Rust objects are stored in C objects and
21 /// eventually "freed" back to Rust.
22 ///
23 /// # Safety
24 ///
25 /// - Implementations must satisfy the guarantees of [`Self::into_foreign`].
26 pub unsafe trait ForeignOwnable: Sized {
27     /// The alignment of pointers returned by `into_foreign`.
28     const FOREIGN_ALIGN: usize;
29 
30     /// Type used to immutably borrow a value that is currently foreign-owned.
31     type Borrowed<'a>;
32 
33     /// Type used to mutably borrow a value that is currently foreign-owned.
34     type BorrowedMut<'a>;
35 
36     /// Converts a Rust-owned object to a foreign-owned one.
37     ///
38     /// The foreign representation is a pointer to void. Aside from the guarantees listed below,
39     /// there are no other guarantees for this pointer. For example, it might be invalid, dangling
40     /// or pointing to uninitialized memory. Using it in any way except for [`from_foreign`],
41     /// [`try_from_foreign`], [`borrow`], or [`borrow_mut`] can result in undefined behavior.
42     ///
43     /// # Guarantees
44     ///
45     /// - Minimum alignment of returned pointer is [`Self::FOREIGN_ALIGN`].
46     /// - The returned pointer is not null.
47     ///
48     /// [`from_foreign`]: Self::from_foreign
49     /// [`try_from_foreign`]: Self::try_from_foreign
50     /// [`borrow`]: Self::borrow
51     /// [`borrow_mut`]: Self::borrow_mut
52     fn into_foreign(self) -> *mut c_void;
53 
54     /// Converts a foreign-owned object back to a Rust-owned one.
55     ///
56     /// # Safety
57     ///
58     /// The provided pointer must have been returned by a previous call to [`into_foreign`], and it
59     /// must not be passed to `from_foreign` more than once.
60     ///
61     /// [`into_foreign`]: Self::into_foreign
62     unsafe fn from_foreign(ptr: *mut c_void) -> Self;
63 
64     /// Tries to convert a foreign-owned object back to a Rust-owned one.
65     ///
66     /// A convenience wrapper over [`ForeignOwnable::from_foreign`] that returns [`None`] if `ptr`
67     /// is null.
68     ///
69     /// # Safety
70     ///
71     /// `ptr` must either be null or satisfy the safety requirements for [`from_foreign`].
72     ///
73     /// [`from_foreign`]: Self::from_foreign
74     unsafe fn try_from_foreign(ptr: *mut c_void) -> Option<Self> {
75         if ptr.is_null() {
76             None
77         } else {
78             // SAFETY: Since `ptr` is not null here, then `ptr` satisfies the safety requirements
79             // of `from_foreign` given the safety requirements of this function.
80             unsafe { Some(Self::from_foreign(ptr)) }
81         }
82     }
83 
84     /// Borrows a foreign-owned object immutably.
85     ///
86     /// This method provides a way to access a foreign-owned value from Rust immutably. It provides
87     /// you with exactly the same abilities as an `&Self` when the value is Rust-owned.
88     ///
89     /// # Safety
90     ///
91     /// The provided pointer must have been returned by a previous call to [`into_foreign`], and if
92     /// the pointer is ever passed to [`from_foreign`], then that call must happen after the end of
93     /// the lifetime `'a`.
94     ///
95     /// [`into_foreign`]: Self::into_foreign
96     /// [`from_foreign`]: Self::from_foreign
97     unsafe fn borrow<'a>(ptr: *mut c_void) -> Self::Borrowed<'a>;
98 
99     /// Borrows a foreign-owned object mutably.
100     ///
101     /// This method provides a way to access a foreign-owned value from Rust mutably. It provides
102     /// you with exactly the same abilities as an `&mut Self` when the value is Rust-owned, except
103     /// that the address of the object must not be changed.
104     ///
105     /// Note that for types like [`Arc`], an `&mut Arc<T>` only gives you immutable access to the
106     /// inner value, so this method also only provides immutable access in that case.
107     ///
108     /// In the case of `Box<T>`, this method gives you the ability to modify the inner `T`, but it
109     /// does not let you change the box itself. That is, you cannot change which allocation the box
110     /// points at.
111     ///
112     /// # Safety
113     ///
114     /// The provided pointer must have been returned by a previous call to [`into_foreign`], and if
115     /// the pointer is ever passed to [`from_foreign`], then that call must happen after the end of
116     /// the lifetime `'a`.
117     ///
118     /// The lifetime `'a` must not overlap with the lifetime of any other call to [`borrow`] or
119     /// `borrow_mut` on the same object.
120     ///
121     /// [`into_foreign`]: Self::into_foreign
122     /// [`from_foreign`]: Self::from_foreign
123     /// [`borrow`]: Self::borrow
124     /// [`Arc`]: crate::sync::Arc
125     unsafe fn borrow_mut<'a>(ptr: *mut c_void) -> Self::BorrowedMut<'a>;
126 }
127 
128 // SAFETY: The pointer returned by `into_foreign` comes from a well aligned
129 // pointer to `()`.
130 unsafe impl ForeignOwnable for () {
131     const FOREIGN_ALIGN: usize = core::mem::align_of::<()>();
132     type Borrowed<'a> = ();
133     type BorrowedMut<'a> = ();
134 
135     fn into_foreign(self) -> *mut c_void {
136         core::ptr::NonNull::dangling().as_ptr()
137     }
138 
139     unsafe fn from_foreign(_: *mut c_void) -> Self {}
140 
141     unsafe fn borrow<'a>(_: *mut c_void) -> Self::Borrowed<'a> {}
142     unsafe fn borrow_mut<'a>(_: *mut c_void) -> Self::BorrowedMut<'a> {}
143 }
144 
145 /// Runs a cleanup function/closure when dropped.
146 ///
147 /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running.
148 ///
149 /// # Examples
150 ///
151 /// In the example below, we have multiple exit paths and we want to log regardless of which one is
152 /// taken:
153 ///
154 /// ```
155 /// # use kernel::types::ScopeGuard;
156 /// fn example1(arg: bool) {
157 ///     let _log = ScopeGuard::new(|| pr_info!("example1 completed\n"));
158 ///
159 ///     if arg {
160 ///         return;
161 ///     }
162 ///
163 ///     pr_info!("Do something...\n");
164 /// }
165 ///
166 /// # example1(false);
167 /// # example1(true);
168 /// ```
169 ///
170 /// In the example below, we want to log the same message on all early exits but a different one on
171 /// the main exit path:
172 ///
173 /// ```
174 /// # use kernel::types::ScopeGuard;
175 /// fn example2(arg: bool) {
176 ///     let log = ScopeGuard::new(|| pr_info!("example2 returned early\n"));
177 ///
178 ///     if arg {
179 ///         return;
180 ///     }
181 ///
182 ///     // (Other early returns...)
183 ///
184 ///     log.dismiss();
185 ///     pr_info!("example2 no early return\n");
186 /// }
187 ///
188 /// # example2(false);
189 /// # example2(true);
190 /// ```
191 ///
192 /// In the example below, we need a mutable object (the vector) to be accessible within the log
193 /// function, so we wrap it in the [`ScopeGuard`]:
194 ///
195 /// ```
196 /// # use kernel::types::ScopeGuard;
197 /// fn example3(arg: bool) -> Result {
198 ///     let mut vec =
199 ///         ScopeGuard::new_with_data(KVec::new(), |v| pr_info!("vec had {} elements\n", v.len()));
200 ///
201 ///     vec.push(10u8, GFP_KERNEL)?;
202 ///     if arg {
203 ///         return Ok(());
204 ///     }
205 ///     vec.push(20u8, GFP_KERNEL)?;
206 ///     Ok(())
207 /// }
208 ///
209 /// # assert_eq!(example3(false), Ok(()));
210 /// # assert_eq!(example3(true), Ok(()));
211 /// ```
212 ///
213 /// # Invariants
214 ///
215 /// The value stored in the struct is nearly always `Some(_)`, except between
216 /// [`ScopeGuard::dismiss`] and [`ScopeGuard::drop`]: in this case, it will be `None` as the value
217 /// will have been returned to the caller. Since  [`ScopeGuard::dismiss`] consumes the guard,
218 /// callers won't be able to use it anymore.
219 pub struct ScopeGuard<T, F: FnOnce(T)>(Option<(T, F)>);
220 
221 impl<T, F: FnOnce(T)> ScopeGuard<T, F> {
222     /// Creates a new guarded object wrapping the given data and with the given cleanup function.
223     pub fn new_with_data(data: T, cleanup_func: F) -> Self {
224         // INVARIANT: The struct is being initialised with `Some(_)`.
225         Self(Some((data, cleanup_func)))
226     }
227 
228     /// Prevents the cleanup function from running and returns the guarded data.
229     pub fn dismiss(mut self) -> T {
230         // INVARIANT: This is the exception case in the invariant; it is not visible to callers
231         // because this function consumes `self`.
232         self.0.take().unwrap().0
233     }
234 }
235 
236 impl ScopeGuard<(), fn(())> {
237     /// Creates a new guarded object with the given cleanup function.
238     pub fn new(cleanup: impl FnOnce()) -> ScopeGuard<(), impl FnOnce(())> {
239         ScopeGuard::new_with_data((), move |()| cleanup())
240     }
241 }
242 
243 impl<T, F: FnOnce(T)> Deref for ScopeGuard<T, F> {
244     type Target = T;
245 
246     fn deref(&self) -> &T {
247         // The type invariants guarantee that `unwrap` will succeed.
248         &self.0.as_ref().unwrap().0
249     }
250 }
251 
252 impl<T, F: FnOnce(T)> DerefMut for ScopeGuard<T, F> {
253     fn deref_mut(&mut self) -> &mut T {
254         // The type invariants guarantee that `unwrap` will succeed.
255         &mut self.0.as_mut().unwrap().0
256     }
257 }
258 
259 impl<T, F: FnOnce(T)> Drop for ScopeGuard<T, F> {
260     fn drop(&mut self) {
261         // Run the cleanup function if one is still present.
262         if let Some((data, cleanup)) = self.0.take() {
263             cleanup(data)
264         }
265     }
266 }
267 
268 /// Stores an opaque value.
269 ///
270 /// [`Opaque<T>`] is meant to be used with FFI objects that are never interpreted by Rust code.
271 ///
272 /// It is used to wrap structs from the C side, like for example `Opaque<bindings::mutex>`.
273 /// It gets rid of all the usual assumptions that Rust has for a value:
274 ///
275 /// * The value is allowed to be uninitialized (for example have invalid bit patterns: `3` for a
276 ///   [`bool`]).
277 /// * The value is allowed to be mutated, when a `&Opaque<T>` exists on the Rust side.
278 /// * No uniqueness for mutable references: it is fine to have multiple `&mut Opaque<T>` point to
279 ///   the same value.
280 /// * The value is not allowed to be shared with other threads (i.e. it is `!Sync`).
281 ///
282 /// This has to be used for all values that the C side has access to, because it can't be ensured
283 /// that the C side is adhering to the usual constraints that Rust needs.
284 ///
285 /// Using [`Opaque<T>`] allows to continue to use references on the Rust side even for values shared
286 /// with C.
287 ///
288 /// # Examples
289 ///
290 /// ```
291 /// # #![expect(unreachable_pub, clippy::disallowed_names)]
292 /// use kernel::types::Opaque;
293 /// # // Emulate a C struct binding which is from C, maybe uninitialized or not, only the C side
294 /// # // knows.
295 /// # mod bindings {
296 /// #     pub struct Foo {
297 /// #         pub val: u8,
298 /// #     }
299 /// # }
300 ///
301 /// // `foo.val` is assumed to be handled on the C side, so we use `Opaque` to wrap it.
302 /// pub struct Foo {
303 ///     foo: Opaque<bindings::Foo>,
304 /// }
305 ///
306 /// impl Foo {
307 ///     pub fn get_val(&self) -> u8 {
308 ///         let ptr = Opaque::get(&self.foo);
309 ///
310 ///         // SAFETY: `Self` is valid from C side.
311 ///         unsafe { (*ptr).val }
312 ///     }
313 /// }
314 ///
315 /// // Create an instance of `Foo` with the `Opaque` wrapper.
316 /// let foo = Foo {
317 ///     foo: Opaque::new(bindings::Foo { val: 0xdb }),
318 /// };
319 ///
320 /// assert_eq!(foo.get_val(), 0xdb);
321 /// ```
322 #[repr(transparent)]
323 pub struct Opaque<T> {
324     value: UnsafeCell<MaybeUninit<T>>,
325     _pin: PhantomPinned,
326 }
327 
328 // SAFETY: `Opaque<T>` allows the inner value to be any bit pattern, including all zeros.
329 unsafe impl<T> Zeroable for Opaque<T> {}
330 
331 impl<T> Opaque<T> {
332     /// Creates a new opaque value.
333     pub const fn new(value: T) -> Self {
334         Self {
335             value: UnsafeCell::new(MaybeUninit::new(value)),
336             _pin: PhantomPinned,
337         }
338     }
339 
340     /// Creates an uninitialised value.
341     pub const fn uninit() -> Self {
342         Self {
343             value: UnsafeCell::new(MaybeUninit::uninit()),
344             _pin: PhantomPinned,
345         }
346     }
347 
348     /// Creates a new zeroed opaque value.
349     pub const fn zeroed() -> Self {
350         Self {
351             value: UnsafeCell::new(MaybeUninit::zeroed()),
352             _pin: PhantomPinned,
353         }
354     }
355 
356     /// Create an opaque pin-initializer from the given pin-initializer.
357     pub fn pin_init(slot: impl PinInit<T>) -> impl PinInit<Self> {
358         Self::ffi_init(|ptr: *mut T| {
359             // SAFETY:
360             //   - `ptr` is a valid pointer to uninitialized memory,
361             //   - `slot` is not accessed on error; the call is infallible,
362             //   - `slot` is pinned in memory.
363             let _ = unsafe { PinInit::<T>::__pinned_init(slot, ptr) };
364         })
365     }
366 
367     /// Creates a pin-initializer from the given initializer closure.
368     ///
369     /// The returned initializer calls the given closure with the pointer to the inner `T` of this
370     /// `Opaque`. Since this memory is uninitialized, the closure is not allowed to read from it.
371     ///
372     /// This function is safe, because the `T` inside of an `Opaque` is allowed to be
373     /// uninitialized. Additionally, access to the inner `T` requires `unsafe`, so the caller needs
374     /// to verify at that point that the inner value is valid.
375     pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
376         // SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
377         // initialize the `T`.
378         unsafe {
379             pin_init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
380                 init_func(Self::cast_into(slot));
381                 Ok(())
382             })
383         }
384     }
385 
386     /// Creates a fallible pin-initializer from the given initializer closure.
387     ///
388     /// The returned initializer calls the given closure with the pointer to the inner `T` of this
389     /// `Opaque`. Since this memory is uninitialized, the closure is not allowed to read from it.
390     ///
391     /// This function is safe, because the `T` inside of an `Opaque` is allowed to be
392     /// uninitialized. Additionally, access to the inner `T` requires `unsafe`, so the caller needs
393     /// to verify at that point that the inner value is valid.
394     pub fn try_ffi_init<E>(
395         init_func: impl FnOnce(*mut T) -> Result<(), E>,
396     ) -> impl PinInit<Self, E> {
397         // SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
398         // initialize the `T`.
399         unsafe {
400             pin_init::pin_init_from_closure::<_, E>(move |slot| init_func(Self::cast_into(slot)))
401         }
402     }
403 
404     /// Returns a raw pointer to the opaque data.
405     pub const fn get(&self) -> *mut T {
406         UnsafeCell::get(&self.value).cast::<T>()
407     }
408 
409     /// Gets the value behind `this`.
410     ///
411     /// This function is useful to get access to the value without creating intermediate
412     /// references.
413     pub const fn cast_into(this: *const Self) -> *mut T {
414         UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()
415     }
416 
417     /// The opposite operation of [`Opaque::cast_into`].
418     pub const fn cast_from(this: *const T) -> *const Self {
419         this.cast()
420     }
421 }
422 
423 /// Types that are _always_ reference counted.
424 ///
425 /// It allows such types to define their own custom ref increment and decrement functions.
426 /// Additionally, it allows users to convert from a shared reference `&T` to an owned reference
427 /// [`ARef<T>`].
428 ///
429 /// This is usually implemented by wrappers to existing structures on the C side of the code. For
430 /// Rust code, the recommendation is to use [`Arc`](crate::sync::Arc) to create reference-counted
431 /// instances of a type.
432 ///
433 /// # Safety
434 ///
435 /// Implementers must ensure that increments to the reference count keep the object alive in memory
436 /// at least until matching decrements are performed.
437 ///
438 /// Implementers must also ensure that all instances are reference-counted. (Otherwise they
439 /// won't be able to honour the requirement that [`AlwaysRefCounted::inc_ref`] keep the object
440 /// alive.)
441 pub unsafe trait AlwaysRefCounted {
442     /// Increments the reference count on the object.
443     fn inc_ref(&self);
444 
445     /// Decrements the reference count on the object.
446     ///
447     /// Frees the object when the count reaches zero.
448     ///
449     /// # Safety
450     ///
451     /// Callers must ensure that there was a previous matching increment to the reference count,
452     /// and that the object is no longer used after its reference count is decremented (as it may
453     /// result in the object being freed), unless the caller owns another increment on the refcount
454     /// (e.g., it calls [`AlwaysRefCounted::inc_ref`] twice, then calls
455     /// [`AlwaysRefCounted::dec_ref`] once).
456     unsafe fn dec_ref(obj: NonNull<Self>);
457 }
458 
459 /// An owned reference to an always-reference-counted object.
460 ///
461 /// The object's reference count is automatically decremented when an instance of [`ARef`] is
462 /// dropped. It is also automatically incremented when a new instance is created via
463 /// [`ARef::clone`].
464 ///
465 /// # Invariants
466 ///
467 /// The pointer stored in `ptr` is non-null and valid for the lifetime of the [`ARef`] instance. In
468 /// particular, the [`ARef`] instance owns an increment on the underlying object's reference count.
469 pub struct ARef<T: AlwaysRefCounted> {
470     ptr: NonNull<T>,
471     _p: PhantomData<T>,
472 }
473 
474 // SAFETY: It is safe to send `ARef<T>` to another thread when the underlying `T` is `Sync` because
475 // it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
476 // `T` to be `Send` because any thread that has an `ARef<T>` may ultimately access `T` using a
477 // mutable reference, for example, when the reference count reaches zero and `T` is dropped.
478 unsafe impl<T: AlwaysRefCounted + Sync + Send> Send for ARef<T> {}
479 
480 // SAFETY: It is safe to send `&ARef<T>` to another thread when the underlying `T` is `Sync`
481 // because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
482 // it needs `T` to be `Send` because any thread that has a `&ARef<T>` may clone it and get an
483 // `ARef<T>` on that thread, so the thread may ultimately access `T` using a mutable reference, for
484 // example, when the reference count reaches zero and `T` is dropped.
485 unsafe impl<T: AlwaysRefCounted + Sync + Send> Sync for ARef<T> {}
486 
487 impl<T: AlwaysRefCounted> ARef<T> {
488     /// Creates a new instance of [`ARef`].
489     ///
490     /// It takes over an increment of the reference count on the underlying object.
491     ///
492     /// # Safety
493     ///
494     /// Callers must ensure that the reference count was incremented at least once, and that they
495     /// are properly relinquishing one increment. That is, if there is only one increment, callers
496     /// must not use the underlying object anymore -- it is only safe to do so via the newly
497     /// created [`ARef`].
498     pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
499         // INVARIANT: The safety requirements guarantee that the new instance now owns the
500         // increment on the refcount.
501         Self {
502             ptr,
503             _p: PhantomData,
504         }
505     }
506 
507     /// Consumes the `ARef`, returning a raw pointer.
508     ///
509     /// This function does not change the refcount. After calling this function, the caller is
510     /// responsible for the refcount previously managed by the `ARef`.
511     ///
512     /// # Examples
513     ///
514     /// ```
515     /// use core::ptr::NonNull;
516     /// use kernel::types::{ARef, AlwaysRefCounted};
517     ///
518     /// struct Empty {}
519     ///
520     /// # // SAFETY: TODO.
521     /// unsafe impl AlwaysRefCounted for Empty {
522     ///     fn inc_ref(&self) {}
523     ///     unsafe fn dec_ref(_obj: NonNull<Self>) {}
524     /// }
525     ///
526     /// let mut data = Empty {};
527     /// let ptr = NonNull::<Empty>::new(&mut data).unwrap();
528     /// # // SAFETY: TODO.
529     /// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) };
530     /// let raw_ptr: NonNull<Empty> = ARef::into_raw(data_ref);
531     ///
532     /// assert_eq!(ptr, raw_ptr);
533     /// ```
534     pub fn into_raw(me: Self) -> NonNull<T> {
535         ManuallyDrop::new(me).ptr
536     }
537 }
538 
539 impl<T: AlwaysRefCounted> Clone for ARef<T> {
540     fn clone(&self) -> Self {
541         self.inc_ref();
542         // SAFETY: We just incremented the refcount above.
543         unsafe { Self::from_raw(self.ptr) }
544     }
545 }
546 
547 impl<T: AlwaysRefCounted> Deref for ARef<T> {
548     type Target = T;
549 
550     fn deref(&self) -> &Self::Target {
551         // SAFETY: The type invariants guarantee that the object is valid.
552         unsafe { self.ptr.as_ref() }
553     }
554 }
555 
556 impl<T: AlwaysRefCounted> From<&T> for ARef<T> {
557     fn from(b: &T) -> Self {
558         b.inc_ref();
559         // SAFETY: We just incremented the refcount above.
560         unsafe { Self::from_raw(NonNull::from(b)) }
561     }
562 }
563 
564 impl<T: AlwaysRefCounted> Drop for ARef<T> {
565     fn drop(&mut self) {
566         // SAFETY: The type invariants guarantee that the `ARef` owns the reference we're about to
567         // decrement.
568         unsafe { T::dec_ref(self.ptr) };
569     }
570 }
571 
572 /// A sum type that always holds either a value of type `L` or `R`.
573 ///
574 /// # Examples
575 ///
576 /// ```
577 /// use kernel::types::Either;
578 ///
579 /// let left_value: Either<i32, &str> = Either::Left(7);
580 /// let right_value: Either<i32, &str> = Either::Right("right value");
581 /// ```
582 pub enum Either<L, R> {
583     /// Constructs an instance of [`Either`] containing a value of type `L`.
584     Left(L),
585 
586     /// Constructs an instance of [`Either`] containing a value of type `R`.
587     Right(R),
588 }
589 
590 /// Zero-sized type to mark types not [`Send`].
591 ///
592 /// Add this type as a field to your struct if your type should not be sent to a different task.
593 /// Since [`Send`] is an auto trait, adding a single field that is `!Send` will ensure that the
594 /// whole type is `!Send`.
595 ///
596 /// If a type is `!Send` it is impossible to give control over an instance of the type to another
597 /// task. This is useful to include in types that store or reference task-local information. A file
598 /// descriptor is an example of such task-local information.
599 ///
600 /// This type also makes the type `!Sync`, which prevents immutable access to the value from
601 /// several threads in parallel.
602 pub type NotThreadSafe = PhantomData<*mut ()>;
603 
604 /// Used to construct instances of type [`NotThreadSafe`] similar to how `PhantomData` is
605 /// constructed.
606 ///
607 /// [`NotThreadSafe`]: type@NotThreadSafe
608 #[allow(non_upper_case_globals)]
609 pub const NotThreadSafe: NotThreadSafe = PhantomData;
610