use core::borrow::Borrow; use core::cmp::Ordering; use core::fmt; use core::num::IntErrorKind; use core::str::FromStr; #[cfg(feature = "std")] use std::error::Error;
#[cfg(feature = "powerfmt")] use powerfmt::smart_display;
impl ParseIntError { /// Outputs the detailed cause of parsing an integer failing. // This function is not const because the counterpart of stdlib isn't #[allow(clippy::missing_const_for_fn)] #[inline(always)] pubfn kind(&self) -> &IntErrorKind {
&self.kind
}
}
impl fmt::Display for ParseIntError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { matchself.kind {
IntErrorKind::Empty => "cannot parse integer from empty string",
IntErrorKind::InvalidDigit => "invalid digit found in string",
IntErrorKind::PosOverflow => "number too large to fit in target type",
IntErrorKind::NegOverflow => "number too small to fit in target type",
IntErrorKind::Zero => "number would be zero for non-zero type",
_ => "Unknown Int error kind",
}
.fmt(f)
}
}
#[cfg(feature = "std")] impl Error for ParseIntError {}
/// Informs the optimizer that a condition is always true. If the condition is false, the behavior /// is undefined. /// /// # Safety /// /// `b` must be `true`. #[inline] constunsafefn assume(b: bool) {
debug_assert!(b); if !b { // Safety: The caller must ensure that `b` is true. unsafe { core::hint::unreachable_unchecked() }
}
}
macro_rules! impl_ranged {
($(
$type:ident {
mod_name: $mod_name:ident
internal: $internal:ident
signed: $is_signed:ident
unsigned: $unsigned_type:ident
optional: $optional_type:ident
}
)*) => {$( #[doc = concat!(
article!($is_signed), " `",
stringify!($internal), "` that is known to be in the range `MIN..=MAX`.",
)] #[repr(transparent)] #[derive(Clone, Copy, Eq, Ord, Hash)] pubstruct $type<const MIN: $internal, const MAX: $internal>( Unsafe<$internal>,
);
#[doc = concat!( "A `",
stringify!($type), "` that is optional. Equivalent to [`Option<",
stringify!($type), ">`] with niche value optimization.",
)] /// #[doc = concat!( "If `MIN` is [`",
stringify!($internal), "::MIN`] _and_ `MAX` is [`",
stringify!($internal)
,"::MAX`] then compilation will fail. This is because there is no way to represent \
the niche value.",
)] /// /// This type is useful when you need to store an optional ranged value in a struct, but /// do not want the overhead of an `Option` type. This reduces the size of the struct /// overall, and is particularly useful when you have a large number of optional fields. /// Note that most operations must still be performed on the [`Option`] type, which is #[doc = concat!("obtained with [`", stringify!($optional_type), "::get`].")] #[repr(transparent)] #[derive(Clone, Copy, Eq, Hash)] pubstruct $optional_type<const MIN: $internal, const MAX: $internal>(
$internal,
);
impl $type<0, 0> { #[inline(always)] pubconstfn exact<const VALUE: $internal>() -> $type<VALUE, VALUE> { // Safety: The value is the only one in range. unsafe { $type::new_unchecked(VALUE) }
}
}
impl<const MIN: $internal, const MAX: $internal> $type<MIN, MAX> { /// The smallest value that can be represented by this type. // Safety: `MIN` is in range by definition. pubconst MIN: Self = Self::new_static::<MIN>();
/// The largest value that can be represented by this type. // Safety: `MAX` is in range by definition. pubconst MAX: Self = Self::new_static::<MAX>();
/// Creates a ranged integer without checking the value. /// /// # Safety /// /// The value must be within the range `MIN..=MAX`. #[inline(always)] pubconstunsafefn new_unchecked(value: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the value is in range. unsafe {
$crate::assume(MIN <= value && value <= MAX); Self(Unsafe::new(value))
}
}
/// Returns the value as a primitive type. #[inline(always)] pubconstfn get(self) -> $internal {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: A stored value is always in range. unsafe { $crate::assume(MIN <= *self.0.get() && *self.0.get() <= MAX) };
*self.0.get()
}
#[inline(always)] pub(crate) constfn get_ref(&self) -> &$internal {
<Selfas $crate::traits::RangeIsValid>::ASSERT; let value = self.0.get(); // Safety: A stored value is always in range. unsafe { $crate::assume(MIN <= *value && *value <= MAX) };
value
}
/// Creates a ranged integer if the given value is in the range `MIN..=MAX`. #[inline(always)] pubconstfn new(value: $internal) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; if value < MIN || value > MAX {
None
} else { // Safety: The value is in range.
Some(unsafe { Self::new_unchecked(value) })
}
}
/// Creates a ranged integer with a statically known value. **Fails to compile** if the /// value is not in range. #[inline(always)] pubconstfn new_static<const VALUE: $internal>() -> Self {
<($type<MIN, VALUE>, $type<VALUE, MAX>) as $crate::traits::StaticIsValid>::ASSERT; // Safety: The value is in range. unsafe { Self::new_unchecked(VALUE) }
}
/// Creates a ranged integer with the given value, saturating if it is out of range. #[inline] pubconstfn new_saturating(value: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; if value < MIN { Self::MIN
} elseif value > MAX { Self::MAX
} else { // Safety: The value is in range. unsafe { Self::new_unchecked(value) }
}
}
/// Expand the range that the value may be in. **Fails to compile** if the new range is /// not a superset of the current range. pubconstfn expand<const NEW_MIN: $internal, const NEW_MAX: $internal>( self,
) -> $type<NEW_MIN, NEW_MAX> {
<$type<MIN, MAX> as $crate::traits::RangeIsValid>::ASSERT;
<$type<NEW_MIN, NEW_MAX> as $crate::traits::RangeIsValid>::ASSERT;
<($type<MIN, MAX>, $type<NEW_MIN, NEW_MAX>) as $crate::traits::ExpandIsValid>
::ASSERT; // Safety: The range is widened. unsafe { $type::new_unchecked(self.get()) }
}
/// Attempt to narrow the range that the value may be in. Returns `None` if the value /// is outside the new range. **Fails to compile** if the new range is not a subset of /// the current range. pubconstfn narrow< const NEW_MIN: $internal, const NEW_MAX: $internal,
>(self) -> Option<$type<NEW_MIN, NEW_MAX>> {
<$type<MIN, MAX> as $crate::traits::RangeIsValid>::ASSERT;
<$type<NEW_MIN, NEW_MAX> as $crate::traits::RangeIsValid>::ASSERT;
<($type<MIN, MAX>, $type<NEW_MIN, NEW_MAX>) as $crate::traits::NarrowIsValid>
::ASSERT;
$type::<NEW_MIN, NEW_MAX>::new(self.get())
}
/// Converts a string slice in a given base to an integer. /// /// The string is expected to be an optional `+` or `-` sign followed by digits. Leading /// and trailing whitespace represent an error. Digits are a subset of these characters, /// depending on `radix`: /// /// - `0-9` /// - `a-z` /// - `A-Z` /// /// # Panics /// /// Panics if `radix` is not in the range `2..=36`. /// /// # Examples /// /// Basic usage: /// /// ```rust #[doc = concat!("# use deranged::", stringify!($type), ";")] #[doc = concat!( "assert_eq!(",
stringify!($type), "::<5, 10>::from_str_radix(\"A\", 16), Ok(",
stringify!($type), "::new_static::<10>()));",
)] /// ``` #[inline] pubfn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; match $internal::from_str_radix(src, radix) {
Ok(value) if value > MAX => {
Err(ParseIntError { kind: IntErrorKind::PosOverflow })
}
Ok(value) if value < MIN => {
Err(ParseIntError { kind: IntErrorKind::NegOverflow })
} // Safety: If the value was out of range, it would have been caught in a // previous arm.
Ok(value) => Ok(unsafe { Self::new_unchecked(value) }),
Err(e) => Err(ParseIntError { kind: e.kind().clone() }),
}
}
/// Checked integer addition. Computes `self + rhs`, returning `None` if the resulting /// value is out of range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn checked_add(self, rhs: $internal) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new(const_try_opt!(self.get().checked_add(rhs)))
}
/// Unchecked integer addition. Computes `self + rhs`, assuming that the result is in /// range. /// /// # Safety /// /// The result of `self + rhs` must be in the range `MIN..=MAX`. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstunsafefn unchecked_add(self, rhs: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the result is in range. unsafe { Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_add(rhs)))
}
}
/// Checked integer addition. Computes `self - rhs`, returning `None` if the resulting /// value is out of range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn checked_sub(self, rhs: $internal) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new(const_try_opt!(self.get().checked_sub(rhs)))
}
/// Unchecked integer subtraction. Computes `self - rhs`, assuming that the result is in /// range. /// /// # Safety /// /// The result of `self - rhs` must be in the range `MIN..=MAX`. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstunsafefn unchecked_sub(self, rhs: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the result is in range. unsafe { Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_sub(rhs)))
}
}
/// Checked integer addition. Computes `self * rhs`, returning `None` if the resulting /// value is out of range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn checked_mul(self, rhs: $internal) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new(const_try_opt!(self.get().checked_mul(rhs)))
}
/// Unchecked integer multiplication. Computes `self * rhs`, assuming that the result is /// in range. /// /// # Safety /// /// The result of `self * rhs` must be in the range `MIN..=MAX`. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstunsafefn unchecked_mul(self, rhs: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the result is in range. unsafe { Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_mul(rhs)))
}
}
/// Checked integer addition. Computes `self / rhs`, returning `None` if `rhs == 0` or /// if the resulting value is out of range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn checked_div(self, rhs: $internal) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new(const_try_opt!(self.get().checked_div(rhs)))
}
/// Unchecked integer division. Computes `self / rhs`, assuming that `rhs != 0` and that /// the result is in range. /// /// # Safety /// /// `self` must not be zero and the result of `self / rhs` must be in the range /// `MIN..=MAX`. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstunsafefn unchecked_div(self, rhs: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the result is in range and that `rhs` is not // zero. unsafe { Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_div(rhs)))
}
}
/// Checked Euclidean division. Computes `self.div_euclid(rhs)`, returning `None` if /// `rhs == 0` or if the resulting value is out of range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn checked_div_euclid(self, rhs: $internal) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new(const_try_opt!(self.get().checked_div_euclid(rhs)))
}
/// Unchecked Euclidean division. Computes `self.div_euclid(rhs)`, assuming that /// `rhs != 0` and that the result is in range. /// /// # Safety /// /// `self` must not be zero and the result of `self.div_euclid(rhs)` must be in the /// range `MIN..=MAX`. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstunsafefn unchecked_div_euclid(self, rhs: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the result is in range and that `rhs` is not // zero. unsafe { Self::new_unchecked(
unsafe_unwrap_unchecked!(self.get().checked_div_euclid(rhs))
)
}
}
if_unsigned!($is_signed /// Remainder. Computes `self % rhs`, statically guaranteeing that the returned value /// is in range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn rem<const RHS_VALUE: $internal>( self,
rhs: $type<RHS_VALUE, RHS_VALUE>,
) -> $type<0, RHS_VALUE> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The result is guaranteed to be in range due to the nature of remainder on // unsigned integers. unsafe { $type::new_unchecked(self.get() % rhs.get()) }
});
/// Checked integer remainder. Computes `self % rhs`, returning `None` if `rhs == 0` or /// if the resulting value is out of range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn checked_rem(self, rhs: $internal) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new(const_try_opt!(self.get().checked_rem(rhs)))
}
/// Unchecked remainder. Computes `self % rhs`, assuming that `rhs != 0` and that the /// result is in range. /// /// # Safety /// /// `self` must not be zero and the result of `self % rhs` must be in the range /// `MIN..=MAX`. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstunsafefn unchecked_rem(self, rhs: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the result is in range and that `rhs` is not // zero. unsafe { Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_rem(rhs)))
}
}
/// Checked Euclidean remainder. Computes `self.rem_euclid(rhs)`, returning `None` if /// `rhs == 0` or if the resulting value is out of range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn checked_rem_euclid(self, rhs: $internal) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new(const_try_opt!(self.get().checked_rem_euclid(rhs)))
}
/// Unchecked Euclidean remainder. Computes `self.rem_euclid(rhs)`, assuming that /// `rhs != 0` and that the result is in range. /// /// # Safety /// /// `self` must not be zero and the result of `self.rem_euclid(rhs)` must be in the /// range `MIN..=MAX`. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstunsafefn unchecked_rem_euclid(self, rhs: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the result is in range and that `rhs` is not // zero. unsafe { Self::new_unchecked(
unsafe_unwrap_unchecked!(self.get().checked_rem_euclid(rhs))
)
}
}
/// Checked negation. Computes `-self`, returning `None` if the resulting value is out /// of range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn checked_neg(self) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new(const_try_opt!(self.get().checked_neg()))
}
/// Unchecked negation. Computes `-self`, assuming that `-self` is in range. /// /// # Safety /// /// The result of `-self` must be in the range `MIN..=MAX`. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstunsafefn unchecked_neg(self) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the result is in range. unsafe { Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_neg())) }
}
/// Negation. Computes `self.neg()`, **failing to compile** if the result is not /// guaranteed to be in range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstfn neg(self) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT;
<Selfas $crate::traits::NegIsSafe>::ASSERT; // Safety: The compiler asserts that the result is in range. unsafe { self.unchecked_neg() }
}
/// Checked shift left. Computes `self << rhs`, returning `None` if the resulting value /// is out of range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn checked_shl(self, rhs: u32) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new(const_try_opt!(self.get().checked_shl(rhs)))
}
/// Unchecked shift left. Computes `self << rhs`, assuming that the result is in range. /// /// # Safety /// /// The result of `self << rhs` must be in the range `MIN..=MAX`. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstunsafefn unchecked_shl(self, rhs: u32) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the result is in range. unsafe { Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_shl(rhs)))
}
}
/// Checked shift right. Computes `self >> rhs`, returning `None` if /// the resulting value is out of range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn checked_shr(self, rhs: u32) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new(const_try_opt!(self.get().checked_shr(rhs)))
}
/// Unchecked shift right. Computes `self >> rhs`, assuming that the result is in range. /// /// # Safety /// /// The result of `self >> rhs` must be in the range `MIN..=MAX`. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstunsafefn unchecked_shr(self, rhs: u32) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the result is in range. unsafe { Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_shr(rhs)))
}
}
if_signed!($is_signed /// Checked absolute value. Computes `self.abs()`, returning `None` if the resulting /// value is out of range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn checked_abs(self) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new(const_try_opt!(self.get().checked_abs()))
}
/// Unchecked absolute value. Computes `self.abs()`, assuming that the result is in /// range. /// /// # Safety /// /// The result of `self.abs()` must be in the range `MIN..=MAX`. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstunsafefn unchecked_abs(self) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the result is in range. unsafe { Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_abs())) }
}
/// Absolute value. Computes `self.abs()`, **failing to compile** if the result is not /// guaranteed to be in range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstfn abs(self) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT;
<Selfas $crate::traits::AbsIsSafe>::ASSERT; // Safety: The compiler asserts that the result is in range. unsafe { self.unchecked_abs() }
});
/// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if the resulting /// value is out of range. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn checked_pow(self, exp: u32) -> Option<Self> {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new(const_try_opt!(self.get().checked_pow(exp)))
}
/// Unchecked exponentiation. Computes `self.pow(exp)`, assuming that the result is in /// range. /// /// # Safety /// /// The result of `self.pow(exp)` must be in the range `MIN..=MAX`. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] pubconstunsafefn unchecked_pow(self, exp: u32) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the result is in range. unsafe { Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_pow(exp)))
}
}
/// Saturating integer addition. Computes `self + rhs`, saturating at the numeric /// bounds. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn saturating_add(self, rhs: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new_saturating(self.get().saturating_add(rhs))
}
/// Saturating integer subtraction. Computes `self - rhs`, saturating at the numeric /// bounds. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn saturating_sub(self, rhs: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new_saturating(self.get().saturating_sub(rhs))
}
if_signed!($is_signed /// Saturating integer negation. Computes `self - rhs`, saturating at the numeric /// bounds. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn saturating_neg(self) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new_saturating(self.get().saturating_neg())
});
if_signed!($is_signed /// Saturating absolute value. Computes `self.abs()`, saturating at the numeric bounds. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn saturating_abs(self) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new_saturating(self.get().saturating_abs())
});
/// Saturating integer multiplication. Computes `self * rhs`, saturating at the numeric /// bounds. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn saturating_mul(self, rhs: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new_saturating(self.get().saturating_mul(rhs))
}
/// Saturating integer exponentiation. Computes `self.pow(exp)`, saturating at the /// numeric bounds. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pubconstfn saturating_pow(self, exp: u32) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; Self::new_saturating(self.get().saturating_pow(exp))
}
/// Compute the `rem_euclid` of this type with its unsigned type equivalent // Not public because it doesn't match stdlib's "method_unsigned implemented only for signed type" tradition. // Also because this isn't implemented for normal types in std. // TODO maybe make public anyway? It is useful. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] #[allow(trivial_numeric_casts)] // needed since some casts have to send unsigned -> unsigned to handle signed -> unsigned constfn rem_euclid_unsigned(
rhs: $internal,
range_len: $unsigned_type
) -> $unsigned_type { #[allow(unused_comparisons)] if rhs >= 0 {
(rhs as $unsigned_type) % range_len
} else { // Let ux refer to an n bit unsigned and ix refer to an n bit signed integer. // Can't write -ux or ux::abs() method. This gets around compilation error. // `wrapping_sub` is to handle rhs = ix::MIN since ix::MIN = -ix::MAX-1 let rhs_abs = ($internal::wrapping_sub(0, rhs)) as $unsigned_type; // Largest multiple of range_len <= type::MAX is lowest if range_len * 2 > ux::MAX -> range_len >= ux::MAX / 2 + 1 // Also = 0 in mod range_len arithmetic. // Sub from this large number rhs_abs (same as sub -rhs = -(-rhs) = add rhs) to get rhs % range_len // ix::MIN = -2^(n-1) so 0 <= rhs_abs <= 2^(n-1) // ux::MAX / 2 + 1 = 2^(n-1) so this subtraction will always be a >= 0 after subtraction // Thus converting rhs signed negative to equivalent positive value in mod range_len arithmetic
((($unsigned_type::MAX / range_len) * range_len) - (rhs_abs)) % range_len
}
}
/// Wrapping integer addition. Computes `self + rhs`, wrapping around the numeric /// bounds. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] #[allow(trivial_numeric_casts)] // needed since some casts have to send unsigned -> unsigned to handle signed -> unsigned pubconstfn wrapping_add(self, rhs: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Forward to internal type's impl if same as type. if MIN == $internal::MIN && MAX == $internal::MAX { // Safety: std's wrapping methods match ranged arithmetic when the range is the internal datatype's range. returnunsafe { Self::new_unchecked(self.get().wrapping_add(rhs)) }
}
let inner = self.get();
// Won't overflow because of std impl forwarding. let range_len = MAX.abs_diff(MIN) + 1;
// Calculate the offset with proper handling for negative rhs let offset = Self::rem_euclid_unsigned(rhs, range_len);
let greater_vals = MAX.abs_diff(inner); // No wrap if offset <= greater_vals { // Safety: // if inner >= 0 -> No overflow beyond range (offset <= greater_vals) // if inner < 0: Same as >=0 with caveat: // `(signed as unsigned).wrapping_add(unsigned) as signed` is the same as // `signed::checked_add_unsigned(unsigned).unwrap()` or `wrapping_add_unsigned` // (the difference doesn't matter since it won't overflow), // but unsigned integers don't have either method so it won't compile that way. unsafe { Self::new_unchecked(
((inner as $unsigned_type).wrapping_add(offset)) as $internal
) }
} // Wrap else { // Safety: // - offset < range_len by rem_euclid (MIN + ... safe) // - offset > greater_vals from if statement (offset - (greater_vals + 1) safe) // // again using `(signed as unsigned).wrapping_add(unsigned) as signed` = `checked_add_unsigned` trick unsafe { Self::new_unchecked(
((MIN as $unsigned_type).wrapping_add(
offset - (greater_vals + 1)
)) as $internal
) }
}
}
/// Wrapping integer subtraction. Computes `self - rhs`, wrapping around the numeric /// bounds. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] #[allow(trivial_numeric_casts)] // needed since some casts have to send unsigned -> unsigned to handle signed -> unsigned pubconstfn wrapping_sub(self, rhs: $internal) -> Self {
<Selfas $crate::traits::RangeIsValid>::ASSERT; // Forward to internal type's impl if same as type. if MIN == $internal::MIN && MAX == $internal::MAX { // Safety: std's wrapping methods match ranged arithmetic when the range is the internal datatype's range. returnunsafe { Self::new_unchecked(self.get().wrapping_sub(rhs)) }
}
let inner = self.get();
// Won't overflow because of std impl forwarding. let range_len = MAX.abs_diff(MIN) + 1;
// Calculate the offset with proper handling for negative rhs let offset = Self::rem_euclid_unsigned(rhs, range_len);
let lesser_vals = MIN.abs_diff(inner); // No wrap if offset <= lesser_vals { // Safety: // if inner >= 0 -> No overflow beyond range (offset <= greater_vals) // if inner < 0: Same as >=0 with caveat: // `(signed as unsigned).wrapping_sub(unsigned) as signed` is the same as // `signed::checked_sub_unsigned(unsigned).unwrap()` or `wrapping_sub_unsigned` // (the difference doesn't matter since it won't overflow below 0), // but unsigned integers don't have either method so it won't compile that way. unsafe { Self::new_unchecked(
((inner as $unsigned_type).wrapping_sub(offset)) as $internal
) }
} // Wrap else { // Safety: // - offset < range_len by rem_euclid (MAX - ... safe) // - offset > lesser_vals from if statement (offset - (lesser_vals + 1) safe) // // again using `(signed as unsigned).wrapping_sub(unsigned) as signed` = `checked_sub_unsigned` trick unsafe { Self::new_unchecked(
((MAX as $unsigned_type).wrapping_sub(
offset - (lesser_vals + 1)
)) as $internal
) }
}
}
}
impl<const MIN: $internal, const MAX: $internal> $optional_type<MIN, MAX> { /// The value used as the niche. Must not be in the range `MIN..=MAX`. const NICHE: $internal = match (MIN, MAX) {
($internal::MIN, $internal::MAX) => panic!("type has no niche"),
($internal::MIN, _) => $internal::MAX,
(_, _) => $internal::MIN,
};
/// An optional ranged value that is not present. #[allow(non_upper_case_globals)] pubconst None: Self = Self(Self::NICHE);
/// Creates an optional ranged value that is present. #[allow(non_snake_case)] #[inline(always)] pubconstfn Some(value: $type<MIN, MAX>) -> Self {
<$type<MIN, MAX> as $crate::traits::RangeIsValid>::ASSERT; Self(value.get())
}
/// Returns the value as the standard library's [`Option`] type. #[inline(always)] pubconstfn get(self) -> Option<$type<MIN, MAX>> {
<$type<MIN, MAX> as $crate::traits::RangeIsValid>::ASSERT; ifself.0 == Self::NICHE {
None
} else { // Safety: A stored value that is not the niche is always in range.
Some(unsafe { $type::new_unchecked(self.0) })
}
}
/// Creates an optional ranged integer without checking the value. /// /// # Safety /// /// The value must be within the range `MIN..=MAX`. As the value used for niche /// value optimization is unspecified, the provided value must not be the niche /// value. #[inline(always)] pubconstunsafefn some_unchecked(value: $internal) -> Self {
<$type<MIN, MAX> as $crate::traits::RangeIsValid>::ASSERT; // Safety: The caller must ensure that the value is in range. unsafe { $crate::assume(MIN <= value && value <= MAX) }; Self(value)
}
/// Obtain the inner value of the struct. This is useful for comparisons. #[inline(always)] pub(crate) constfn inner(self) -> $internal {
<$type<MIN, MAX> as $crate::traits::RangeIsValid>::ASSERT; self.0
}
/// Returns `true` if the value is the niche value. #[inline(always)] pubconstfn is_none(self) -> bool {
<$type<MIN, MAX> as $crate::traits::RangeIsValid>::ASSERT; self.get().is_none()
}
/// Returns `true` if the value is not the niche value. #[inline(always)] pubconstfn is_some(self) -> bool {
<$type<MIN, MAX> as $crate::traits::RangeIsValid>::ASSERT; self.get().is_some()
}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.22Bemerkung:
(vorverarbeitet am 2026-06-19)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.