pub struct Index<T: Bitset>(/* private fields */);Expand description
Struct allowing to safely index T, where T implements Bitset.
Implementations§
Source§impl<T> Index<T>where
T: Bitset,
impl<T> Index<T>where
T: Bitset,
Sourcepub const ONE: Self
pub const ONE: Self
Value of 1 for Index.
Shortcut from having to use Index::<T>::from_usize(1)
Sourcepub const MIN: Self
pub const MIN: Self
Minimum value for Index.
Shortcut from having to use Index::<T>::from_usize(usize::MIN)
Sourcepub const MAX: Self
pub const MAX: Self
Maximum value for Index.
Shortcut from having to use Index::<T>::from_usize(T::BITS - 1)
Sourcepub const fn from_usize(value: usize) -> Self
pub const fn from_usize(value: usize) -> Self
Constructs a valid Index<T> value from usize.
Only use this, if you’re certain, that resulting index will be valid.
Valid inputs to this function are in range 0..(T::BYTE_SIZE * 8).
This function exists solely to avoid situations, where you are certain of
the validity and don’t want to write:
Index::<T>::try_from_usize(value).expect("value should be in range")
§Panics
This function panics, if the value supplied is outside the range 0..(T::BYTE_SIZE * 8).
§Examples
use bitworks::prelude::{Bitset8, Index, Index8};
let index = Index8::from_usize(7);
assert_eq!(index.into_inner(), 7);Sourcepub const fn try_from_usize(value: usize) -> ConvResult<Self>
pub const fn try_from_usize(value: usize) -> ConvResult<Self>
Tries constructing an Index<T> value from usize.
Meant to be used in situations, where you’re uncertain, if the value supplied would make
a valid Index<T> value.
§Errors
This function errors, if the value supplied is outside the range 0..(T::BYTE_SIZE * 8).
§Examples
use bitworks::prelude::{Bitset8, Index, Index8};
let index = Index8::try_from_usize(7)?;
assert_eq!(index.into_inner(), 7);Sourcepub const fn into_inner(&self) -> usize
pub const fn into_inner(&self) -> usize
Sourcepub const fn checked_add(&self, other: Self) -> Option<Self>
pub const fn checked_add(&self, other: Self) -> Option<Self>
Returns Some Index, that is the sum of self and other, or None on overflow.
§Examples
use bitworks::prelude::{Bitset8, Index, Index8};
let a = Index8::ONE;
let b = Index8::ONE;
let c = a.checked_add(b);
assert_eq!(c.unwrap().into_inner(), 2);
let d = Index8::MAX;
let e = Index8::ONE;
let f = d.checked_add(e);
assert_eq!(f, None);Sourcepub const fn checked_sub(&self, other: Self) -> Option<Self>
pub const fn checked_sub(&self, other: Self) -> Option<Self>
Returns Some Index, that is the difference of self and other, or None on overflow.
§Examples
use bitworks::prelude::{Bitset8, Index, Index8};
let a = Index8::MAX;
let b = Index8::ONE;
let c = a.checked_sub(b);
assert_eq!(c.unwrap().into_inner(), 6);
let d = Index8::MIN;
let e = Index8::ONE;
let f = d.checked_sub(e);
assert_eq!(f, None);Sourcepub const fn saturating_add(&self, other: Self) -> Self
pub const fn saturating_add(&self, other: Self) -> Self
Returns Index, that is sum of self and other,
or Index::<T>::MAX on overflow.
§Examples
use bitworks::prelude::{Bitset8, Index, Index8};
let a = Index8::ONE;
let b = Index8::ONE;
let c = a.saturating_add(b);
assert_eq!(c.into_inner(), 2);
let d = Index8::MAX;
let e = Index8::ONE;
let f = d.saturating_add(e);
assert_eq!(f.into_inner(), 7);Sourcepub const fn saturating_sub(&self, other: Self) -> Self
pub const fn saturating_sub(&self, other: Self) -> Self
Returns Index, that is difference of self and other,
or Index::<T>::MIN on overflow.
§Examples
use bitworks::prelude::{Bitset8, Index, Index8};
let a = Index8::MAX;
let b = Index8::ONE;
let c = a.saturating_sub(b);
assert_eq!(c.into_inner(), 6);
let d = Index8::MIN;
let e = Index8::ONE;
let f = d.saturating_sub(e);
assert_eq!(f.into_inner(), 0);Sourcepub fn to_other<U>(self) -> Index<U>where
U: Bitset,
pub fn to_other<U>(self) -> Index<U>where
U: Bitset,
Conversion between indeces.
§Panics
Panics if U::BIT_SIZE is smaller, than T::BIT_SIZE.
§Examples
use bitworks::prelude::{Bitset8, Bitset16, Index, Index8, Index16};
let a16 = Index16::from_usize(7);
let b16 = Index16::from_usize(8);
let a8: Index8 = a16.to_other();
assert_eq!(a8.into_inner(), 7);
// The following will panic!
// let b8: Index8 = b16.to_other();Sourcepub fn to_other_saturating<U>(self) -> Index<U>where
U: Bitset,
pub fn to_other_saturating<U>(self) -> Index<U>where
U: Bitset,
Conversion between indeces, with saturation on overflow.
§Examples
use bitworks::prelude::{Bitset8, Bitset16, Index, Index8, Index16};
let a16 = Index16::from_usize(7);
let b16 = Index16::from_usize(8);
let a8: Index8 = a16.to_other_saturating();
assert_eq!(a8.into_inner(), 7);
let b8: Index8 = b16.to_other_saturating();
assert_eq!(b8.into_inner(), 7);Sourcepub fn try_to_other<U>(self) -> ConvResult<Index<U>>where
U: Bitset,
pub fn try_to_other<U>(self) -> ConvResult<Index<U>>where
U: Bitset,
Attempted conversion between BisetIndexes.
§Errors
U::BIT_SIZE is smaller, than T::BIT_SIZE.
§Examples
use bitworks::prelude::{Bitset8, Bitset16, Index, Index8, Index16};
let a16 = Index16::from_usize(7);
let b16 = Index16::from_usize(8);
let a8 = a16.try_to_other::<Bitset8>();
assert_eq!(a8.ok(), Some(Index8::from_usize(7)));
let b8 = b16.try_to_other::<Bitset8>();
assert_eq!(b8.ok(), None);Sourcepub const fn byte_index(self) -> usize
pub const fn byte_index(self) -> usize
Returns index of byte, where the given index falls into.
This function is useful in cases, where you are working with Bitset
as with an array of bytes. As Index is always valid, this function will
return a valid index of the byte in the Bitset.
§Examples
use bitworks::prelude::{Bitset8, Bitset16, Index, Index16};
let a = Index16::from_usize(7);
let b = Index16::from_usize(8);
assert_eq!(a.byte_index(), 0); // 7 / 8 = 0
assert_eq!(b.byte_index(), 1); // 8 / 8 = 1Sourcepub const fn bit_index(self) -> usize
pub const fn bit_index(self) -> usize
Returns index of bit within it’s byte.
This function is useful in cases, where you are working with Bitset
as with an array of bytes. As Index is always valid, this function will
return a valid index of the bit in it’s byte in the Bitset.
§Examples
use bitworks::prelude::{Bitset8, Bitset16, Index, Index16};
let a = Index16::from_usize(7);
let b = Index16::from_usize(8);
assert_eq!(a.bit_index(), 7); // 7 % 8 = 7
assert_eq!(b.bit_index(), 0); // 8 % 8 = 0Sourcepub const fn bitmask(self) -> u8
pub const fn bitmask(self) -> u8
Returns a bitmask, with only given index bit set within it’s byte.
This function is useful in cases, where you are working with Bitset
as with an array of bytes. As Index is always valid, this function will
return a valid bitmask. This function is defined as (1 << Index::bit_index).
§Examples
use bitworks::prelude::{Bitset8, Bitset16, Index, Index16};
let a = Index16::from_usize(7);
let b = Index16::from_usize(8);
assert_eq!(a.bitmask(), 0b10000000); // 1 << 7
assert_eq!(b.bitmask(), 0b00000001); // 1 << 0