Skip to content

Commit 43dfbf6

Browse files
committed
implement ZeroableOption macro for NonZero* integer types
add a macro for implementing `ZeroableOption` for `NonZero*` integer types instead of using `Option<T>` wrapper, which was previously implemented for each `NonZero` type individually inside the `impl_zeroable` macro. Link: #95 Signed-off-by: Hamdan-Khan <hamdankhan212@gmail.com>
1 parent a188482 commit 43dfbf6

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

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+
- Add a macro to implement `ZeroableOption` for non-zero integer types (i.e. `NonZero*`).
1516

1617
### Changed
1718

src/lib.rs

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

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

16651658
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 });
16661659

1660+
macro_rules! impl_non_zero_int_zeroable_option {
1661+
($($int:ty),* $(,)?) => {
1662+
// SAFETY: Safety comment written in the macro invocation.
1663+
$(unsafe impl ZeroableOption for $int {})*
1664+
};
1665+
}
1666+
1667+
impl_non_zero_int_zeroable_option! {
1668+
// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee:
1669+
// <https://doc.rust-lang.org/stable/std/option/index.html#representation>).
1670+
NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,
1671+
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize,
1672+
}
1673+
16671674
/// This trait allows creating an instance of `Self` which contains exactly one
16681675
/// [structurally pinned value](https://doc.rust-lang.org/std/pin/index.html#projections-and-structural-pinning).
16691676
///

0 commit comments

Comments
 (0)