[][src]Trait bitvec::store::BitStore

pub trait BitStore: Sealed + Binary + BitAnd<Self, Output = Self> + BitAndAssign<Self> + BitOrAssign<Self> + Copy + Debug + Display + Eq + From<u8> + Into<u64> + LowerHex + Not<Output = Self> + Send + Shl<u8, Output = Self> + ShlAssign<u8> + Shr<u8, Output = Self> + ShrAssign<u8> + Sized + Sync + UpperHex {
    const BITS: u8;
    const INDX: u8;
    const MASK: u8;
    const TYPENAME: &'static str;
    fn load(&self) -> Self { ... }
fn set<C>(&mut self, place: BitIdx, value: bool)
    where
        C: Cursor
, { ... }
fn set_at(&mut self, place: BitPos, value: bool) { ... }
fn get<C>(&self, place: BitIdx) -> bool
    where
        C: Cursor
, { ... }
fn get_at(&self, place: BitPos) -> bool { ... }
fn mask_at(place: BitPos) -> Self { ... }
fn count_ones(&self) -> usize { ... }
fn count_zeros(&self) -> usize { ... }
fn bits(bit: bool) -> Self { ... } }

Generalizes over the fundamental types for use in bitvec data structures.

This trait must only be implemented on unsigned integer primitives with full alignment. It cannot be implemented on u128 on any architecture, or on u64 on 32-bit systems.

The Sealed supertrait ensures that this can only be implemented locally, and will never be implemented by downstream crates on new types.

Associated Constants

const BITS: u8

The width, in bits, of this type.

const INDX: u8

The number of bits required to index a bit inside the type. This is always log2 of the type’s bit width.

const MASK: u8

The bitmask to turn an arbitrary number into a bit index. Bit indices are always stored in the lowest bits of an index value.

const TYPENAME: &'static str

Name of the implementing type. This is only necessary until the compiler stabilizes type_name().

Loading content...

Provided methods

fn load(&self) -> Self

Performs a synchronized load on the underlying element.

Parameters

  • &self

Returns

The element referred to by the self reference, loaded synchronously after any in-progress accesses have concluded.

fn set<C>(&mut self, place: BitIdx, value: bool) where
    C: Cursor

Sets a specific bit in an element to a given value.

Parameters

  • place: A bit index in the element, from 0 to Self::MASK. The bit under this index will be set according to value.
  • value: A Boolean value, which sets the bit on true and unsets it on false.

Type Parameters

  • C: Cursor: A Cursor implementation to translate the index into a position.

Panics

This function panics if place is not less than T::BITS, in order to avoid index out of range errors.

Examples

This example sets and unsets bits in a byte.

use bitvec::prelude::{
  BitStore,
  BigEndian,
  LittleEndian,
};

let mut elt: u16 = 0;

elt.set::<BigEndian>(1.into(), true);
assert_eq!(elt, 0b0100_0000__0000_0000);
elt.set::<LittleEndian>(1.into(), true);
assert_eq!(elt, 0b0100_0000__0000_0010);

elt.set::<BigEndian>(1.into(), false);
assert_eq!(elt, 0b0000_0000__0000_0010);
elt.set::<LittleEndian>(1.into(), false);
assert_eq!(elt, 0);

This example overruns the index, and panics.

use bitvec::prelude::{BitStore, BigEndian};
let mut elt: u8 = 0;
elt.set::<BigEndian>(8.into(), true);

fn set_at(&mut self, place: BitPos, value: bool)

Sets a specific bit in an element to a given value.

Parameters

  • place: A bit position in the element, where 0 is the LSbit and Self::MASK is the MSbit.
  • value: A Boolean value, which sets the bit high on true and unsets it low on false.

Panics

This function panics if place is not less than T::BITS, in order to avoid index out of range errors.

Examples

This example sets and unsets bits in a byte.

use bitvec::prelude::BitStore;
let mut elt: u8 = 0;
elt.set_at(0.into(), true);
assert_eq!(elt, 0b0000_0001);
elt.set_at(7.into(), true);
assert_eq!(elt, 0b1000_0001);

This example overshoots the width, and panics.

use bitvec::prelude::BitStore;
let mut elt: u8 = 0;
elt.set_at(8.into(), true);

fn get<C>(&self, place: BitIdx) -> bool where
    C: Cursor

Gets a specific bit in an element.

Parameters

  • place: A bit index in the element, from 0 to Self::MASK. The bit under this index will be retrieved as a bool.

Returns

The value of the bit under place, as a bool.

Type Parameters

  • C: Cursor: A Cursor implementation to translate the index into a position.

Panics

This function panics if place is not less than T::BITS, in order to avoid index out of range errors.

Examples

This example gets two bits from a byte.

use bitvec::prelude::{BitStore, BigEndian};
let elt: u8 = 0b0010_0000;
assert!(!elt.get::<BigEndian>(1.into()));
assert!(elt.get::<BigEndian>(2.into()));
assert!(!elt.get::<BigEndian>(3.into()));

This example overruns the index, and panics.

use bitvec::prelude::{BitStore, BigEndian};
0u8.get::<BigEndian>(8.into());

fn get_at(&self, place: BitPos) -> bool

Gets a specific bit in an element.

Parameters

  • place: A bit position in the element, from 0 at LSbit to Self::MASK at MSbit. The bit under this position will be retrieved as a bool.

Returns

The value of the bit under place, as a bool.

Panics

