Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Strict on pointers to unsized types. #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 15 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ mod private {
}

pub trait Strict: private::Sealed {
type Pointee;
type Pointee: ?Sized;
/// Gets the "address" portion of the pointer.
///
/// This is similar to `self as usize`, which semantically discards *provenance* and
Expand All @@ -527,9 +527,7 @@ pub trait Strict: private::Sealed {
/// might change in the future (including possibly weakening this so it becomes wholly
/// equivalent to `self as usize`). See the [module documentation][crate] for details.
#[must_use]
fn addr(self) -> usize
where
Self::Pointee: Sized;
fn addr(self) -> usize;

/// Gets the "address" portion of the pointer, and 'exposes' the "provenance" part for future
/// use in [`from_exposed_addr`][].
Expand All @@ -556,9 +554,7 @@ pub trait Strict: private::Sealed {
///
/// [`from_exposed_addr`]: crate::from_exposed_addr
#[must_use]
fn expose_addr(self) -> usize
where
Self::Pointee: Sized;
fn expose_addr(self) -> usize;

/// Creates a new pointer with the given address.
///
Expand Down Expand Up @@ -591,32 +587,26 @@ pub trait Strict: private::Sealed {
Self::Pointee: Sized;
}

impl<T> private::Sealed for *mut T {}
impl<T> private::Sealed for *const T {}
impl<T: ?Sized> private::Sealed for *mut T {}
impl<T: ?Sized> private::Sealed for *const T {}

impl<T> Strict for *mut T {
impl<T: ?Sized> Strict for *mut T {
type Pointee = T;

#[must_use]
#[inline]
fn addr(self) -> usize
where
T: Sized,
{
fn addr(self) -> usize {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
// SAFETY: Pointer-to-integer transmutes are valid (if you are okay with losing the
// provenance).
unsafe { core::mem::transmute(self) }
unsafe { core::mem::transmute(self.cast::<()>()) }
}

#[must_use]
#[inline]
fn expose_addr(self) -> usize
where
T: Sized,
{
fn expose_addr(self) -> usize {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
self as usize
self.cast::<()>() as usize
}

#[must_use]
Expand Down Expand Up @@ -650,29 +640,23 @@ impl<T> Strict for *mut T {
}
}

impl<T> Strict for *const T {
impl<T: ?Sized> Strict for *const T {
type Pointee = T;

#[must_use]
#[inline]
fn addr(self) -> usize
where
T: Sized,
{
fn addr(self) -> usize {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
// SAFETY: Pointer-to-integer transmutes are valid (if you are okay with losing the
// provenance).
unsafe { core::mem::transmute(self) }
unsafe { core::mem::transmute(self.cast::<()>()) }
}

#[must_use]
#[inline]
fn expose_addr(self) -> usize
where
T: Sized,
{
fn expose_addr(self) -> usize {
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
self as usize
self.cast::<()>() as usize
}

#[must_use]
Expand Down