Bounded

Struct Bounded 

Source
pub struct Bounded<T: Integer, const N: u32>(/* private fields */);
Expand description

An integer value that requires only the N less 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 with in 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 withheld.

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 Boundeds 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 single-bit `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);

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);

// 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);

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_bitint();
assert_eq!(v.as_deref().copied(), Some(128));

// Fails because `128` doesn't fits into 6 bits.
let v: Option<Bounded<u16, 6>> = 128u32.try_into_bitint();
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 bits.

This method should be preferred to Self::from_expr whenever possible.

§Examples
use kernel::num::Bounded;

let v = Bounded::<i8, 4>::new::<7>();
assert_eq!(v.get(), 7);
Source§

impl<const N: u32> Bounded<i16, N>

Source

pub const fn new<const VALUE: i16>() -> 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::<i16, 4>::new::<7>();
assert_eq!(v.get(), 7);
Source§

impl<const N: u32> Bounded<i32, N>

Source

pub const fn new<const VALUE: i32>() -> 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::<i32, 4>::new::<7>();
assert_eq!(v.get(), 7);
Source§

impl<const N: u32> Bounded<i64, N>

Source

pub const fn new<const VALUE: i64>() -> 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::<i64, 4>::new::<7>();
assert_eq!(v.get(), 7);
Source§

impl<const N: u32> Bounded<isize, N>

Source

pub const fn new<const VALUE: isize>() -> 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::<isize, 4>::new::<7>();
assert_eq!(v.get(), 7);
Source§

impl<T, const N: u32> Bounded<T, N>
where T: Integer,

Source

pub fn try_new(value: T) -> Option<Self>

Attempts to turn value into a Bounded using N bits.

Returns None if value doesn’t fit within N bits.

§Examples
use kernel::num::Bounded;

let v = Bounded::<u8, 1>::try_new(1);
assert_eq!(v.as_deref().copied(), Some(1));

let v = Bounded::<i8, 4>::try_new(-2);
assert_eq!(v.as_deref().copied(), Some(-2));

// `0x1ff` doesn't fit into 8 unsigned bits.
let v = Bounded::<u32, 8>::try_new(0x1ff);
assert_eq!(v, None);

// The range of values representable with 4 bits is `[-8..=7]`. The following tests these
// limits.
let v = Bounded::<i8, 4>::try_new(-8);
assert_eq!(v.map(Bounded::get), Some(-8));
let v = Bounded::<i8, 4>::try_new(-9);
assert_eq!(v, None);
let v = Bounded::<i8, 4>::try_new(7);
assert_eq!(v.map(Bounded::get), Some(7));
let v = Bounded::<i8, 4>::try_new(8);
assert_eq!(v, None);
Source

pub fn from_expr(expr: T) -> Self

Checks that expr is valid for this type at compile-time and build a new value.

This relies on build_assert! and guaranteed optimization to perform validation at compile-time. If expr cannot be proved to be within the requested bounds at compile-time, use the fallible Self::try_new instead.

Limit this to simple, easily provable expressions, and prefer one of the Self::new constructors whenever possible as they statically validate the value instead of relying on compiler optimizations.

§Examples
use kernel::num::Bounded;

// Some undefined number.
let v: u32 = some_number();

// Triggers a build error as `v` cannot be asserted to fit within 4 bits...
// let _ = Bounded::<u32, 4>::from_expr(v);

// ... but this works as the compiler can assert the range from the mask.
let _ = Bounded::<u32, 4>::from_expr(v & 0xf);

// These expressions are simple enough to be proven correct, but since they are static the
// `new` constructor should be preferred.
assert_eq!(Bounded::<u8, 1>::from_expr(1).get(), 1);
assert_eq!(Bounded::<u16, 8>::from_expr(0xff).get(), 0xff);
Source

pub fn get(self) -> T

Returns the wrapped value as the backing type.

§Examples
use kernel::num::Bounded;

let v = Bounded::<u32, 4>::new::<7>();
assert_eq!(v.get(), 7u32);
Source

pub const fn extend<const M: u32>(self) -> Bounded<T, M>

Increases the number of bits usable for self.

This operation cannot fail.

§Examples
use kernel::num::Bounded;

let v = Bounded::<u32, 4>::new::<7>();
let larger_v = v.extend::<12>();
// The contained values are equal even though `larger_v` has a bigger capacity.
assert_eq!(larger_v, v);
Source

pub fn try_shrink<const M: u32>(self) -> Option<Bounded<T, M>>

Attempts to shrink the number of bits usable for self.

Returns None if the value of self cannot be represented within M bits.

§Examples
use kernel::num::Bounded;

let v = Bounded::<u32, 12>::new::<7>();

// `7` can be represented using 3 unsigned bits...
let smaller_v = v.try_shrink::<3>();
assert_eq!(smaller_v.as_deref().copied(), Some(7));

// ... but doesn't fit within `2` bits.
assert_eq!(v.try_shrink::<2>(), None);
Source

pub fn cast<U>(self) -> Bounded<U, N>
where U: TryFrom<T> + Integer + Integer<Signedness = T::Signedness>, T: Integer,

Casts self into a Bounded backed by a different storage type, but using the same number of valid bits.

Both T and U must be of same signedness, and U must be at least as large as N bits, or a build error will occur.

§Examples
use kernel::num::Bounded;

let v = Bounded::<u32, 12>::new::<127>();

let u16_v: Bounded<u16, 12> = v.cast();
assert_eq!(u16_v.get(), 127);

// This won't build: a `u8` is smaller than the required 12 bits.
// let _: Bounded<u8, 12> = v.cast();

Trait Implementations§

Source§

impl<T, const N: u32, const M: u32> Add<Bounded<T, M>> for Bounded<T, N>
where T: Integer + Add<Output = T>,

Source§

type Output = T

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Bounded<T, M>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T, const N: u32> Add<T> for Bounded<T, N>
where T: Integer + Add<Output = T>,

Source§

type Output = T

The resulting type after applying the + operator.
Source§

fn add(self, rhs: T) -> Self::Output

Performs the + operation. Read more
Source§

impl<T, const N: u32> Binary for Bounded<T, N>
where T: Integer + Binary,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const N: u32, const M: u32> BitAnd<Bounded<T, M>> for Bounded<T, N>
where T: Integer + BitAnd<Output = T>,

Source§

type Output = T

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Bounded<T, M>) -> Self::Output

Performs the & operation. Read more
Source§

impl<T, const N: u32> BitAnd<T> for Bounded<T, N>
where T: Integer + BitAnd<Output = T>,

Source§

type Output = T

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: T) -> Self::Output

Performs the & operation. Read more
Source§

impl<T, const N: u32, const M: u32> BitOr<Bounded<T, M>> for Bounded<T, N>
where T: Integer + BitOr<Output = T>,

Source§

type Output = T

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Bounded<T, M>) -> Self::Output

Performs the | operation. Read more
Source§

impl<T, const N: u32> BitOr<T> for Bounded<T, N>
where T: Integer + BitOr<Output = T>,

Source§

type Output = T

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: T) -> Self::Output

Performs the | operation. Read more
Source§

impl<T, const N: u32, const M: u32> BitXor<Bounded<T, M>> for Bounded<T, N>
where T: Integer + BitXor<Output = T>,

Source§

type Output = T

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Bounded<T, M>) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T, const N: u32> BitXor<T> for Bounded<T, N>
where T: Integer + BitXor<Output = T>,

Source§

type Output = T

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: T) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T: Clone + Integer, const N: u32> Clone for Bounded<T, N>

Source§

fn clone(&self) -> Bounded<T, N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Integer, const N: u32> Debug for Bounded<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default + Integer, const N: u32> Default for Bounded<T, N>

Source§

fn default() -> Bounded<T, N>

Returns the “default value” for a type. Read more
Source§

impl<T, const N: u32> Deref for Bounded<T, N>
where T: Integer,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T, const N: u32> Display for Bounded<T, N>
where T: Integer + Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const N: u32, const M: u32> Div<Bounded<T, M>> for Bounded<T, N>
where T: Integer + Div<Output = T>,

Source§

type Output = T

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Bounded<T, M>) -> Self::Output

Performs the / operation. Read more
Source§

impl<T, const N: u32> Div<T> for Bounded<T, N>
where T: Integer + Div<Output = T>,

Source§

type Output = T

The resulting type after applying the / operator.
Source§

fn div(self, rhs: T) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> From<Bounded<T, 1>> for bool
where T: Integer + Zeroable,

Source§

fn from(value: Bounded<T, 1>) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<Bounded<T, N>> for i16
where i16: Integer + TryFrom<T>, T: Integer<Signedness = <i16 as Integer>::Signedness>, Bounded<T, N>: FitsInXBits<{ _ }>,

Conversion from a Bounded with no more bits than a i16 and of same signedness into i16

Source§

fn from(value: Bounded<T, N>) -> i16

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<Bounded<T, N>> for i32
where i32: Integer + TryFrom<T>, T: Integer<Signedness = <i32 as Integer>::Signedness>, Bounded<T, N>: FitsInXBits<{ _ }>,

Conversion from a Bounded with no more bits than a i32 and of same signedness into i32

Source§

fn from(value: Bounded<T, N>) -> i32

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<Bounded<T, N>> for i64
where i64: Integer + TryFrom<T>, T: Integer<Signedness = <i64 as Integer>::Signedness>, Bounded<T, N>: FitsInXBits<{ _ }>,

Conversion from a Bounded with no more bits than a i64 and of same signedness into i64

Source§

fn from(value: Bounded<T, N>) -> i64

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<Bounded<T, N>> for i8
where i8: Integer + TryFrom<T>, T: Integer<Signedness = <i8 as Integer>::Signedness>, Bounded<T, N>: FitsInXBits<{ _ }>,

Conversion from a Bounded with no more bits than a i8 and of same signedness into i8

Source§

fn from(value: Bounded<T, N>) -> i8

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<Bounded<T, N>> for isize
where isize: Integer + TryFrom<T>, T: Integer<Signedness = <isize as Integer>::Signedness>, Bounded<T, N>: FitsInXBits<{ _ }>,

Conversion from a Bounded with no more bits than a isize and of same signedness into isize

Source§

fn from(value: Bounded<T, N>) -> isize

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<Bounded<T, N>> for u16
where u16: Integer + TryFrom<T>, T: Integer<Signedness = <u16 as Integer>::Signedness>, Bounded<T, N>: FitsInXBits<{ _ }>,

Conversion from a Bounded with no more bits than a u16 and of same signedness into u16

Source§

fn from(value: Bounded<T, N>) -> u16

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<Bounded<T, N>> for u32
where u32: Integer + TryFrom<T>, T: Integer<Signedness = <u32 as Integer>::Signedness>, Bounded<T, N>: FitsInXBits<{ _ }>,

Conversion from a Bounded with no more bits than a u32 and of same signedness into u32

Source§

fn from(value: Bounded<T, N>) -> u32

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<Bounded<T, N>> for u64
where u64: Integer + TryFrom<T>, T: Integer<Signedness = <u64 as Integer>::Signedness>, Bounded<T, N>: FitsInXBits<{ _ }>,

Conversion from a Bounded with no more bits than a u64 and of same signedness into u64

Source§

fn from(value: Bounded<T, N>) -> u64

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<Bounded<T, N>> for u8
where u8: Integer + TryFrom<T>, T: Integer<Signedness = <u8 as Integer>::Signedness>, Bounded<T, N>: FitsInXBits<{ _ }>,

Conversion from a Bounded with no more bits than a u8 and of same signedness into u8

Source§

fn from(value: Bounded<T, N>) -> u8

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<Bounded<T, N>> for usize
where usize: Integer + TryFrom<T>, T: Integer<Signedness = <usize as Integer>::Signedness>, Bounded<T, N>: FitsInXBits<{ _ }>,

Conversion from a Bounded with no more bits than a usize and of same signedness into usize

Source§

fn from(value: Bounded<T, N>) -> usize

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<bool> for Bounded<T, N>
where T: Integer + From<bool>,

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<i16> for Bounded<T, N>
where i16: Integer, T: Integer<Signedness = <i16 as Integer>::Signedness> + From<i16>, Self: AtLeastXBits<{ _ }>,

Conversion from a i16 into a Bounded of same signedness with enough bits to store it.

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<i32> for Bounded<T, N>
where i32: Integer, T: Integer<Signedness = <i32 as Integer>::Signedness> + From<i32>, Self: AtLeastXBits<{ _ }>,

Conversion from a i32 into a Bounded of same signedness with enough bits to store it.

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<i64> for Bounded<T, N>
where i64: Integer, T: Integer<Signedness = <i64 as Integer>::Signedness> + From<i64>, Self: AtLeastXBits<{ _ }>,

Conversion from a i64 into a Bounded of same signedness with enough bits to store it.

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<i8> for Bounded<T, N>
where i8: Integer, T: Integer<Signedness = <i8 as Integer>::Signedness> + From<i8>, Self: AtLeastXBits<{ _ }>,

Conversion from a i8 into a Bounded of same signedness with enough bits to store it.

Source§

fn from(value: i8) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<isize> for Bounded<T, N>
where isize: Integer, T: Integer<Signedness = <isize as Integer>::Signedness> + From<isize>, Self: AtLeastXBits<{ _ }>,

Conversion from a isize into a Bounded of same signedness with enough bits to store it.

Source§

fn from(value: isize) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<u16> for Bounded<T, N>
where u16: Integer, T: Integer<Signedness = <u16 as Integer>::Signedness> + From<u16>, Self: AtLeastXBits<{ _ }>,

Conversion from a u16 into a Bounded of same signedness with enough bits to store it.

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<u32> for Bounded<T, N>
where u32: Integer, T: Integer<Signedness = <u32 as Integer>::Signedness> + From<u32>, Self: AtLeastXBits<{ _ }>,

Conversion from a u32 into a Bounded of same signedness with enough bits to store it.

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<u64> for Bounded<T, N>
where u64: Integer, T: Integer<Signedness = <u64 as Integer>::Signedness> + From<u64>, Self: AtLeastXBits<{ _ }>,

Conversion from a u64 into a Bounded of same signedness with enough bits to store it.

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<u8> for Bounded<T, N>
where u8: Integer, T: Integer<Signedness = <u8 as Integer>::Signedness> + From<u8>, Self: AtLeastXBits<{ _ }>,

Conversion from a u8 into a Bounded of same signedness with enough bits to store it.

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: u32> From<usize> for Bounded<T, N>
where usize: Integer, T: Integer<Signedness = <usize as Integer>::Signedness> + From<usize>, Self: AtLeastXBits<{ _ }>,

Conversion from a usize into a Bounded of same signedness with enough bits to store it.

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash + Integer, const N: u32> Hash for Bounded<T, N>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T, const N: u32> LowerExp for Bounded<T, N>
where T: Integer + LowerExp,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const N: u32> LowerHex for Bounded<T, N>
where T: Integer + LowerHex,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const N: u32, const M: u32> Mul<Bounded<T, M>> for Bounded<T, N>
where T: Integer + Mul<Output = T>,

Source§

type Output = T

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Bounded<T, M>) -> Self::Output

Performs the * operation. Read more
Source§

impl<T, const N: u32> Mul<T> for Bounded<T, N>
where T: Integer + Mul<Output = T>,

Source§

type Output = T

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: T) -> Self::Output

Performs the * operation. Read more
Source§

impl<T, const N: u32> Neg for Bounded<T, N>
where T: Integer + Neg<Output = T>,

Source§

type Output = T

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<T, const N: u32> Not for Bounded<T, N>
where T: Integer + Not<Output = T>,

Source§

type Output = T

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl<T, const N: u32> Octal for Bounded<T, N>
where T: Integer + Octal,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const N: u32> Ord for Bounded<T, N>
where T: Integer + Ord,

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T, U, const N: u32, const M: u32> PartialEq<Bounded<U, M>> for Bounded<T, N>
where T: Integer + PartialEq<U>, U: Integer,

Source§

fn eq(&self, other: &Bounded<U, M>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, const N: u32> PartialEq<T> for Bounded<T, N>
where T: Integer + PartialEq,

Source§

fn eq(&self, other: &T) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, const N: u32, const M: u32> PartialOrd<Bounded<U, M>> for Bounded<T, N>
where T: Integer + PartialOrd<U>, U: Integer,

Source§

fn partial_cmp(&self, other: &Bounded<U, M>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T, const N: u32> PartialOrd<T> for Bounded<T, N>
where T: Integer + PartialOrd,

Source§

fn partial_cmp(&self, other: &T) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T, const N: u32, const M: u32> Rem<Bounded<T, M>> for Bounded<T, N>
where T: Integer + Rem<Output = T>,

Source§

type Output = T

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Bounded<T, M>) -> Self::Output

Performs the % operation. Read more
Source§

impl<T, const N: u32> Rem<T> for Bounded<T, N>
where T: Integer + Rem<Output = T>,

Source§

type Output = T

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: T) -> Self::Output

Performs the % operation. Read more
Source§

impl<T, const N: u32, const M: u32> Sub<Bounded<T, M>> for Bounded<T, N>
where T: Integer + Sub<Output = T>,

Source§

type Output = T

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Bounded<T, M>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T, const N: u32> Sub<T> for Bounded<T, N>
where T: Integer + Sub<Output = T>,

Source§

type Output = T

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: T) -> Self::Output

Performs the - operation. Read more
Source§

impl<T, const N: u32> UpperExp for Bounded<T, N>
where T: Integer + UpperExp,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const N: u32> UpperHex for Bounded<T, N>
where T: Integer + UpperHex,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Copy + Integer, const N: u32> Copy for Bounded<T, N>

Source§

impl<T, const N: u32> Eq for Bounded<T, N>
where T: Integer,

Auto Trait Implementations§

§

impl<T, const N: u32> Freeze for Bounded<T, N>
where T: Freeze,

§

impl<T, const N: u32> RefUnwindSafe for Bounded<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: u32> Send for Bounded<T, N>
where T: Send,

§

impl<T, const N: u32> Sync for Bounded<T, N>
where T: Sync,

§

impl<T, const N: u32> Unpin for Bounded<T, N>
where T: Unpin,

§

impl<T, const N: u32> UnwindSafe for Bounded<T, N>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Init<T> for T

Source§

unsafe fn __init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PinInit<T> for T

Source§

unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U, const N: u32> TryIntoBounded<T, N> for U
where T: Integer, U: TryInto<T>,

Source§

fn try_into_bitint(self) -> Option<Bounded<T, N>>

Attempts to convert self into a Bounded using N bits. Read more
Source§

impl<T> Writer for T
where T: Debug,

Source§

fn write(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter.