-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
685 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "munge" | ||
version = "0.1.1" | ||
version = "0.2.0" | ||
authors = ["David Koloski <[email protected]>"] | ||
edition = "2021" | ||
description = "Macro for easily initializing `MaybeUninit`s" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
use { | ||
::core::{cell::{Cell, UnsafeCell}, mem::MaybeUninit, pin::Pin}, | ||
crate::{Destructure, Restructure, StructuralPinning}, | ||
}; | ||
|
||
// *const T | ||
|
||
impl<T: ?Sized> Destructure for *const T { | ||
type Underlying = T; | ||
|
||
fn as_mut_ptr(&mut self) -> *mut Self::Underlying { | ||
*self as *mut T | ||
} | ||
} | ||
|
||
unsafe impl<T: ?Sized, U: ?Sized> Restructure<&*const T> for U { | ||
type Restructured = *const Self; | ||
|
||
unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { | ||
ptr as *const Self | ||
} | ||
} | ||
|
||
// *mut T | ||
|
||
impl<T: ?Sized> Destructure for *mut T { | ||
type Underlying = T; | ||
|
||
fn as_mut_ptr(&mut self) -> *mut Self::Underlying { | ||
*self | ||
} | ||
} | ||
|
||
unsafe impl<T: ?Sized, U: ?Sized> Restructure<&*mut T> for U { | ||
type Restructured = *mut Self; | ||
|
||
unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { | ||
ptr | ||
} | ||
} | ||
|
||
// &MaybeUninit | ||
|
||
impl<'a, T> Destructure for &'a MaybeUninit<T> { | ||
type Underlying = T; | ||
|
||
fn as_mut_ptr(&mut self) -> *mut Self::Underlying { | ||
self.as_ptr() as *mut Self::Underlying | ||
} | ||
} | ||
|
||
unsafe impl<'a: 'b, 'b, T, U: 'b> Restructure<&'b &'a MaybeUninit<T>> for U { | ||
type Restructured = &'b MaybeUninit<U>; | ||
|
||
unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { | ||
// SAFETY: The caller has guaranteed that `ptr` points to a subfield of some | ||
// `&'b &'a MaybeUninit<T>`, so it's safe to dereference for the `'b` lifetime. | ||
unsafe { &*ptr.cast() } | ||
} | ||
} | ||
|
||
// &mut MaybeUninit | ||
|
||
impl<'a, T> Destructure for &'a mut MaybeUninit<T> { | ||
type Underlying = T; | ||
|
||
fn as_mut_ptr(&mut self) -> *mut Self::Underlying { | ||
MaybeUninit::as_mut_ptr(self) | ||
} | ||
} | ||
|
||
unsafe impl<'a: 'b, 'b, T, U: 'b> Restructure<&'b &'a mut MaybeUninit<T>> for U { | ||
type Restructured = &'b mut MaybeUninit<U>; | ||
|
||
unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { | ||
// SAFETY: The caller has guaranteed that `ptr` points to a subfield of some | ||
// `&'b &'a mut MaybeUninit<T>`, so it's safe to mutably dereference for the `'b` lifetime. | ||
unsafe { &mut *ptr.cast() } | ||
} | ||
} | ||
|
||
// &Cell<T> | ||
|
||
impl<'a, T> Destructure for &'a Cell<T> { | ||
type Underlying = T; | ||
|
||
fn as_mut_ptr(&mut self) -> *mut Self::Underlying { | ||
self.as_ptr() | ||
} | ||
} | ||
|
||
unsafe impl<'a: 'b, 'b, T, U: 'b> Restructure<&'b &'a Cell<T>> for U { | ||
type Restructured = &'b Cell<U>; | ||
|
||
unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { | ||
// SAFETY: The caller has guaranteed that `ptr` points to a subfield of some | ||
// `&'b &'a Cell<T>`, so it's safe to dereference for the `'b` lifetime. | ||
unsafe { &*ptr.cast() } | ||
} | ||
} | ||
|
||
// &UnsafeCell<T> | ||
|
||
impl<'a, T> Destructure for &'a UnsafeCell<T> { | ||
type Underlying = T; | ||
|
||
fn as_mut_ptr(&mut self) -> *mut Self::Underlying { | ||
self.get() | ||
} | ||
} | ||
|
||
unsafe impl<'a: 'b, 'b, T, U: 'b> Restructure<&'b &'a UnsafeCell<T>> for U { | ||
type Restructured = &'b UnsafeCell<U>; | ||
|
||
unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { | ||
// SAFETY: The caller has guaranteed that `ptr` points to a subfield of some | ||
// `&'b &'a UnsafeCell<T>`, so it's safe to dereference for the `'b` lifetime. | ||
unsafe { &*ptr.cast() } | ||
} | ||
} | ||
|
||
// Pin<&T> where T: StructuralPinning | ||
|
||
impl<'a, T: StructuralPinning> Destructure for Pin<&'a T> { | ||
type Underlying = T; | ||
|
||
fn as_mut_ptr(&mut self) -> *mut Self::Underlying { | ||
// SAFETY: The value pointed to by `self` will continue to be treated as pinned. | ||
unsafe { Pin::into_inner_unchecked(self.as_ref()) as *const T as *mut T } | ||
} | ||
} | ||
|
||
unsafe impl<'a: 'b, 'b, T: StructuralPinning, U: 'b> Restructure<&'b Pin<&'a T>> for U { | ||
type Restructured = Pin<&'b U>; | ||
|
||
unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { | ||
// SAFETY: The caller has guaranteed that `ptr` points to a subfield of some | ||
// `&'b Pin<&'a T>`, and `T` has structural pinning, so it's safe to derefence as pinned for | ||
// the `'b` lifetime. | ||
unsafe { Pin::new_unchecked(&*ptr) } | ||
} | ||
} | ||
|
||
// Pin<&mut T> where T: StructuralPinning | ||
|
||
impl<'a, T: StructuralPinning> Destructure for Pin<&'a mut T> { | ||
type Underlying = T; | ||
|
||
fn as_mut_ptr(&mut self) -> *mut Self::Underlying { | ||
// SAFETY: The value pointed to by `self` will continue to be treated as pinned. | ||
unsafe { Pin::into_inner_unchecked(self.as_mut()) as *mut T } | ||
} | ||
} | ||
|
||
unsafe impl<'a: 'b, 'b, T: StructuralPinning, U: 'b> Restructure<&'b Pin<&'a mut T>> for U { | ||
type Restructured = Pin<&'b mut U>; | ||
|
||
unsafe fn restructure(ptr: *mut Self) -> Self::Restructured { | ||
// SAFETY: The caller has guaranteed that `ptr` points to a subfield of some | ||
// `&'b Pin<&'a mut T>`, and `T` has structural pinning, so it's safe to mutably derefence | ||
// as pinned for the `'b` lifetime. | ||
unsafe { Pin::new_unchecked(&mut *ptr) } | ||
} | ||
} |
Oops, something went wrong.