Skip to content

Implement FromBytes and AsBytes for raw pointers #171

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

Closed
wants to merge 1 commit into from
Closed
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
33 changes: 30 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,30 @@ safety_comment! {
unsafe_impl!(isize: FromZeroes, FromBytes, AsBytes);
}

safety_comment! {
/// SAFETY:
/// - Raw pointers to `Sized` types have the same size and alignment as
/// `usize` [1]
/// - We know that any bit pattern is valid for a raw pointer to a `Sized`
/// type because it is a safe operation to `as` cast from a `usize`
/// - We know that raw pointers to `Sized` types do not contain
/// uninitialized or padding bytes - and more generally that it is always
/// valid to view the bytes of a raw pointer to a `Sized` type as a `[u8]`
/// - because it is a safe operation to `as` cast such a pointer to a
/// `usize`
///
/// TODO: Make the bit validity argument in terms of the reference once bit
/// validity is described in the reference
///
/// [1] https://doc.rust-lang.org/reference/type-layout.html#pointers-and-references-layout
unsafe_impl!(T: Sized => FromZeroes for *const T);
unsafe_impl!(T: Sized => FromBytes for *const T);
unsafe_impl!(T: Sized => AsBytes for *const T);
unsafe_impl!(T: Sized => FromZeroes for *mut T);
unsafe_impl!(T: Sized => FromBytes for *mut T);
unsafe_impl!(T: Sized => AsBytes for *mut T);
}

safety_comment! {
/// SAFETY:
/// - `FromZeroes`, `FromBytes`: the `{f32,f64}::from_bits` constructors'
Expand Down Expand Up @@ -3976,6 +4000,12 @@ mod tests {
assert_impls!(f32: FromZeroes, FromBytes, AsBytes, !Unaligned);
assert_impls!(f64: FromZeroes, FromBytes, AsBytes, !Unaligned);

// Implements none of the ZC traits.
struct NotZerocopy;

assert_impls!(*const NotZerocopy: FromZeroes, FromBytes, AsBytes, !Unaligned);
assert_impls!(*mut NotZerocopy: FromZeroes, FromBytes, AsBytes, !Unaligned);

assert_impls!(bool: FromZeroes, AsBytes, Unaligned, !FromBytes);
assert_impls!(char: FromZeroes, AsBytes, !FromBytes, !Unaligned);
assert_impls!(str: FromZeroes, AsBytes, Unaligned, !FromBytes);
Expand Down Expand Up @@ -4006,9 +4036,6 @@ mod tests {
assert_impls!(Option<NonZeroUsize>: FromZeroes, FromBytes, AsBytes, !Unaligned);
assert_impls!(Option<NonZeroIsize>: FromZeroes, FromBytes, AsBytes, !Unaligned);

// Implements none of the ZC traits.
struct NotZerocopy;

assert_impls!(PhantomData<NotZerocopy>: FromZeroes, FromBytes, AsBytes, Unaligned);
assert_impls!(PhantomData<[u8]>: FromZeroes, FromBytes, AsBytes, Unaligned);

Expand Down