Skip to content

Commit 07082c3

Browse files
authored
Implement KnownLayout, FromZeroes for raw pointers (#584)
Makes progress on #170
1 parent a045ba7 commit 07082c3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,8 @@ impl_known_layout!(
707707
T: ?Sized => PhantomData<T>,
708708
T => Wrapping<T>,
709709
T => MaybeUninit<T>,
710+
T: ?Sized => *const T,
711+
T: ?Sized => *mut T,
710712
);
711713
impl_known_layout!(const N: usize, T => [T; N]);
712714

@@ -1865,6 +1867,22 @@ safety_comment! {
18651867
unsafe_impl!(T: AsBytes => AsBytes for [T]);
18661868
unsafe_impl!(T: Unaligned => Unaligned for [T]);
18671869
}
1870+
safety_comment! {
1871+
/// SAFETY:
1872+
/// - `FromZeroes`: For thin pointers (note that `T: Sized`), the zero
1873+
/// pointer is considered "null". [1] No operations which require
1874+
/// provenance are legal on null pointers, so this is not a footgun.
1875+
///
1876+
/// NOTE(#170): Implementing `FromBytes` and `AsBytes` for raw pointers
1877+
/// would be sound, but carries provenance footguns. We want to support
1878+
/// `FromBytes` and `AsBytes` for raw pointers eventually, but we are
1879+
/// holding off until we can figure out how to address those footguns.
1880+
///
1881+
/// [1] TODO(https://github.com/rust-lang/rust/pull/116988): Cite the
1882+
/// documentation once this PR lands.
1883+
unsafe_impl!(T => FromZeroes for *const T);
1884+
unsafe_impl!(T => FromZeroes for *mut T);
1885+
}
18681886

18691887
// SIMD support
18701888
//
@@ -5384,6 +5402,13 @@ mod tests {
53845402
assert_impls!([u8; 1]: KnownLayout, FromZeroes, FromBytes, AsBytes, Unaligned);
53855403
assert_impls!([NotZerocopy; 1]: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
53865404

5405+
assert_impls!(*const NotZerocopy: KnownLayout, FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5406+
assert_impls!(*mut NotZerocopy: KnownLayout, FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5407+
assert_impls!(*const [NotZerocopy]: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5408+
assert_impls!(*mut [NotZerocopy]: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5409+
assert_impls!(*const dyn Debug: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5410+
assert_impls!(*mut dyn Debug: KnownLayout, !FromZeroes, !FromBytes, !AsBytes, !Unaligned);
5411+
53875412
#[cfg(feature = "simd")]
53885413
{
53895414
#[allow(unused_macros)]

0 commit comments

Comments
 (0)