Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,32 @@ macro_rules! impl_nonzero_fmt {
}
}

macro_rules! doc_comment {
($x:expr, $($tt:tt)*) => {
#[doc = $x]
$($tt)*
};
}

macro_rules! nonzero_integers {
( $( $Ty: ident($Int: ty); )+ ) => {
$(
/// An integer that is known not to equal zero.
///
/// This enables some memory layout optimization.
/// For example, `Option<NonZeroU32>` is the same size as `u32`:
///
/// ```rust
/// use std::mem::size_of;
/// assert_eq!(size_of::<Option<std::num::NonZeroU32>>(), size_of::<u32>());
/// ```
#[stable(feature = "nonzero", since = "1.28.0")]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct $Ty(NonZero<$Int>);
doc_comment! {
concat!("An integer that is known not to equal zero.

This enables some memory layout optimization.
For example, `Option<", stringify!($Ty), ">` is the same size as `", stringify!($Int), "`:

```rust
use std::mem::size_of;
assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", stringify!($Int),
">());
```"),
#[stable(feature = "nonzero", since = "1.28.0")]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct $Ty(NonZero<$Int>);
}

impl $Ty {
/// Create a non-zero without checking the value.
Expand Down Expand Up @@ -176,13 +186,6 @@ pub mod dec2flt;
pub mod bignum;
pub mod diy_float;

macro_rules! doc_comment {
($x:expr, $($tt:tt)*) => {
#[doc = $x]
$($tt)*
};
}

mod wrapping;

// `Int` + `SignedInt` implemented for signed integers
Expand Down