Skip to content

Commit

Permalink
Add more Zeroable and ZeroableInOption impls (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr authored Jan 19, 2023
1 parent f276289 commit 2548e46
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/zeroable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,17 @@ unsafe impl Zeroable for f64 {}
unsafe impl<T: Zeroable> Zeroable for Wrapping<T> {}
unsafe impl<T: Zeroable> Zeroable for core::cmp::Reverse<T> {}

// Note: we can't implement this for all `T: ?Sized` types because it would
// create NULL pointers for vtables.
// Maybe one day this could be changed to be implemented for
// `T: ?Sized where <T as core::ptr::Pointee>::Metadata: Zeroable`.
unsafe impl<T> Zeroable for *mut T {}
unsafe impl<T> Zeroable for *const T {}
unsafe impl<T> Zeroable for *mut [T] {}
unsafe impl<T> Zeroable for *const [T] {}
unsafe impl Zeroable for *mut str {}
unsafe impl Zeroable for *const str {}

unsafe impl<T: Zeroable> Zeroable for PhantomData<T> {}
unsafe impl Zeroable for PhantomPinned {}
unsafe impl<T: Zeroable> Zeroable for ManuallyDrop<T> {}
Expand Down
8 changes: 7 additions & 1 deletion src/zeroable_in_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ unsafe impl ZeroableInOption for NonZeroU64 {}
unsafe impl ZeroableInOption for NonZeroU128 {}
unsafe impl ZeroableInOption for NonZeroUsize {}

unsafe impl<T> ZeroableInOption for NonNull<T> {}
// Note: this does not create NULL vtable because we get `None` anyway.
unsafe impl<T: ?Sized> ZeroableInOption for NonNull<T> {}
unsafe impl<T: ?Sized> ZeroableInOption for &'_ T {}
unsafe impl<T: ?Sized> ZeroableInOption for &'_ mut T {}

#[cfg(feature = "extern_crate_alloc")]
unsafe impl<T: ?Sized> ZeroableInOption for alloc::boxed::Box<T> {}

0 comments on commit 2548e46

Please sign in to comment.