Skip to content

Commit

Permalink
Relax pointer_to_metadata preconditions
Browse files Browse the repository at this point in the history
Change argument type of `pointer_to_metadata` from `NonNull<Self>`
to `*mut Self`, since the implementations of `pointer_to_metadata`
do not actually require the argument to be non-null.

I anticipate this change will make future code less verbose, as
many standard library APIs return raw pointers, not  `NonNull`; for
example, `Box::into_raw`.
  • Loading branch information
jswrenn committed Oct 5, 2024
1 parent 1271aa0 commit 95cb9a7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ pub unsafe trait KnownLayout {
/// `pointer_to_metadata` always returns the correct metadata stored in
/// `ptr`.
#[doc(hidden)]
fn pointer_to_metadata(ptr: NonNull<Self>) -> Self::PointerMetadata;
fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata;

/// Computes the length of the byte range addressed by `ptr`.
///
Expand All @@ -775,7 +775,7 @@ pub unsafe trait KnownLayout {
#[must_use]
#[inline(always)]
fn size_of_val_raw(ptr: NonNull<Self>) -> Option<usize> {
let meta = Self::pointer_to_metadata(ptr);
let meta = Self::pointer_to_metadata(ptr.as_ptr());
// SAFETY: `size_for_metadata` promises to only return `None` if the
// resulting size would not fit in a `usize`.
meta.size_for_metadata(Self::LAYOUT)
Expand Down Expand Up @@ -868,9 +868,9 @@ unsafe impl<T> KnownLayout for [T] {
}

#[inline(always)]
fn pointer_to_metadata(ptr: NonNull<[T]>) -> usize {
fn pointer_to_metadata(ptr: *mut [T]) -> usize {
#[allow(clippy::as_conversions)]
let slc = ptr.as_ptr() as *const [()];
let slc = ptr as *const [()];

// SAFETY:
// - `()` has alignment 1, so `slc` is trivially aligned.
Expand Down
14 changes: 10 additions & 4 deletions src/pointer/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ mod _project {
/// Unsafe code my rely on `trailing_slice_len` satisfying the above
/// contract.
pub(super) fn trailing_slice_len(&self) -> usize {
T::pointer_to_metadata(self.as_non_null())
T::pointer_to_metadata(self.as_non_null().as_ptr())
}
}

Expand Down Expand Up @@ -1760,7 +1760,9 @@ mod tests {
}

if let Some(want) = meta {
let got = KnownLayout::pointer_to_metadata(slf.as_non_null());
let got = KnownLayout::pointer_to_metadata(
slf.as_non_null().as_ptr(),
);
assert_eq!(got, want);
}
}
Expand All @@ -1775,7 +1777,8 @@ mod tests {
assert_eq!(len, bytes.len());

if let Some(want) = meta {
let got = KnownLayout::pointer_to_metadata(slf.as_non_null());
let got =
KnownLayout::pointer_to_metadata(slf.as_non_null().as_ptr());
assert_eq!(got, want);
}
}
Expand Down Expand Up @@ -1859,7 +1862,10 @@ mod tests {
ptr.try_cast_into::<$ty, BecauseImmutable>(CastType::Prefix, Some($elems));
if let Some(expect) = $expect {
let (ptr, _) = res.unwrap();
assert_eq!(KnownLayout::pointer_to_metadata(ptr.as_non_null()), expect);
assert_eq!(
KnownLayout::pointer_to_metadata(ptr.as_non_null().as_ptr()),
expect
);
} else {
let _ = res.unwrap_err();
}
Expand Down
7 changes: 3 additions & 4 deletions src/util/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ macro_rules! impl_known_layout {
}

#[inline(always)]
fn pointer_to_metadata(_ptr: NonNull<Self>) -> () {
fn pointer_to_metadata(_ptr: *mut Self) -> () {
}
}
};
Expand Down Expand Up @@ -589,10 +589,9 @@ macro_rules! unsafe_impl_known_layout {
}

#[inline(always)]
fn pointer_to_metadata(ptr: NonNull<Self>) -> Self::PointerMetadata {
// SAFETY: `ptr` is non-null.
fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata {
#[allow(clippy::as_conversions)]
let ptr = unsafe { NonNull::new_unchecked(ptr.as_ptr() as *mut $repr) };
let ptr = ptr as *mut $repr;
<$repr>::pointer_to_metadata(ptr)
}
}
Expand Down
11 changes: 3 additions & 8 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,8 @@ fn derive_known_layout_inner(ast: &DeriveInput, _top_level: Trait) -> Result<Tok
}

#[inline(always)]
fn pointer_to_metadata(ptr: ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<Self>) -> Self::PointerMetadata {
// SAFETY: `ptr` is non-null.
let ptr = unsafe { ::zerocopy::util::macro_util::core_reexport::ptr::NonNull::new_unchecked(ptr.as_ptr() as *mut _) };
<#trailing_field_ty>::pointer_to_metadata(ptr)
fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata {
<#trailing_field_ty>::pointer_to_metadata(ptr as *mut _)
}
),
)
Expand Down Expand Up @@ -270,10 +268,7 @@ fn derive_known_layout_inner(ast: &DeriveInput, _top_level: Trait) -> Result<Tok
}

#[inline(always)]
fn pointer_to_metadata(
_ptr: ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<Self>,
) -> () {
}
fn pointer_to_metadata(_ptr: *mut Self) -> () {}
),
)
};
Expand Down
5 changes: 1 addition & 4 deletions zerocopy-derive/src/output_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ fn test_known_layout() {
}

#[inline(always)]
fn pointer_to_metadata(
_ptr: ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<Self>,
) -> () {
}
fn pointer_to_metadata(_ptr: *mut Self) -> () {}
}
} no_build
}
Expand Down

0 comments on commit 95cb9a7

Please sign in to comment.