Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8b165db

Browse files
committedNov 5, 2024
Implement div_ceil for NonZero<unsigned>
1 parent 538f5b4 commit 8b165db

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
 

‎core/src/num/nonzero.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,35 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
12141214
*self = *self % other;
12151215
}
12161216
}
1217+
1218+
impl NonZero<$Int> {
1219+
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
1220+
///
1221+
/// The result is guaranteed to be non-zero.
1222+
///
1223+
/// # Examples
1224+
///
1225+
/// ```
1226+
/// # #![feature(unsigned_nonzero_div_ceil)]
1227+
/// # use std::num::NonZero;
1228+
#[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
1229+
#[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
1230+
/// assert_eq!(one.div_ceil(max), one);
1231+
///
1232+
#[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
1233+
#[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
1234+
/// assert_eq!(three.div_ceil(two), two);
1235+
/// ```
1236+
#[unstable(feature = "unsigned_nonzero_div_ceil", issue = "none")]
1237+
#[must_use = "this returns the result of the operation, \
1238+
without modifying the original"]
1239+
#[inline]
1240+
pub const fn div_ceil(self, rhs: Self) -> Self {
1241+
let v = self.get().div_ceil(rhs.get());
1242+
// SAFETY: ceiled division of two positive integers can never be zero.
1243+
unsafe { Self::new_unchecked(v) }
1244+
}
1245+
}
12171246
};
12181247
// Impls for signed nonzero types only.
12191248
(signed $Int:ty) => {

0 commit comments

Comments
 (0)
Failed to load comments.