1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
use ::core::{
fmt,
marker::PhantomData,
mem::ManuallyDrop,
ops::{Deref, DerefMut},
ptr::{copy_nonoverlapping, NonNull},
};
use ::mischief::{
layout_of_val_raw,
Metadata,
Pointer,
Region,
RestructurablePointer,
Slot,
Unique,
Within,
};
use ::munge::{Destructure, Restructure};
use ::ptr_meta::{metadata, Pointee};
use crate::{
fmt::{DebugRaw, DisplayRaw},
DropRaw,
Mut,
Pinned,
Ref,
};
/// An immovable owned value in borrowed backing memory.
pub struct Val<'a, T: DropRaw + ?Sized> {
ptr: NonNull<T>,
_phantom: PhantomData<&'a mut T>,
}
impl<T: DropRaw + ?Sized> Drop for Val<'_, T> {
fn drop(&mut self) {
// SAFETY:
// - `self.ptr` is always non-null, properly aligned, and valid for
// reading and writing.
// - `Val` owns the value it points to, so `self.ptr` is always valid
// for dropping.
unsafe { T::drop_raw(self.as_mut()) }
}
}
impl<'a, T: DropRaw + ?Sized> Val<'a, T> {
/// Creates a new `Val` from an exclusively owned pointer.
///
/// # Safety
///
/// - `ptr` must be non-null, properly aligned, and valid for reading,
/// writing, and dropping.
/// - `ptr` must not alias any other accessible references for `'a`.
/// - The value pointed to by `ptr` must be initialized and immovable.
pub unsafe fn new_unchecked(ptr: *mut T) -> Self {
Self {
// SAFETY: `ptr` is non-null.
ptr: unsafe { NonNull::new_unchecked(ptr) },
_phantom: PhantomData,
}
}
/// Creates a new `Val` from an initialized `Slot`.
///
/// # Safety
///
/// The value pointed to by `slot` must be initialized, valid for dropping,
/// and immovable.
pub unsafe fn from_slot_unchecked(slot: Slot<'a, T>) -> Self {
Self {
// SAFETY: `Slot`s always have a non-null pointer.
ptr: unsafe { NonNull::new_unchecked(slot.as_ptr()) },
_phantom: PhantomData,
}
}
/// Returns a pointer to the referenced value.
pub fn as_ptr(&self) -> *mut T {
self.ptr.as_ptr()
}
/// Consumes the `Val` and leaks its value, returning a mutable reference
/// `&'a mut T`.
///
/// This function is mainly useful for data that lives for as long as its
/// backing memory. Dropping the returned reference will cause a memory
/// leak. If this is not acceptable, then the reference should first be
/// wrapped with the [`Val::new_unchecked`] function, producing a `Val`.
/// This `Val` can then be dropped which will properly destroy `T`.
///
/// Note: this is an associated function, which means that you have to call
/// it as `Val::leak(x)` instead of `x.leak()`. This is so that there is no
/// conflict with a method named `leak` on the value type.
pub fn leak(v: Self) -> Mut<'a, T> {
let v = ManuallyDrop::new(v);
// SAFETY:
// - `v.ptr` is always non-null, properly aligned, and valid for reads
// and writes.
// - `v.ptr` always points to a valid `T` and does not alias any other
// mutable references because it has the same aliasing as `v`.
// - The value pointed to by `v.ptr` will live for at least `'a` because
// its backing memory lives for at least that long. `v` was the only
// site allowed to drop the value, so it will also not be dropped.
// - `v` ensured that the pointee of `v.ptr` is immovable, so creating
// a `Mut` out of it will continue that guarantee.
unsafe { Mut::new_unchecked(v.ptr.as_ptr()) }
}
/// Casts a `Val<T>` to a `Val<U>`.
///
/// # Safety
///
/// The value owned by `self` must be a valid `U`.
pub unsafe fn cast<U: DropRaw>(self) -> Val<'a, U>
where
T: Sized,
{
let v = ManuallyDrop::new(self);
// SAFETY: `v.ptr` is always non-null, properly aligned, and valid for
// reading, writing, and dropping. The caller has guaranteed that `self`
// actually owns a `U`, so the cast `Val` is also initialized and
// immovable.
unsafe { Val::new_unchecked(v.ptr.as_ptr().cast()) }
}
/// Consumes the `Val` and returns the value it contained.
///
/// Note: this is an associated function, which means that you have to call
/// it as `Val::read(this)` instead of `this.read()`. This is so that there
/// is no conflict with a method on the inner type.
pub fn read(this: Self) -> T
where
T: Sized + Unpin,
{
let this = ManuallyDrop::new(this);
// SAFETY: `ptr` is always non-null, properly aligned, and valid for
// reads. The value may be moved because it implements `Unpin`.
unsafe { this.ptr.as_ptr().read() }
}
/// Consumes the `Val` and moves it into the given `Slot`.
///
/// Note: this is an associated function, which means that you have to call
/// it as `Val::read_unsized(this, slot)` instead of
/// `this.read_unsized(slot)`. This is so that there is no conflict with a
/// method on the inner type.
///
/// # Panics
///
/// Panics if `slot` does not have the same metadata as `this`.
pub fn read_unsized(this: Self, slot: Slot<'_, T>)
where
T: Pointee + Unpin,
<T as Pointee>::Metadata: Metadata<T>,
{
assert!(metadata(this.as_ptr()) == metadata(slot.as_ptr()));
// SAFETY: We asserted that `this` has the same metadata as `slot`.
unsafe {
Self::read_unsized_unchecked(this, slot);
}
}
/// Consumes the `Val` and moves it into the given `Slot`.
///
/// Note: this is an associated function, which means that you have to call
/// it as `Val::read_unsized_unchecked(this, slot)` instead of
/// `this.read_unsized_unchecked(slot)`. This is so that there is no
/// conflict with a method on the inner type.
///
/// # Safety
///
/// `slot` must have the same metadata as `this`.
pub unsafe fn read_unsized_unchecked(this: Self, slot: Slot<'_, T>)
where
T: Pointee + Unpin,
<T as Pointee>::Metadata: Metadata<T>,
{
let this = ManuallyDrop::new(this);
let layout = layout_of_val_raw(this.as_ptr());
// SAFETY:
// - `this` is valid for reads of `size` bytes because that is the size
unsafe {
copy_nonoverlapping(
this.as_ptr().cast::<u8>(),
slot.as_ptr().cast::<u8>(),
layout.size(),
);
}
}
/// Forgets the contained value, returning a `Slot` of the underlying
/// memory.
pub fn forget(this: Self) -> Slot<'a, T> {
let this = ManuallyDrop::new(this);
// SAFETY: `ptr` is a valid pointer for `'a` and the returned `Slot` is
// borrowed for `'b` and cannot be modified until the returned value is
// dropped.
unsafe { Slot::new_unchecked(this.ptr.as_ptr()) }
}
/// Drops the contained value, returning a `Slot` of the underlying memory.
pub fn drop(this: Self) -> Slot<'a, T> {
// SAFETY: `ptr` is a valid pointer for `'a` and the returned `Slot` is
// borrowed for `'b` and cannot be modified until the returned value is
// dropped.
let result = unsafe { Slot::new_unchecked(this.ptr.as_ptr()) };
drop(this);
result
}
/// Returns a `Ref` of the referenced value.
pub fn as_ref(&self) -> Ref<'_, T> {
// SAFETY: The requirements for `Ref` are a subset of those for `Val`.
unsafe { Ref::new_unchecked(self.as_ptr()) }
}
/// Returns a reborrowed `Mut` of the referenced value.
pub fn as_mut(&mut self) -> Mut<'_, T> {
// SAFETY: The requirements for `Ref` are a subset of those for `Val`.
unsafe { Mut::new_unchecked(self.as_ptr()) }
}
}
// SAFETY: `Val` returns the same value from `target`, `deref`, and `deref_mut`.
unsafe impl<T: DropRaw + ?Sized> Pointer for Val<'_, T> {
type Target = T;
fn target(&self) -> *mut Self::Target {
self.ptr.as_ptr()
}
}
// SAFETY: `T` is only located in `R`, so the targets of all `Val<'_, T>` must
// be located in `R`.
unsafe impl<T, R> Within<R> for Val<'_, T>
where
T: DropRaw + Pinned<R> + ?Sized,
R: Region,
{
}
impl<T: DropRaw + ?Sized> Deref for Val<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
// SAFETY:
// - `self.ptr` is always properly aligned and dereferenceable.
// - `self.ptr` always points to an initialized value of `T`.
// - Because `Val<'a, T>` lives for `'a` at most, the lifetime of
// `&self` must be shorter than `'a`. That lifetime is used for the
// returned reference, so the returned reference is valid for `'a` and
// has shared read-only aliasing.
unsafe { self.ptr.as_ref() }
}
}
// Note that `T` must be `Unpin` to avoid violating the immovability invariant
// of `Val`.
impl<T: DropRaw + Unpin + ?Sized> DerefMut for Val<'_, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
// SAFETY:
// - `self.ptr` is always properly aligned and dereferenceable.
// - `self.ptr` always points to an initialized value of `T`.
// - Because `Val<'a, T>` is mutably borrowed for `'_`, the returned
// reference is also valid for `'_` and has unique read-write
// aliasing.
unsafe { &mut *self.ptr.as_ptr() }
}
}
impl<T: DebugRaw + DropRaw + ?Sized> fmt::Debug for Val<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
DebugRaw::fmt_raw(self.as_ref(), f)
}
}
impl<T: DisplayRaw + DropRaw + ?Sized> fmt::Display for Val<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
DisplayRaw::fmt_raw(self.as_ref(), f)
}
}
// SAFETY: `Destructure::underlying` for `Val` returns the same pointer as
// `Pointer::target`.
unsafe impl<T: DropRaw + ?Sized> RestructurablePointer for Val<'_, T> {}
// SAFETY:
// - `Val<'a, T>` is destructured by value, so its `Destructuring` type is
// `Value`.
// - `underlying` returns the pointer inside the `Val<'a, T>`, which is
// guaranteed to be non-null, properly-aligned, and valid for reads.
unsafe impl<'a, T: DropRaw + ?Sized> Destructure for Val<'a, T> {
type Underlying = T;
type Destructuring = ::munge::Value;
fn underlying(&mut self) -> *mut Self::Underlying {
self.as_ptr()
}
}
// SAFETY: `restructure` returns a `Val<'a, U>` that takes ownership of the
// restructured field because `Val<'a, T>` is destructured by value.
unsafe impl<'a, T: DropRaw, U: 'a + DropRaw> Restructure<U> for Val<'a, T> {
type Restructured = Val<'a, U>;
unsafe fn restructure(&self, ptr: *mut U) -> Self::Restructured {
// SAFETY:
// - A pointer to a subfield of a `Val` is also non-null, properly
// aligned, and valid for reads and writes. It is also valid for
// dropping because we have been given ownership of the field.
// - `munge` enforces that the field pointer cannot alias another
// accessible reference to the field. Because `Val` owns the entire
// object, there cannot be another mutable reference to one of its
// fields.
// - All of the fields of a `Val<'a, T>` must be initialized and
// immovable because the overall `Val<'a, T>` is initialized and
// immovable.
unsafe { Val::new_unchecked(ptr) }
}
}
// SAFETY: Because the `T` value is unique and values can only have one owner,
// there can only ever be one `Val` of each unique `T` at any time.
unsafe impl<T: DropRaw + Unique + ?Sized> Unique for Val<'_, T> {}