|
| 1 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 | + |
| 3 | +//! Rust standard library vendored code. |
| 4 | +//! |
| 5 | +//! The contents of this file come from the Rust standard library, hosted in |
| 6 | +//! the <https://github.com/rust-lang/rust> repository, licensed under |
| 7 | +//! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details, |
| 8 | +//! see <https://github.com/rust-lang/rust/blob/master/COPYRIGHT>. |
| 9 | +//! |
| 10 | +//! This file provides a implementation of a subset of the upstream rust |
| 11 | +//! `UnsafePinned` type for rust versions that don't include this type. |
| 12 | +
|
| 13 | +use core::{cell::UnsafeCell, marker::PhantomPinned}; |
| 14 | + |
| 15 | +/// This type provides a way to opt-out of typical aliasing rules; |
| 16 | +/// specifically, `&mut UnsafePinned<T>` is not guaranteed to be a unique pointer. |
| 17 | +/// |
| 18 | +/// However, even if you define your type like `pub struct Wrapper(UnsafePinned<...>)`, it is still |
| 19 | +/// very risky to have an `&mut Wrapper` that aliases anything else. Many functions that work |
| 20 | +/// generically on `&mut T` assume that the memory that stores `T` is uniquely owned (such as |
| 21 | +/// `mem::swap`). In other words, while having aliasing with `&mut Wrapper` is not immediate |
| 22 | +/// Undefined Behavior, it is still unsound to expose such a mutable reference to code you do not |
| 23 | +/// control! Techniques such as pinning via [`Pin`](core::pin::Pin) are needed to ensure soundness. |
| 24 | +/// |
| 25 | +/// Similar to [`UnsafeCell`], [`UnsafePinned`] will not usually show up in |
| 26 | +/// the public API of a library. It is an internal implementation detail of libraries that need to |
| 27 | +/// support aliasing mutable references. |
| 28 | +/// |
| 29 | +/// Further note that this does *not* lift the requirement that shared references must be read-only! |
| 30 | +/// Use [`UnsafeCell`] for that. |
| 31 | +/// |
| 32 | +/// This type blocks niches the same way [`UnsafeCell`] does. |
| 33 | +// |
| 34 | +// As opposed to the upstream Rust type this contains a `PhantomPinned`` and `UnsafeCell<T>` |
| 35 | +// - `PhantomPinned` to avoid needing a `impl<T> !Unpin for UnsafePinned<T>` |
| 36 | +// - `UnsafeCell<T>` instead of T to disallow niche optimizations, |
| 37 | +// which is handled in the compiler in upstream Rust |
| 38 | +#[repr(transparent)] |
| 39 | +pub struct UnsafePinned<T: ?Sized> { |
| 40 | + _ph: PhantomPinned, |
| 41 | + value: UnsafeCell<T>, |
| 42 | +} |
| 43 | + |
| 44 | +impl<T> UnsafePinned<T> { |
| 45 | + /// Constructs a new instance of [`UnsafePinned`] which will wrap the specified value. |
| 46 | + /// |
| 47 | + /// All access to the inner value through `&UnsafePinned<T>` or `&mut UnsafePinned<T>` or |
| 48 | + /// `Pin<&mut UnsafePinned<T>>` requires `unsafe` code. |
| 49 | + #[inline(always)] |
| 50 | + #[must_use] |
| 51 | + pub const fn new(value: T) -> Self { |
| 52 | + UnsafePinned { |
| 53 | + value: UnsafeCell::new(value), |
| 54 | + _ph: PhantomPinned, |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +impl<T: ?Sized> UnsafePinned<T> { |
| 59 | + /// Get read-only access to the contents of a shared `UnsafePinned`. |
| 60 | + /// |
| 61 | + /// Note that `&UnsafePinned<T>` is read-only if `&T` is read-only. This means that if there is |
| 62 | + /// mutation of the `T`, future reads from the `*const T` returned here are UB! Use |
| 63 | + /// [`UnsafeCell`] if you also need interior mutability. |
| 64 | + /// |
| 65 | + /// [`UnsafeCell`]: core::cell::UnsafeCell |
| 66 | + /// |
| 67 | + /// ```rust,no_build |
| 68 | + /// use kernel::types::UnsafePinned; |
| 69 | + /// |
| 70 | + /// unsafe { |
| 71 | + /// let mut x = UnsafePinned::new(0); |
| 72 | + /// let ptr = x.get(); // read-only pointer, assumes immutability |
| 73 | + /// x.get_mut_unchecked().write(1); |
| 74 | + /// ptr.read(); // UB! |
| 75 | + /// } |
| 76 | + /// ``` |
| 77 | + /// |
| 78 | + /// Note that the `get_mut_unchecked` function used by this example is |
| 79 | + /// currently not implemented in the kernel implementation. |
| 80 | + #[inline(always)] |
| 81 | + #[must_use] |
| 82 | + pub const fn get(&self) -> *const T { |
| 83 | + self.value.get() |
| 84 | + } |
| 85 | + |
| 86 | + /// Gets a mutable pointer to the wrapped value. |
| 87 | + /// |
| 88 | + /// The difference from `get_mut_pinned` and `get_mut_unchecked` is that this function |
| 89 | + /// accepts a raw pointer, which is useful to avoid the creation of temporary references. |
| 90 | + /// |
| 91 | + /// These functions mentioned here are currently not implemented in the kernel. |
| 92 | + #[inline(always)] |
| 93 | + #[must_use] |
| 94 | + pub const fn raw_get_mut(this: *mut Self) -> *mut T { |
| 95 | + this as *mut T |
| 96 | + } |
| 97 | +} |
0 commit comments