Text of rust.docs.kernel.org › kernel › num › bounded › struct.Bounded.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Bounded in kernel::num::bounded - Rust

Skip to main content Bounded kernel :: num :: bounded Struct Bounded Source pub struct Bounded<T: Integer , const N: u32 >( /* private fields */ ); Expand description An integer value that requires only the N least significant bits of the wrapped type to be encoded. This limits the number of usable bits in the wrapped integer type, and thus the stored value to a narrower range, which provides guarantees that can be useful when working within e.g. bitfields. § Invariants N is greater than 0 .

N is less than or equal to T::BITS . Stored values can be represented with at most N bits. § Examples The preferred way to create values is through constants and the Bounded::new family of constructors, as they trigger a build error if the type invariants cannot be upheld. use kernel::num::Bounded; // An unsigned 8-bit integer, of which only the 4 LSBs are used. // The value `15` is statically validated to fit that constraint at build time. let v = Bounded::<u8, 4 >::new::< 15 >(); assert_eq! (v.get(), 15 ); // Same using signed values. let v = Bounded::<i8, 4 >::new::<- 8 >(); assert_eq! (v.get(), - 8 ); // This doesn't build: a `u8` is smaller than the requested 9 bits. // let _ = Bounded::<u8, 9>::new::<10>(); // This also doesn't build: the requested value doesn't fit within 4 signed bits. // let _ = Bounded::<i8, 4>::new::<8>(); Values can also be validated at runtime with Bounded::try_new . use kernel::num::Bounded; // This succeeds because `15` can be represented with 4 unsigned bits. assert! (Bounded::<u8, 4 >::try_new( 15 ).is_some()); // This fails because `16` cannot be represented with 4 unsigned bits. assert! (Bounded::<u8, 4 >::try_new( 16 ).is_none()); Non-constant expressions can be validated at build-time thanks to compiler optimizations.

This should be used with caution, on simple expressions only. use kernel::num::Bounded; // Here the compiler can infer from the mask that the type invariants are not violated, even // though the value returned by `some_number` is not statically known. let v = Bounded::<u32, 4 >::from_expr(some_number() & 0xf ); Comparison and arithmetic operations are supported on Bounded s with a compatible backing type, regardless of their number of valid bits. use kernel::num::Bounded; let v1 = Bounded::<u32, 8 >::new::< 4 >(); let v2 = Bounded::<u32, 4 >::new::< 15 >(); assert! (v1 != v2); assert! (v1 < v2); assert_eq! (v1 + v2, 19 ); assert_eq! (v2 % v1, 3 ); These operations are also supported between a Bounded and its backing type. use kernel::num::Bounded; let v = Bounded::<u8, 4 >::new::< 15 >(); assert! (v == 15 ); assert! (v > 12 ); assert_eq! (v + 5 , 20 ); assert_eq! (v / 3 , 5 ); A change of backing types is possible using Bounded::cast , and the number of valid bits can be extended or reduced with Bounded::extend and Bounded::try_shrink . use kernel::num::Bounded; let v = Bounded::<u32, 12 >::new::< 127 >(); // Changes backing type from `u32` to `u16`. let _ : Bounded<u16, 12 > = v.cast(); // This does not build, as `u8` is smaller than 12 bits. // let _: Bounded<u8, 12> = v.cast(); // We can safely extend the number of bits... let _ = v.extend::< 15 >(); // ... to the limits of the backing type.

