| 1 |
#![deny(missing_docs, missing_debug_implementations)] |
| 2 |
//! This library defines the `PinCell` type, a pinning variant of the standard |
| 3 |
//! library's `RefCell`. |
| 4 |
//! |
| 5 |
//! It is not safe to "pin project" through a `RefCell` - getting a pinned |
| 6 |
//! reference to something inside the `RefCell` when you have a pinned |
| 7 |
//! refernece to the `RefCell` - because `RefCell` is too powerful. |
| 8 |
//! |
| 9 |
//! A `PinCell` is slightly less powerful than `RefCell`: unlike a `RefCell`, |
| 10 |
//! one cannot get a mutable reference into a `PinCell`, only a pinned mutable |
| 11 |
//! reference (`Pin<&mut T>`). This makes pin projection safe, allowing you |
| 12 |
//! to use interior mutability with the knowledge that `T` will never actually |
| 13 |
//! be moved out of the `RefCell` that wraps it. |
| 14 |
|
| 15 |
mod pin_mut; |
| 16 |
mod pin_ref; |
| 17 |
|
| 18 |
use core::cell::{BorrowMutError, RefCell, RefMut}; |
| 19 |
use core::pin::Pin; |
| 20 |
|
| 21 |
pub use pin_mut::PinMut; |
| 22 |
pub use pin_ref::PinRef; |
| 23 |
|
| 24 |
/// A mutable memory location with dynamically checked borrow rules |
| 25 |
/// |
| 26 |
/// Unlike `RefCell`, this type only allows *pinned* mutable access to the |
| 27 |
/// inner value, enabling a "pin-safe" version of interior mutability. |
| 28 |
/// |
| 29 |
/// See the standard library documentation for more information. |
| 30 |
#[derive(Default, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)] |
| 31 |
pub struct PinCell<T: ?Sized> { |
| 32 |
inner: RefCell<T>, |
| 33 |
} |
| 34 |
|
| 35 |
impl<T> PinCell<T> { |
| 36 |
/// Creates a new `PinCell` containing `value`. |
| 37 |
pub const fn new(value: T) -> PinCell<T> { |
| 38 |
PinCell { |
| 39 |
inner: RefCell::new(value), |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl<T: ?Sized> PinCell<T> { |
| 45 |
/// Mutably borrows the wrapped value, preserving its pinnedness. |
| 46 |
/// |
| 47 |
/// The borrow lasts until the returned `PinMut` or all `PinMut`s derived |
| 48 |
/// from it exit scope. The value cannot be borrowed while this borrow is |
| 49 |
/// active. |
| 50 |
pub fn borrow_mut(self: Pin<&Self>) -> PinMut<'_, T> { |
| 51 |
self.try_borrow_mut().expect("already borrowed") |
| 52 |
} |
| 53 |
|
| 54 |
/// Mutably borrows the wrapped value, preserving its pinnedness, |
| 55 |
/// returning an error if the value is currently borrowed. |
| 56 |
/// |
| 57 |
/// The borrow lasts until the returned `PinMut` or all `PinMut`s derived |
| 58 |
/// from it exit scope. The value cannot be borrowed while this borrow is |
| 59 |
/// active. |
| 60 |
/// |
| 61 |
/// This is the non-panicking variant of `borrow_mut`. |
| 62 |
pub fn try_borrow_mut<'a>(self: Pin<&'a Self>) -> Result<PinMut<'a, T>, BorrowMutError> { |
| 63 |
let ref_mut: RefMut<'a, T> = Pin::get_ref(self).inner.try_borrow_mut()?; |
| 64 |
|
| 65 |
// this is a pin projection from Pin<&PinCell<T>> to Pin<RefMut<T>> |
| 66 |
// projecting is safe because: |
| 67 |
// |
| 68 |
// - for<T: ?Sized> (PinCell<T>: Unpin) imples (RefMut<T>: Unpin) |
| 69 |
// holds true |
| 70 |
// - PinCell does not implement Drop |
| 71 |
// |
| 72 |
// see discussion on tracking issue #49150 about pin projection |
| 73 |
// invariants |
| 74 |
let pin_ref_mut: Pin<RefMut<'a, T>> = unsafe { Pin::new_unchecked(ref_mut) }; |
| 75 |
|
| 76 |
Ok(PinMut { inner: pin_ref_mut }) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
impl<T> From<T> for PinCell<T> { |
| 81 |
fn from(value: T) -> PinCell<T> { |
| 82 |
PinCell::new(value) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
impl<T> From<RefCell<T>> for PinCell<T> { |
| 87 |
fn from(cell: RefCell<T>) -> PinCell<T> { |
| 88 |
PinCell { inner: cell } |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
impl<T> From<PinCell<T>> for RefCell<T> { |
| 93 |
fn from(input: PinCell<T>) -> Self { |
| 94 |
input.inner |
| 95 |
} |
| 96 |
} |
| 97 |
// TODO CoerceUnsized |