This function panics if place is not less than T::BITS, in order to avoid index out of range errors.

Examples

This example gets two bits from a byte.

use bitvec::prelude::BitStore;
let elt: u8 = 0b0010_0000;
assert!(!elt.get_at(4.into()));
assert!(elt.get_at(5.into()));
assert!(!elt.get_at(6.into()));

This example overruns the index, and panics.

use bitvec::prelude::BitStore;
0u8.get_at(8.into());

fn mask_at(place: BitPos) -> Self

Produces the bit mask which selects only the bit at the requested position.

This mask must be inverted in order to clear the bit.

Parameters

  • place: The bit position for which to create a bitmask.

Returns

The one-hot encoding of the bit position index.

Panics

This function panics if place is not less than T::BITS, in order to avoid index out of range errors.

Examples

This example produces the one-hot encodings for indices.

use bitvec::prelude::BitStore;

assert_eq!(u8::mask_at(0.into()), 0b0000_0001);
assert_eq!(u8::mask_at(1.into()), 0b0000_0010);
assert_eq!(u8::mask_at(2.into()), 0b0000_0100);
assert_eq!(u8::mask_at(3.into()), 0b0000_1000);
assert_eq!(u8::mask_at(4.into()), 0b0001_0000);
assert_eq!(u8::mask_at(5.into()), 0b0010_0000);
assert_eq!(u8::mask_at(6.into()), 0b0100_0000);
assert_eq!(u8::mask_at(7.into()), 0b1000_0000);

assert_eq!(u16::mask_at(8.into()),  0b0000_0001__0000_0000);
assert_eq!(u16::mask_at(9.into()),  0b0000_0010__0000_0000);
assert_eq!(u16::mask_at(10.into()), 0b0000_0100__0000_0000);
assert_eq!(u16::mask_at(11.into()), 0b0000_1000__0000_0000);
assert_eq!(u16::mask_at(12.into()), 0b0001_0000__0000_0000);
assert_eq!(u16::mask_at(13.into()), 0b0010_0000__0000_0000);
assert_eq!(u16::mask_at(14.into()), 0b0100_0000__0000_0000);
assert_eq!(u16::mask_at(15.into()), 0b1000_0000__0000_0000);

assert_eq!(u32::mask_at(16.into()), 1 << 16);
assert_eq!(u32::mask_at(24.into()), 1 << 24);
assert_eq!(u32::mask_at(31.into()), 1 << 31);

assert_eq!(u64::mask_at(32.into()), 1 << 32);
assert_eq!(u64::mask_at(48.into()), 1 << 48);
assert_eq!(u64::mask_at(63.into()), 1 << 63);

These examples ensure that indices panic when out of bounds.

use bitvec::prelude::BitStore;
u8::mask_at(8.into());
use bitvec::prelude::BitStore;
u16::mask_at(16.into());
use bitvec::prelude::BitStore;
u32::mask_at(32.into());
use bitvec::prelude::BitStore;
u64::mask_at(64.into());

fn count_ones(&self) -> usize

Counts how many bits in self are set to 1.

This zero-extends self to u64, and uses the u64::count_ones inherent method.

Parameters

  • &self

Returns

The number of bits in self set to 1. This is a usize instead of a u32 in order to ease arithmetic throughout the crate.

Examples

use bitvec::prelude::BitStore;
assert_eq!(BitStore::count_ones(&0u8), 0);
assert_eq!(BitStore::count_ones(&128u8), 1);
assert_eq!(BitStore::count_ones(&192u8), 2);
assert_eq!(BitStore::count_ones(&224u8), 3);
assert_eq!(BitStore::count_ones(&240u8), 4);
assert_eq!(BitStore::count_ones(&248u8), 5);
assert_eq!(BitStore::count_ones(&252u8), 6);
assert_eq!(BitStore::count_ones(&254u8), 7);
assert_eq!(BitStore::count_ones(&255u8), 8);

fn count_zeros(&self) -> usize

Counts how many bits in self are set to 0.

This inverts self, so all 0 bits are 1 and all 1 bits are 0, then zero-extends self to u64 and uses the u64::count_ones inherent method.

Parameters

  • &self

Returns

The number of bits in self set to 0. This is a usize instead of a u32 in order to ease arithmetic throughout the crate.

Examples

use bitvec::prelude::BitStore;
assert_eq!(BitStore::count_zeros(&0u8), 8);
assert_eq!(BitStore::count_zeros(&1u8), 7);
assert_eq!(BitStore::count_zeros(&3u8), 6);
assert_eq!(BitStore::count_zeros(&7u8), 5);
assert_eq!(BitStore::count_zeros(&15u8), 4);
assert_eq!(BitStore::count_zeros(&31u8), 3);
assert_eq!(BitStore::count_zeros(&63u8), 2);
assert_eq!(BitStore::count_zeros(&127u8), 1);
assert_eq!(BitStore::count_zeros(&255u8), 0);

fn bits(bit: bool) -> Self

Extends a single bit to fill the entire element.

Parameters

  • bit: The bit to extend.

Returns

An element with all bits set to the input.

Loading content...

Implementations on Foreign Types

impl BitStore for u8[src]

type Atom = AtomicU8

impl BitStore for u16[src]

type Atom = AtomicU16

impl BitStore for u32[src]

type Atom = AtomicU32

impl BitStore for u64[src]

type Atom = AtomicU64

Loading content...

Implementors

Loading content...