This doesn't build as a `u32` cannot contain 33 bits. // let _ = v.extend::<33>(); // Reducing the number of bits is validated at runtime. This works because `127` can be // represented with 8 bits. assert! (v.try_shrink::< 8 >().is_some()); // ... but not with 6, so this fails. assert! (v.try_shrink::< 6 >().is_none()); Infallible conversions from a primitive integer to a large-enough Bounded are supported. use kernel::num::Bounded; // This unsigned `Bounded` has 8 bits, so it can represent any `u8`. let v = Bounded::<u32, 8 >::from( 128u8 ); assert_eq! (v.get(), 128 ); // This signed `Bounded` has 8 bits, so it can represent any `i8`. let v = Bounded::<i32, 8 >::from(- 128i8 ); assert_eq! (v.get(), - 128 ); // This doesn't build, as this 6-bit `Bounded` does not have enough capacity to represent a // `u8` (regardless of the passed value). // let _ = Bounded::<u32, 6>::from(10u8); // Booleans can be converted into unsigned `Bounded`s. let v = Bounded::<u64, 1 >::from( false ); assert_eq! (v.get(), 0 ); let v = Bounded::<u64, 1 >::from( true ); assert_eq! (v.get(), 1 ); // This does not build because `i8` is signed. // let _ = Bounded::<i8, 2>::from(true); Infallible conversions from a Bounded to a primitive integer are also supported, and dependent on the number of bits used for value representation, not on the backing type. use kernel::num::Bounded; // Even though its backing type is `u32`, this `Bounded` only uses 6 bits and thus can safely // be converted to a `u8`. let v = Bounded::<u32, 6 >::new::< 63 >(); assert_eq! (u8::from(v), 63 ); // Same using signed values. let v = Bounded::<i32, 8 >::new::<- 128 >(); assert_eq! (i8::from(v), - 128 ); // This however does not build, as 10 bits won't fit into a `u8` (regardless of the actually // contained value). let _v = Bounded::<u32, 10 >::new::< 10 >(); // assert_eq!(u8::from(_v), 10); // Unsigned single-bit `Bounded`s can be converted into a boolean. let v = Bounded::<u8, 1 >::new::< 1 >(); assert_eq! (bool::from(v), true ); let v = Bounded::<u8, 1 >::new::< 0 >(); assert_eq! (bool::from(v), false ); // This does not build because `i8` is signed. // let v = Bounded::<i8, 1>::new::<-1>(); // let _ = bool::from(v); Fallible conversions from any primitive integer to any Bounded are also supported using the TryIntoBounded trait. use kernel::num::{Bounded, TryIntoBounded}; // Succeeds because `128` fits into 8 bits. let v: Option <Bounded<u16, 8 >> = 128u32 .try_into_bounded(); assert_eq! (v.as_deref().copied(), Some ( 128 )); // Fails because `128` doesn't fit into 6 bits. let v: Option <Bounded<u16, 6 >> = 128u32 .try_into_bounded(); assert_eq! (v, None ); Implementations § Source § impl<const N: u32 > Bounded < u8 , N> Source pub const fn new <const VALUE: u8 >() -> Self Creates a Bounded for the constant VALUE .

Fails at build time if VALUE cannot be represented with N bits. This method should be preferred to Self::from_expr whenever possible. § Examples use kernel::num::Bounded; let v = Bounded::<u8, 4 >::new::< 7 >(); assert_eq! (v.get(), 7 ); Source § impl<const N: u32 > Bounded < u16 , N> Source pub const fn new <const VALUE: u16 >() -> Self Creates a Bounded for the constant VALUE . Fails at build time if VALUE cannot be represented with N bits.

This method should be preferred to Self::from_expr whenever possible. § Examples use kernel::num::Bounded; let v = Bounded::<u16, 4 >::new::< 7 >(); assert_eq! (v.get(), 7 ); Source § impl<const N: u32 > Bounded < u32 , N> Source pub const fn new <const VALUE: u32 >() -> Self Creates a Bounded for the constant VALUE . Fails at build time if VALUE cannot be represented with N bits. This method should be preferred to Self::from_expr whenever possible. § Examples use kernel::num::Bounded; let v = Bounded::<u32, 4 >::new::< 7 >(); assert_eq! (v.get(), 7 ); Source § impl<const N: u32 > Bounded < u64 , N> Source pub const fn new <const VALUE: u64 >() -> Self Creates a Bounded for the constant VALUE .

Fails at build time if VALUE cannot be represented with N bits. This method should be preferred to Self::from_expr whenever possible. § Examples use kernel::num::Bounded; let v = Bounded::<u64, 4 >::new::< 7 >(); assert_eq! (v.get(), 7 ); Source § impl<const N: u32 > Bounded < usize , N> Source pub const fn new <const VALUE: usize >() -> Self Creates a Bounded for the constant VALUE . Fails at build time if VALUE cannot be represented with N bits.

This method should be preferred to Self::from_expr whenever possible. § Examples use kernel::num::Bounded; let v = Bounded::<usize, 4 >::new::< 7 >(); assert_eq! (v.get(), 7 ); Source § impl<const N: u32 > Bounded < i8 , N> Source pub const fn new <const VALUE: i8 >() -> Self Creates a Bounded for the constant VALUE . Fails at build time if VALUE cannot be represented with N

The rest of this page is on the live site.