6 releases

0.3.1 Apr 12, 2023
0.3.0 Apr 9, 2023
0.2.3 Apr 7, 2023
0.1.0 Apr 6, 2023

#17 in #sized

MIT license

21KB
337 lines


Matrix Test crate-name at crates.io crate-name at docs.rs

This library provides SizedBiset<B>, which is an idea borrowed from C++'s std::bitset<N> and some functionalities for it.


lib.rs:

sized_bitset

This library provides SizedBitset (statically-sized bitset) and functionality for its.

SizedBitset 101

  • constant initialization
use sized_bitset::SizedBitset;
const BITSET: SizedBitset<4> = SizedBitset::from_const([true, true, false, false]);
  • from primitives

From<u{N}> SizedBitset<{M}> is defined if and only if N <= M.

use sized_bitset::SizedBitset;
let bitset: SizedBitset<8> = 0b10101010.into();
  • try from slice

This panics if slice length is not exactly same as SizedBitset size.


use sized_bitset::SizedBitset;
let bitset: SizedBitset<4> = [true, true, true, true].as_slice().try_into().unwrap();
  • parse str

This panics if str length is greater than SizedBitset size.


use sized_bitset::SizedBitset;
let bitset: SizedBitset<4> = "1010".parse().unwrap();

To Primitives

Allows sized_bitset::convert::*; us to convert [SizedBitset] to primitives.

To{N} for SizedBitset<{M}> is defined if and only if N >= M.

pub mod convert {
    pub trait To8 {
        fn to_u8(&self) -> u8;
    }
    pub trait To16 {
        fn to_u16(&self) -> u16;
    }
    pub trait To32 {
        fn to_u32(&self) -> u32;
    }
    pub trait To64 {
        fn to_u64(&self) -> u64;
    }
    pub trait To128 {
        fn to_u128(&self) -> u128;
    }
}

Dependencies

~1.1–2.2MB
~41K SLoC