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
62 changes: 30 additions & 32 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1573,38 +1573,6 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
*self = *self % other;
}
}

impl NonZero<$Int> {
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
///
/// The result is guaranteed to be non-zero.
///
/// # Examples
///
/// ```
/// # use std::num::NonZero;
#[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
#[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
/// assert_eq!(one.div_ceil(max), one);
///
#[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
#[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
/// assert_eq!(three.div_ceil(two), two);
/// ```
#[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
#[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn div_ceil(self, rhs: Self) -> Self {
// An implementation of the function without calculating the remainder.
// It is better than the implementation for normal integers, but it can only
// be used here because of the possibility to subtract by one without overflow.
let v = (self.get() - 1) / rhs.get() + 1;
// SAFETY: ceiled division of two positive integers can never be zero.
unsafe { Self::new_unchecked(v) }
}
}
};
// Impls for signed nonzero types only.
(signed $Int:ty) => {
Expand Down Expand Up @@ -1772,6 +1740,36 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
}

/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
///
/// The result is guaranteed to be non-zero.
///
/// # Examples
///
/// ```
/// # use std::num::NonZero;
#[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
#[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
/// assert_eq!(one.div_ceil(max), one);
///
#[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
#[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
/// assert_eq!(three.div_ceil(two), two);
/// ```
#[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
#[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn div_ceil(self, rhs: Self) -> Self {
// An implementation of the function without calculating the remainder.
// It is better than the implementation for normal integers, but it can only
// be used here because of the possibility to subtract by one without overflow.
let v = (self.get() - 1) / rhs.get() + 1;
// SAFETY: ceiled division of two positive integers can never be zero.
unsafe { Self::new_unchecked(v) }
}

/// Returns the smallest power of two greater than or equal to `self`.
/// Checks for overflow and returns [`None`]
/// if the next power of two is greater than the type’s maximum value.
Expand Down
Loading