safebit 
safebit provides a simple interface to bit slices of integers.
It is designed to fulfill the following goals:
- No dependencies: Every dependency is a security and reliability risk.
safebitis simple enough to not need any dependencies. - No unsafe code: Safety and security are more important than performance.
- No panics: While panics usually have no security impact, they can have a significant impact on safety.
safebitmust not panic during any operation on any ofsafebit's types. - no std and alloc: All basic operations work without std and alloc. Glue code for common std types is gated by the
stdfeature.
Examples
Bitstream
extern crate std;
use ;
// Blanket implementation for Read is provided if the std feature is enabled
let mut f = open.unwrap;
let mut stream: = new;
// Read the first few nibbles of "[package]"
let n0: u16 = stream.read.unwrap;
assert_eq!;
let n1: u16 = stream.read.unwrap;
assert_eq!;
let n2: u16 = stream.read.unwrap;
assert_eq!;
let n3: u16 = stream.read.unwrap;
assert_eq!;
// The bitstream constructor accepts references to everything that implements read, such as
// BufReader. If you need a custom reader, you can implement your own.
let mut buf_reader = new;
let mut stream2: = new;
let n4: i16 = stream2.read.unwrap;
assert_eq!;
let n5: i16 = stream2.read.unwrap;
assert_eq!;
let zero: u16 = stream2.read.unwrap;
assert_eq!;
let signed: i16 = stream2.read.unwrap; // 0b110
assert_eq!;
Cursor
use CursorError;
use Cursor;
use WordRead;
let data: = ;
let mut cursor = new;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Words
use Word;
use WordError;
let t0: u16 = 0xff00;
let t1: u8 = 0b0000_0010;
// Extract a slice from an u16 into an u8
assert_eq!;
// Extract a slice into an u8 which does not fit
assert_eq!;
// Extract a slice with the most significant bit set into a signed type
assert_eq!;