Skip to content

Commit d7f0af0

Browse files
authored
Rollup merge of #163292 - Eleocraft:move-div-ceil, r=clarfonthey
moving div_ceil() to associated items macro Currently the div_ceil() method of unsigned NonZero is beeing generated inside the `nonzero_integer_signedness_dependent_impls` macro. This seems odd, as this macro seems to otherwise only be used for Trait impls, whereas associated methods like div_ceil() are all in the `nonzero_integer_signedness_dependent_methods` macro. If nothing else this causes an issue in the docs, where currently all unsigned NonZero types have two documented impl blocks, one containing only div_ceil(), and the other containing the remaining associated methods and constants. This PR assumes that this is accidental. If that is not the case then maybe the explanation should be documented somewhere instead.
2 parents a8c7176 + 980e0f0 commit d7f0af0

1 file changed

Lines changed: 30 additions & 32 deletions

File tree

‎library/core/src/num/nonzero.rs‎

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,38 +1573,6 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
15731573
*self = *self % other;
15741574
}
15751575
}
1576-
1577-
impl NonZero<$Int> {
1578-
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
1579-
///
1580-
/// The result is guaranteed to be non-zero.
1581-
///
1582-
/// # Examples
1583-
///
1584-
/// ```
1585-
/// # use std::num::NonZero;
1586-
#[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
1587-
#[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
1588-
/// assert_eq!(one.div_ceil(max), one);
1589-
///
1590-
#[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
1591-
#[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
1592-
/// assert_eq!(three.div_ceil(two), two);
1593-
/// ```
1594-
#[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1595-
#[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1596-
#[must_use = "this returns the result of the operation, \
1597-
without modifying the original"]
1598-
#[inline]
1599-
pub const fn div_ceil(self, rhs: Self) -> Self {
1600-
// An implementation of the function without calculating the remainder.
1601-
// It is better than the implementation for normal integers, but it can only
1602-
// be used here because of the possibility to subtract by one without overflow.
1603-
let v = (self.get() - 1) / rhs.get() + 1;
1604-
// SAFETY: ceiled division of two positive integers can never be zero.
1605-
unsafe { Self::new_unchecked(v) }
1606-
}
1607-
}
16081576
};
16091577
// Impls for signed nonzero types only.
16101578
(signed $Int:ty) => {
@@ -1772,6 +1740,36 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
17721740
unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
17731741
}
17741742

1743+
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
1744+
///
1745+
/// The result is guaranteed to be non-zero.
1746+
///
1747+
/// # Examples
1748+
///
1749+
/// ```
1750+
/// # use std::num::NonZero;
1751+
#[doc = concat!("let one = NonZero::new(1", stringify!($Int), ").unwrap();")]
1752+
#[doc = concat!("let max = NonZero::new(", stringify!($Int), "::MAX).unwrap();")]
1753+
/// assert_eq!(one.div_ceil(max), one);
1754+
///
1755+
#[doc = concat!("let two = NonZero::new(2", stringify!($Int), ").unwrap();")]
1756+
#[doc = concat!("let three = NonZero::new(3", stringify!($Int), ").unwrap();")]
1757+
/// assert_eq!(three.div_ceil(two), two);
1758+
/// ```
1759+
#[stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1760+
#[rustc_const_stable(feature = "unsigned_nonzero_div_ceil", since = "1.92.0")]
1761+
#[must_use = "this returns the result of the operation, \
1762+
without modifying the original"]
1763+
#[inline]
1764+
pub const fn div_ceil(self, rhs: Self) -> Self {
1765+
// An implementation of the function without calculating the remainder.
1766+
// It is better than the implementation for normal integers, but it can only
1767+
// be used here because of the possibility to subtract by one without overflow.
1768+
let v = (self.get() - 1) / rhs.get() + 1;
1769+
// SAFETY: ceiled division of two positive integers can never be zero.
1770+
unsafe { Self::new_unchecked(v) }
1771+
}
1772+
17751773
/// Returns the smallest power of two greater than or equal to `self`.
17761774
/// Checks for overflow and returns [`None`]
17771775
/// if the next power of two is greater than the type’s maximum value.

0 commit comments

Comments
 (0)