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

pub trait BitStore: Sealed + Binary + BitAnd<Self, Output = Self> + BitAndAssign<Self> + BitOr<Self, Output = 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 + BitOps {
    type Access: BitAccess<Self>;

    const BITS: u8;
    const INDX: u8;
    const MASK: u8;
    const FALSE: Self;
    const TRUE: Self;
    const TYPENAME: &'static str;

    fn as_bytes(&self) -> &[u8];
fn from_bytes(bytes: &[u8]) -> Self; fn get<C>(&self, place: BitIdx<Self>) -> bool
    where
        C: Cursor
, { ... }
fn set<C>(&mut self, place: BitIdx<Self>, value: bool)
    where
        C: Cursor
, { ... }
fn count_ones(self) -> usize { ... }
fn count_zeros(self) -> usize { ... } }

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 Types

type Access: BitAccess<Self>

Shared/mutable access wrapper.

Within &BitSlice and &mut BitSlice contexts, the Access type governs all access to underlying memory that may be contended by multiple slices. When a codepath knows that it has full, uncontended ownership of a memory element of Self, and no other codepath may observe or modify it, then that codepath may skip the Access type and use plain accessors.

Loading content...

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 FALSE: Self

The value with all bits unset.

const TRUE: Self

The value with all bits set.

const TYPENAME: &'static str

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

Loading content...

Required methods

fn as_bytes(&self) -> &[u8]

Interprets a value as a sequence of bytes.

Parameters

  • &self

Returns

A slice covering *self as a sequence of individual bytes.

fn from_bytes(bytes: &[u8]) -> Self

Interprets a sequence of bytes as Self.

Parameters

  • bytes: The bytes to interpret as Self. This must be exactly mem::size_of::<Self> bytes long.

Returns

An instance of Self constructed by reinterpreting bytes.

Panics

This panics if bytes.len() is not mem::size_of::<Self>().

Loading content...

Provided methods

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

Gets a specific bit in an element.

Safety

This method cannot be called from within an &BitSlice context; it may only be called by construction of an &Self reference from a Self element directly.

Parameters

  • &self
  • place: A bit index in the element. The bit under this index, as governed by the C Cursor, will be retrieved as a bool.

Returns

The value of the bit under place.

Type Parameters

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

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

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

Safety

This method cannot be called from within an &mut BitSlice context; it may only be called by construction of an &mut Self reference from a Self element directly.

Parameters

  • place: A bit index in the element. The bit under this index, as governed by the C Cursor, will be set according to value.

Type Parameters

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

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);
Loading content...

Implementations on Foreign Types

impl BitStore for u8[src]

type Access = AtomicU8

impl BitStore for u16[src]

type Access = AtomicU16

impl BitStore for u32[src]

type Access = AtomicU32

impl BitStore for u64[src]

type Access = AtomicU64

Loading content...

Implementors

Loading content...