Skip to main content

BitIndex

Struct BitIndex 

Source
pub struct BitIndex<T> { /* private fields */ }
Expand description

Proof that a value is a valid bit index for T (0 <= index < T::BITS), so a shift by it can’t overflow, panic, or hit the “shift >= BITS” UB.

Like PowerOfTwo, the rep is the u32 index (never T), so the proof is Copy regardless of T; consuming ops live in BitIndexOps (signed and unsigned). No constant-time constructor — shift amounts are public.

use const_num_traits::{BitIndex, BitIndexOps};

let i = BitIndex::<u8>::new(3).unwrap();
assert_eq!(1u8.shl_index(i), 8);
assert_eq!(0x80u8.shr_index(i), 0x10);
assert!(BitIndex::<u8>::new(8).is_none()); // == BITS, rejected

Implementations§

Source§

impl<T> BitIndex<T>

Source

pub const unsafe fn from_u32_unchecked(index: u32) -> Self

Constructs the proof directly from an index, without checking.

§Safety

index must be < T::BITS; otherwise the consuming shifts are undefined / panic.

Source

pub const fn get(self) -> u32

The proven index. Free (no recompute).

Source§

impl<T> BitIndex<T>

Source

pub fn new_checked(index: u32) -> Option<Self>
where T: PrimBits,

Safe constructor for any PrimBits carrier, not just the primitives: Some iff index < T::BITS. The per-primitive new stays the const fast path.

Source§

impl BitIndex<u8>

Source

pub const fn new(index: u32) -> Option<Self>

Checked constructor: Some iff index < <$t>::BITS.

Source§

impl BitIndex<u16>

Source

pub const fn new(index: u32) -> Option<Self>

Checked constructor: Some iff index < <$t>::BITS.

Source§

impl BitIndex<u32>

Source

pub const fn new(index: u32) -> Option<Self>

Checked constructor: Some iff index < <$t>::BITS.

Source§

impl BitIndex<u64>

Source

pub const fn new(index: u32) -> Option<Self>

Checked constructor: Some iff index < <$t>::BITS.

Examples found in repository?
examples/typestates.rs (line 43)
42fn bit_index() {
43    let pos = BitIndex::<u64>::new(40).expect("40 < 64");
44    assert_eq!((1u64).shl_index(pos), 1u64 << 40);
45    assert_eq!((1u64 << 63).shr_index(pos), (1u64 << 63) >> 40);
46
47    // out-of-range index is rejected at construction, not at the shift
48    assert!(BitIndex::<u64>::new(64).is_none());
49}
Source§

impl BitIndex<u128>

Source

pub const fn new(index: u32) -> Option<Self>

Checked constructor: Some iff index < <$t>::BITS.

Source§

impl BitIndex<usize>

Source

pub const fn new(index: u32) -> Option<Self>

Checked constructor: Some iff index < <$t>::BITS.

Source§

impl BitIndex<i8>

Source

pub const fn new(index: u32) -> Option<Self>

Checked constructor: Some iff index < <$t>::BITS.

Source§

impl BitIndex<i16>

Source

pub const fn new(index: u32) -> Option<Self>

Checked constructor: Some iff index < <$t>::BITS.

Source§

impl BitIndex<i32>

Source

pub const fn new(index: u32) -> Option<Self>

Checked constructor: Some iff index < <$t>::BITS.

Source§

impl BitIndex<i64>

Source

pub const fn new(index: u32) -> Option<Self>

Checked constructor: Some iff index < <$t>::BITS.

Source§

impl BitIndex<i128>

Source

pub const fn new(index: u32) -> Option<Self>

Checked constructor: Some iff index < <$t>::BITS.

Source§

impl BitIndex<isize>

Source

pub const fn new(index: u32) -> Option<Self>

Checked constructor: Some iff index < <$t>::BITS.

Trait Implementations§

Source§

impl<T> Clone for BitIndex<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Copy for BitIndex<T>

Source§

impl TryFrom<u32> for BitIndex<u8>

Checked construction from an index (< BITS); mirrors BitIndex::new. The source is the u32 index, not the carrier $t.

Source§

type Error = TypestateError

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

fn try_from(index: u32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u32> for BitIndex<u16>

Checked construction from an index (< BITS); mirrors BitIndex::new. The source is the u32 index, not the carrier $t.

Source§

type Error = TypestateError

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

fn try_from(index: u32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u32> for BitIndex<u32>

Checked construction from an index (< BITS); mirrors BitIndex::new. The source is the u32 index, not the carrier $t.

Source§

type Error = TypestateError

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

fn try_from(index: u32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u32> for BitIndex<u64>

Checked construction from an index (< BITS); mirrors BitIndex::new. The source is the u32 index, not the carrier $t.

Source§

type Error = TypestateError

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

fn try_from(index: u32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u32> for BitIndex<u128>

Checked construction from an index (< BITS); mirrors BitIndex::new. The source is the u32 index, not the carrier $t.

Source§

type Error = TypestateError

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

fn try_from(index: u32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u32> for BitIndex<usize>

Checked construction from an index (< BITS); mirrors BitIndex::new. The source is the u32 index, not the carrier $t.

Source§

type Error = TypestateError

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

fn try_from(index: u32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u32> for BitIndex<i8>

Checked construction from an index (< BITS); mirrors BitIndex::new. The source is the u32 index, not the carrier $t.

Source§

type Error = TypestateError

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

fn try_from(index: u32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u32> for BitIndex<i16>

Checked construction from an index (< BITS); mirrors BitIndex::new. The source is the u32 index, not the carrier $t.

Source§

type Error = TypestateError

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

fn try_from(index: u32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u32> for BitIndex<i32>

Checked construction from an index (< BITS); mirrors BitIndex::new. The source is the u32 index, not the carrier $t.

Source§

type Error = TypestateError

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

fn try_from(index: u32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u32> for BitIndex<i64>

Checked construction from an index (< BITS); mirrors BitIndex::new. The source is the u32 index, not the carrier $t.

Source§

type Error = TypestateError

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

fn try_from(index: u32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u32> for BitIndex<i128>

Checked construction from an index (< BITS); mirrors BitIndex::new. The source is the u32 index, not the carrier $t.

Source§

type Error = TypestateError

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

fn try_from(index: u32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u32> for BitIndex<isize>

Checked construction from an index (< BITS); mirrors BitIndex::new. The source is the u32 index, not the carrier $t.

Source§

type Error = TypestateError

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

fn try_from(index: u32) -> Result<Self, TypestateError>

Performs the conversion.

Auto Trait Implementations§

§

impl<T> Freeze for BitIndex<T>

§

impl<T> RefUnwindSafe for BitIndex<T>
where T: RefUnwindSafe,

§

impl<T> Send for BitIndex<T>
where T: Send,

§

impl<T> Sync for BitIndex<T>
where T: Sync,

§

impl<T> Unpin for BitIndex<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for BitIndex<T>

§

impl<T> UnwindSafe for BitIndex<T>
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, 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.