Crates.io | nibbler |
lib.rs | nibbler |
version | 0.2.3 |
source | src |
created_at | 2020-08-14 21:13:51.676077 |
updated_at | 2020-08-16 19:55:40.31034 |
description | A small utility for working with nibbles (half byte, 4-bits). |
homepage | https://gitlab.com/RustyChalk/nibbler |
repository | https://gitlab.com/RustyChalk/nibbler |
max_upload_size | |
id | 276829 |
size | 112,614 |
A small library to handle nibbles.
There are a few other libraries on Crates.io which handle nibbles.
Nothing in this library should be considered "efficient," nor "fast". It is however meant to be simple.
Only thing needed is the std::fmt::Display
(standard library) for displaying objects.
use nibbler::traits::Nib;
use nibbler::nibble::Nibble;
let val: u8 = 10;
let mut nib: Nibble = Nibble::from(val);
assert_eq!(10, nib.into());
use nibbler::nibbles::Nibbles;
let val: u16 = 0xaa; // 10101010
let nibs: Nibbles = val.into();
assert_eq!(2, nibs.len());
assert!(nibs.fits_u8());
assert_eq!(170, nibs.into());
assert_eq!(10, nibs.get(0).into());
assert_eq!(10, nibs.get(1).into());
let val: u16 = 0xba; // 10111010
let nibs: Nibbles = val.into();
assert_eq!(2, nibs.len());
assert!(nibs.fits_u8());
assert_eq!(186, nibs.into());
assert_eq!(11, nibs.get(0).into());
assert_eq!(10, nibs.get(1).into());
let val: u16 = 0xf00;
let nibs: Nibbles = val.into();
assert_eq!(3, nibs.len());
assert!(!nibs.fits_u8());
assert!(nibs.fits_u16());
assert_eq!(3840, nibs.into());
assert_eq!(15, nibs.get(0).into());
assert_eq!(0, nibs.get(1).into());
assert_eq!(0, nibs.get(2).into());
Nibbles don't care about signing. Nibbles are just 4 bits. Signed integers are treated as unsigned integers in all conversions. You can cast back .into()
a signed integer, as available.
For more information, see the documentation... or on Crates.io ... or the repo.
On the purported "todo" list is to add right and left shift, addition, subtraction, multiplication.
GPLv3