Skip to content

Commit 09dcb4f

Browse files
committed
implement ZeroableOption for &T and &mut T
`Option<&T>` and `Option<&mut T>` are documented [1] to have the `None` variant be all zeroes. Link: https://doc.rust-lang.org/stable/std/option/index.html#representation [1] Signed-off-by: Benno Lossin <[email protected]>
1 parent fdd6bcb commit 09dcb4f

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- `impl<T, E> [Pin]Init<T, E> for Result<T, E>`, so results are now (pin-)initializers
1717
- add `Zeroable::init_zeroed()` delegating to `init_zeroed()`
1818
- add new `zeroed()`, a safe version of `mem::zeroed()` and also provide it via `Zeroable::zeroed()`
19+
- implement `Zeroable` for `Option<&T>` and `Option<&mut T>`
1920

2021
### Changed
2122

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,13 @@ pub unsafe trait ZeroableOption {}
15461546
// SAFETY: by the safety requirement of `ZeroableOption`, this is valid.
15471547
unsafe impl<T: ZeroableOption> Zeroable for Option<T> {}
15481548

1549+
// SAFETY: `Option<&T>` is part of the option layout optimization guarantee:
1550+
// <https://doc.rust-lang.org/stable/std/option/index.html#representation>.
1551+
unsafe impl<T> ZeroableOption for &T {}
1552+
// SAFETY: `Option<&mut T>` is part of the option layout optimization guarantee:
1553+
// <https://doc.rust-lang.org/stable/std/option/index.html#representation>.
1554+
unsafe impl<T> ZeroableOption for &mut T {}
1555+
15491556
/// Create an initializer for a zeroed `T`.
15501557
///
15511558
/// The returned initializer will write `0x00` to every byte of the given `slot`.

0 commit comments

Comments
 (0)