Skip to content

Commit d316316

Browse files
authored
Merge pull request #110 from Hamdan-Khan/main
implement ZeroableOption for NonZero* types
2 parents bad9e6a + 74f7726 commit d316316

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- `[pin_]init_scope` functions to run arbitrary code inside of an initializer.
1313
- `&'static mut MaybeUninit<T>` now implements `InPlaceWrite`. This enables users to use external
1414
allocation mechanisms such as `static_cell`.
15+
- Non-zero integer types (`NonZero*`) now implement `ZeroableOption`.
1516

1617
### Changed
1718

src/lib.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,13 +1615,6 @@ impl_zeroable! {
16151615
// SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`.
16161616
{<T: ?Sized + Zeroable>} UnsafeCell<T>,
16171617

1618-
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee:
1619-
// <https://doc.rust-lang.org/stable/std/option/index.html#representation>).
1620-
Option<NonZeroU8>, Option<NonZeroU16>, Option<NonZeroU32>, Option<NonZeroU64>,
1621-
Option<NonZeroU128>, Option<NonZeroUsize>,
1622-
Option<NonZeroI8>, Option<NonZeroI16>, Option<NonZeroI32>, Option<NonZeroI64>,
1623-
Option<NonZeroI128>, Option<NonZeroIsize>,
1624-
16251618
// SAFETY: `null` pointer is valid.
16261619
//
16271620
// We cannot use `T: ?Sized`, since the VTABLE pointer part of fat pointers is not allowed to be
@@ -1681,6 +1674,20 @@ macro_rules! impl_fn_zeroable_option {
16811674

16821675
impl_fn_zeroable_option!(["Rust", "C"] { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U });
16831676

1677+
macro_rules! impl_non_zero_int_zeroable_option {
1678+
($($int:ty),* $(,)?) => {
1679+
// SAFETY: Safety comment written in the macro invocation.
1680+
$(unsafe impl ZeroableOption for $int {})*
1681+
};
1682+
}
1683+
1684+
impl_non_zero_int_zeroable_option! {
1685+
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee:
1686+
// <https://doc.rust-lang.org/stable/std/option/index.html#representation>).
1687+
NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,
1688+
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize,
1689+
}
1690+
16841691
/// This trait allows creating an instance of `Self` which contains exactly one
16851692
/// [structurally pinned value](https://doc.rust-lang.org/std/pin/index.html#projections-and-structural-pinning).
16861693
///

0 commit comments

Comments
 (0)