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 KnownLayout, FromZeroes for raw pointers #584

Merged
merged 1 commit into from
Nov 2, 2023
Merged
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
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,8 @@ impl_known_layout!(
T: ?Sized => PhantomData<T>,
T => Wrapping<T>,
T => MaybeUninit<T>,
T: ?Sized => *const T,
T: ?Sized => *mut T,
);
impl_known_layout!(const N: usize, T => [T; N]);

Expand Down Expand Up @@ -1857,6 +1859,22 @@ safety_comment! {
unsafe_impl!(T: AsBytes => AsBytes for [T]);
unsafe_impl!(T: Unaligned => Unaligned for [T]);
}
safety_comment! {
/// SAFETY:
/// - `FromZeroes`: For thin pointers (note that `T: Sized`), the zero
/// pointer is considered "null". [1] No operations which require
/// provenance are legal on null pointers, so this is not a footgun.
///
/// NOTE(#170): Implementing `FromBytes` and `AsBytes` for raw pointers
/// would be sound, but carries provenance footguns. We want to support
/// `FromBytes` and `AsBytes` for raw pointers eventually, but we are
/// holding off until we can figure out how to address those footguns.
///
/// [1] TODO(https://github.com/rust-lang/rust/pull/116988): Cite the
/// documentation once this PR lands.
unsafe_impl!(T => FromZeroes for *const T);
unsafe_impl!(T => FromZeroes for *mut T);
}

// SIMD support
//
Expand Down Expand Up @@ -5375,6 +5393,13 @@ mod tests {
assert_impls!([u8; 1]: KnownLayout, FromZeroes, FromBytes, AsBytes, Unaligned);
assert_impls!([NotZerocopy; 1]: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);

assert_impls!(*const NotZerocopy: KnownLayout, FromZeroes, !FromBytes, !AsBytes, !Unaligned);
assert_impls!(*mut NotZerocopy: KnownLayout, FromZeroes, !FromBytes, !AsBytes, !Unaligned);
assert_impls!(*const [NotZerocopy]: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
assert_impls!(*mut [NotZerocopy]: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
assert_impls!(*const dyn Debug: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
assert_impls!(*mut dyn Debug: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);

#[cfg(feature = "simd")]
{
#[allow(unused_macros)]
Expand Down