Lines Matching refs:N
37 /// Returns `true` if `value` can be represented with at most `N` bits in a `T`.
43 /// An integer value that requires only the `N` least significant bits of the wrapped type to be
52 /// - `N` is greater than `0`.
53 /// - `N` is less than or equal to `T::BITS`.
54 /// - Stored values can be represented with at most `N` bits.
230 pub struct Bounded<T: Integer, const N: u32>(T);
238 impl<const N: u32> Bounded<$type, N> {
241 /// Fails at build time if `VALUE` cannot be represented with `N` bits.
258 const_assert!(fits_within!(VALUE, $type, N));
261 // `N` bits.
274 impl<T, const N: u32> Bounded<T, N>
285 /// The caller must ensure that `value` can be represented within `N` bits.
288 // `N` cannot be zero.
289 const_assert!(N != 0);
290 // The backing type is at least as large as `N` bits.
291 const_assert!(N <= T::BITS);
293 // INVARIANT: The caller ensures `value` fits within `N` bits.
297 /// Attempts to turn `value` into a `Bounded` using `N` bits.
299 /// Returns [`None`] if `value` doesn't fit within `N` bits.
328 fits_within(value, N).then(|| {
329 // SAFETY: `fits_within` confirmed that `value` can be represented within `N` bits.
368 fits_within(expr, N),
372 // SAFETY: `fits_within` confirmed that `expr` can be represented within `N` bits.
409 M >= N,
413 // SAFETY: The value did fit within `N` bits, so it will all the more fit within
444 /// `N` bits, or a build error will occur.
459 pub fn cast<U>(self) -> Bounded<U, N>
465 // SAFETY: The converted value is represented using `N` bits, `U` can contain `N` bits, and
470 // `N` bits, and with the same signedness.
475 /// N - SHIFT`.
488 const { assert!(RES + SHIFT >= N) }
491 // represent the shifted value by as much, and just asserted that `RES >= N - SHIFT`.
496 /// N + SHIFT`.
509 const { assert!(RES >= N + SHIFT) }
512 // represent the shifted value by as much, and just asserted that `RES >= N + SHIFT`.
517 impl<T, const N: u32> Deref for Bounded<T, N>
525 if !fits_within(self.0, N) {
550 pub trait TryIntoBounded<T: Integer, const N: u32> {
551 /// Attempts to convert `self` into a [`Bounded`] using `N` bits.
554 fn try_into_bounded(self) -> Option<Bounded<T, N>>;
558 impl<T, U, const N: u32> TryIntoBounded<T, N> for U
563 fn try_into_bounded(self) -> Option<Bounded<T, N>> {
570 impl<T, U, const N: u32, const M: u32> PartialEq<Bounded<U, M>> for Bounded<T, N>
581 impl<T, const N: u32> Eq for Bounded<T, N> where T: Integer {}
583 impl<T, U, const N: u32, const M: u32> PartialOrd<Bounded<U, M>> for Bounded<T, N>
594 impl<T, const N: u32> Ord for Bounded<T, N>
606 impl<T, const N: u32> PartialEq<T> for Bounded<T, N>
616 impl<T, const N: u32> PartialOrd<T> for Bounded<T, N>
628 impl<T, const N: u32, const M: u32> ops::Add<Bounded<T, M>> for Bounded<T, N>
640 impl<T, const N: u32, const M: u32> ops::BitAnd<Bounded<T, M>> for Bounded<T, N>
652 impl<T, const N: u32, const M: u32> ops::BitOr<Bounded<T, M>> for Bounded<T, N>
664 impl<T, const N: u32, const M: u32> ops::BitXor<Bounded<T, M>> for Bounded<T, N>
676 impl<T, const N: u32, const M: u32> ops::Div<Bounded<T, M>> for Bounded<T, N>
688 impl<T, const N: u32, const M: u32> ops::Mul<Bounded<T, M>> for Bounded<T, N>
700 impl<T, const N: u32, const M: u32> ops::Rem<Bounded<T, M>> for Bounded<T, N>
712 impl<T, const N: u32, const M: u32> ops::Sub<Bounded<T, M>> for Bounded<T, N>
726 impl<T, const N: u32> ops::Add<T> for Bounded<T, N>
738 impl<T, const N: u32> ops::BitAnd<T> for Bounded<T, N>
750 impl<T, const N: u32> ops::BitOr<T> for Bounded<T, N>
762 impl<T, const N: u32> ops::BitXor<T> for Bounded<T, N>
774 impl<T, const N: u32> ops::Div<T> for Bounded<T, N>
786 impl<T, const N: u32> ops::Mul<T> for Bounded<T, N>
798 impl<T, const N: u32> ops::Neg for Bounded<T, N>
810 impl<T, const N: u32> ops::Not for Bounded<T, N>
822 impl<T, const N: u32> ops::Rem<T> for Bounded<T, N>
834 impl<T, const N: u32> ops::Sub<T> for Bounded<T, N>
848 impl<T, const N: u32> fmt::Display for Bounded<T, N>
858 impl<T, const N: u32> fmt::Binary for Bounded<T, N>
868 impl<T, const N: u32> fmt::LowerExp for Bounded<T, N>
878 impl<T, const N: u32> fmt::LowerHex for Bounded<T, N>
888 impl<T, const N: u32> fmt::Octal for Bounded<T, N>
898 impl<T, const N: u32> fmt::UpperExp for Bounded<T, N>
908 impl<T, const N: u32> fmt::UpperHex for Bounded<T, N>
929 /// Local trait expressing the fact that a given [`Bounded`] has at least `N` bits used for value
931 trait AtLeastXBits<const N: usize> {}
982 impl<T, const N: u32> From<$type> for Bounded<T, N>
989 // SAFETY: The trait bound on `Self` guarantees that `N` bits is
1003 /// Local trait expressing the fact that a given [`Bounded`] fits into a primitive type of `N` bits,
1005 trait FitsInXBits<const N: usize> {}
1058 impl<T, const N: u32> From<Bounded<T, N>> for $type
1062 Bounded<T, N>: FitsInXBits<{ <$type as Integer>::BITS as usize }>,
1064 fn from(value: Bounded<T, N>) -> $type {
1066 // is constrained to `N` bits) can fit into the destination type, so this
1091 impl<T, const N: u32> From<bool> for Bounded<T, N>
1097 // integer type for any `N` > 0.