[][src]Struct nibbler::nibbles::Nibbles

pub struct Nibbles(_, _);

Holds a vector (array) of Nibble values. Second value is boolean; whether or not the object is editable (in addition to mutability concerns).

If instantiated with an integer: Cannot be edited. If instantiated with one of the ::new_ methods, and declared mutable, then the objects vector is editable.

Implementations

impl Nibbles[src]

pub fn new() -> Nibbles[src]

Creates a new instance of nibbles. Can be modified.

Example Usage:

use nibbler::nibbles::Nibbles;
let t1 = Nibbles::new();
assert_eq!(t1.len(), 1);
assert_eq!(0, t1.get(0).into());

pub fn new_from_nib_vec(nv: Vec<Nibble>) -> Nibbles[src]

Creates a new instance from a vector of nibbles. Can be modified.

Example Usage:

use nibbler::nibbles::Nibbles;
let t1 = Nibbles::from(5u8);
let t2 = Nibbles::new_from_nib_vec(t1.raw_vec());
assert!(t1 != t2);
assert_eq!(t1.raw_vec(), t2.raw_vec());  // vector is self.0 -> Vec<Nibble>
assert!(t1.editable() != t2.editable()); // editable is self.1 -> bool
// t1.push(0.into()); // fails, panics, not editable.
assert!(t2.editable());
// t2.push(0.into()); // fails, t2 is not mutable.
let mut t3 = Nibbles::new_from_nib_vec(t2.raw_vec());
assert_eq!(5, t1.clone().into());
assert_eq!(5, t2.clone().into());
assert_eq!(5, t3.clone().into());
assert_eq!(t2, t3); // vector and editable are the same, unlike t1; however t3 is mutable.
t3.push(0.into());
assert!(t2 != t3);
assert_eq!(80, t3.clone().into());

pub fn new_from_self(&self) -> Nibbles[src]

Creates a new instance from an existing Nibbles instance. Can be modified.

Example Usage:

use nibbler::nibbles::Nibbles;
let t1 = Nibbles::from(5u8);
let t2 = Nibbles::new_from_self(&t1);
assert!(t1 != t2);
assert_eq!(t1.raw_vec(), t2.raw_vec());  // vector is self.0 -> Vec<Nibble>
assert!(t1.editable() != t2.editable()); // editable is self.1 -> bool
// t1.push(0.into()); // fails, panics, not editable.
assert!(t2.editable());
// t2.push(0.into()); // fails, t2 is not mutable.
let mut t3 = Nibbles::new_from_self(&t2);
assert_eq!(5, t1.clone().into());
assert_eq!(5, t2.clone().into());
assert_eq!(5, t3.clone().into());
assert_eq!(t2, t3); // vector and editable are the same, unlike t1; however t3 is mutable.
t3.push(0.into());
assert!(t2 != t3);
assert_eq!(80, t3.clone().into());

pub fn with_capacity(size: usize) -> Nibbles[src]

Creates a new instance of Nibbles with a specified capacity. Can be modified.

Example Usage:

use nibbler::nibbles::Nibbles;
let t1 = Nibbles::with_capacity(5);
assert_eq!(t1.len(), 0);
assert_eq!(t1.capacity(), 5);
// but you can't do anything with this, t1 is not mutable. It is a dead object.
let mut t2 = Nibbles::with_capacity(5);
assert_eq!(t2.len(), 0);
assert_eq!(t2.capacity(), 5);
t2.push(5.into());
assert_eq!(t2.len(), 1);
assert_eq!(t2.capacity(), 5);
let five = t2.pop();
assert_eq!(t2.len(), 0);
assert_eq!(t2.capacity(), 5);
assert_eq!(5, five.into());
t2.push(0.into());
t2.push(1.into());
t2.push(2.into());
t2.push(3.into());
t2.push(4.into());
assert_eq!(t2.len(), 5);
assert_eq!(t2.capacity(), 5);
t2.push(5.into());
assert!(t2.capacity() >= 6);
// capacity increased to accomodate a 6th item; additional allocation determinted by rust

pub fn new_from_hex_str(s: &str) -> Nibbles[src]

Creates a new instance of Nibbles from a hex-dec string. Can be modified.

Example Usage:

use nibbler::nibbles::Nibbles;
let shreck: Nibbles = Nibbles::new_from_hex_str("0xff");
assert_eq!(2, shreck.len());
assert_eq!(15, shreck.get(0).into());
assert_eq!(15, shreck.get(1).into());
assert_eq!(255, shreck.clone().into());
let shreck: Nibbles = Nibbles::new_from_hex_str("0xABCDEF0123456789fedcab");
assert_eq!(22, shreck.len());
assert_eq!(10, shreck.get(0).into());
assert_eq!(11, shreck.get(1).into());
assert_eq!(12, shreck.get(2).into());
assert_eq!(13, shreck.get(3).into());
assert_eq!(14, shreck.get(4).into());
assert_eq!(15, shreck.get(5).into());
assert_eq!(0, shreck.get(6).into());
assert_eq!(1, shreck.get(7).into());
assert_eq!(2, shreck.get(8).into());
assert_eq!(3, shreck.get(9).into());
assert_eq!(4, shreck.get(10).into());
assert_eq!(5, shreck.get(11).into());
assert_eq!(6, shreck.get(12).into());
assert_eq!(7, shreck.get(13).into());
assert_eq!(8, shreck.get(14).into());
assert_eq!(9, shreck.get(15).into());
assert_eq!(15, shreck.get(16).into());
assert_eq!(14, shreck.get(17).into());
assert_eq!(13, shreck.get(18).into());
assert_eq!(12, shreck.get(19).into());
assert_eq!(10, shreck.get(20).into());
assert_eq!(11, shreck.get(21).into());
// let shreck: Nibbles = Nibbles::new_from_hex_str("0xabcdexxx"); // panic
// notice the xxx at the end of the string: That's not a hex-dec character.

pub fn set_static(&mut self)[src]

Makes the object not editable (even if mutable).

Example Usage:

use nibbler::nibbles::Nibbles;
let mut t1 = Nibbles::with_capacity(5);
t1.push(0.into());
t1.set_static();
//t1.push(1.into()); // this will fail; the editable flag has been set to false.
//t1.pop(); // this will fail; the editable flag has been set to false.

pub fn len(&self) -> usize[src]

Get count of nibbles.

Example Usage:

use nibbler::nibbles::Nibbles;
let t1 = Nibbles::new();
assert_eq!(t1.len(), 1);
assert_eq!(0, t1.get(0).into());

pub fn capacity(&self) -> usize[src]

Get the capacity of the internal vector.

Example Usage:

use nibbler::nibbles::Nibbles;
let t1 = Nibbles::with_capacity(5);
assert_eq!(t1.len(), 0);
assert_eq!(t1.capacity(), 5);
// but you can't do anything with this, t1 is not mutable. It is a dead object.
let mut t2 = Nibbles::with_capacity(5);
assert_eq!(t2.len(), 0);
assert_eq!(t2.capacity(), 5);
t2.push(5.into());
assert_eq!(t2.len(), 1);
assert_eq!(t2.capacity(), 5);
let five = t2.pop();
assert_eq!(t2.len(), 0);
assert_eq!(t2.capacity(), 5);
assert_eq!(5, five.into());
t2.push(0.into());
t2.push(1.into());
t2.push(2.into());
t2.push(3.into());
t2.push(4.into());
assert_eq!(t2.len(), 5);
assert_eq!(t2.capacity(), 5);
t2.push(5.into());
assert!(t2.capacity() >= 6);
// capacity increased to accomodate a 6th item; additional allocation determinted by rust

pub fn fits_u8(&self) -> bool[src]

Checks if the nibbles will fit into a u8 (vector <= 2 values)

Example Usage:

use nibbler::nibbles::Nibbles;
let mut tst = Nibbles::new();
assert!(tst.fits_u8());
assert_eq!(tst.len(), 1);
let pop = tst.pop();
assert_eq!(tst.len(), 0);
assert!(tst.fits_u8());
tst.push(pop);
tst.push(1.into());
assert_eq!(tst.len(), 2);
assert!(tst.fits_u8());
tst.push(2.into());
assert_eq!(tst.len(), 3);
assert!(!tst.fits_u8());
assert!(tst.fits_u16());

pub fn fits_u16(&self) -> bool[src]

Checks if the nibbles will fit into a u16 (vector <= 4 values)

Example Usage:

use nibbler::nibbles::Nibbles;
let t1: Nibbles = 256u16.into();
let mut t2 = Nibbles::new_from_self(&t1);
assert!(!t2.fits_u8());
assert!(t2.fits_u16());
assert_eq!(t2.len(), 4);
t2.shrink();
assert_eq!(t2.len(), 3);
let pop = t2.pop();
assert_eq!(t2.len(), 2);
assert!(t2.fits_u8());
t2.push(pop);
t2.push(4.into());
assert_eq!(t2.len(), 4);
assert!(!t2.fits_u8());
assert!(t2.fits_u16());
t2.push(5.into());
assert_eq!(t2.len(), 5);
assert!(!t2.fits_u8());
assert!(!t2.fits_u16());
assert!(t2.fits_u32());

pub fn fits_u32(&self) -> bool[src]

Checks if the nibbles will fit into a u32 (vector <= 8 values)

Example Usage:

use nibbler::nibbles::Nibbles;
let t1: Nibbles = 65536u32.into();
let mut t2 = Nibbles::new_from_self(&t1);
assert!(!t2.fits_u8());
assert!(!t2.fits_u16());
assert!(t2.fits_u32());
assert_eq!(t2.len(), 8);
t2.shrink();
assert_eq!(t2.len(), 5);
let pop = t2.pop();
assert_eq!(t2.len(), 4);
assert!(!t2.fits_u8());
assert!(t2.fits_u16());
assert!(t2.fits_u32());
t2.push(pop);
assert!(!t2.fits_u16());
assert!(t2.fits_u32());
t2.push(6.into());
assert_eq!(t2.len(), 6);
assert!(t2.fits_u32());
t2.push(7.into());
t2.push(8.into());
t2.push(9.into());
assert_eq!(t2.len(), 9);
assert!(!t2.fits_u8());
assert!(!t2.fits_u16());
assert!(!t2.fits_u32());
assert!(t2.fits_u64());

pub fn fits_u64(&self) -> bool[src]

Checks if the nibbles will fit into a u64 (vector <= 16 values)

Example Usage:

use nibbler::nibbles::Nibbles;
let t1: Nibbles = 4294967296u64.into();
let mut t2 = Nibbles::new_from_self(&t1);
assert!(!t2.fits_u8());
assert!(!t2.fits_u16());
assert!(!t2.fits_u32());
assert!(t2.fits_u64());
assert_eq!(t2.len(), 16);
t2.shrink();
assert_eq!(t2.len(), 9);
let pop = t2.pop();
assert_eq!(t2.len(), 8);
assert!(!t2.fits_u8());
assert!(!t2.fits_u16());
assert!(t2.fits_u32());
assert!(t2.fits_u64());
t2.push(pop);
assert!(!t2.fits_u32());
assert!(t2.fits_u64());
t2.push(10.into());
assert_eq!(t2.len(), 10);
assert!(t2.fits_u64());
t2.push(11.into()); // 10+1 == 11
t2.push(12.into()); // ...2
t2.push(13.into()); // ...3
t2.push(14.into()); // ...4
t2.push(15.into()); // ...5
// t2.push(16.into()); // this would fail; 0 <= Nibble <= 15
t2.push(0.into());  // ...6
t2.push(1.into());  // ...7 == 17
assert_eq!(t2.len(), 17);
assert!(!t2.fits_u8());
assert!(!t2.fits_u16());
assert!(!t2.fits_u32());
assert!(!t2.fits_u64());
assert!(t2.is_bignum());

pub fn is_bignum(&self) -> bool[src]

Checks to see if this is a bignum (nibbles count > 16)

Example Usage:

use nibbler::nibbles::Nibbles;
let val: u64 = 18364758544493064720;
let t1: Nibbles = val.into();
let mut t2: Nibbles = Nibbles::new_from_self(&t1);
t2.push(1.into());
assert!(t2.capacity() >= 17);
assert!(!t2.fits_u64());
assert!(t2.is_bignum());
let _tmp = t2.pop();
assert!(t2.capacity() >= 17);
assert!(t2.fits_u64());
assert!(!t2.is_bignum());

pub fn raw_vec(&self) -> Vec<Nibble>[src]

Returns a clone of the internal nibble vector.

Example Usage:

use nibbler::nibbles::Nibbles;
let val: u64 = 18364758544493064720;
let t1 = Nibbles::from(val);
let t2 = Nibbles::new_from_nib_vec(t1.raw_vec());
assert_eq!(t1.len(), 16);
assert_eq!(t2.len(), 16);
assert_eq!(t1.raw_vec(), t2.raw_vec());
assert!(t1.editable() != t2.editable() && t2.editable());

pub fn editable(&self) -> bool[src]

Returns whether or not the object is editable.

Example Usage:

use nibbler::nibbles::Nibbles;
let val: u64 = 18364758544493064720;
let mut t1 = Nibbles::from(val);
let t2 = Nibbles::new_from_nib_vec(t1.raw_vec());
assert_eq!(t1.len(), 16);
assert_eq!(t2.len(), 16);
assert_eq!(t1.raw_vec(), t2.raw_vec());
assert!(t1.editable() != t2.editable() && t2.editable());
let mut t3 = Nibbles::new_from_self(&t1);
assert_eq!(t2.raw_vec(), t3.raw_vec());
assert!(t1.editable() != t3.editable() && t2.editable() == t3.editable() && t3.editable());
// t1.set(0, 0.into()); // This will panic, because t1 is not editable.
// t2.set(0, 0.into()); // This will not work, because t2 is not mutable.
t3.set(0, 0.into());
assert_eq!(1070935975390360080u64, t3.into());
// bits 2^63, 2^62, 2^61 and 2^60 have been zero'ed out (0).

In the example above, t1 was instantiated from an integer. It is not editable. The new instance, t2, manually instantiated from the t1 vector, is editable. A simpler example below...

use nibbler::nibbles::Nibbles;
let tst = Nibbles::new();
assert_eq!(tst.len(), 1);
assert_eq!(0, tst.get(0).into());
assert!(tst.editable());

pub fn get(&self, idx: usize) -> Nibble[src]

Get the value at a specified index.

Example Usage:

use nibbler::nibbles::Nibbles;
let val: u64 = 18364758544493064720;
let t1 = Nibbles::from(val);
let get = t1.get(0);
assert_eq!(15, get.into());

pub fn set(&mut self, idx: usize, val: Nibble)[src]

Set the value at a specified index, if editable.

Example Usage:

use nibbler::nibbles::Nibbles;
let val: u64 = 18364758544493064720;
let t1 = Nibbles::from(val); // not mutable, not editable
let mut t2 = Nibbles::new_from_self(&t1);
t2.set(0, 0.into());
// MSB bits 2^63, 2^62, 2^61 and 2^60 have been zero'ed out (0).
assert_eq!(1070935975390360080u64, t2.into());

pub fn push(&mut self, val: Nibble)[src]

Push a Nibble onto the end of the vector. Expands internal vector capacity as necessary.

Example Usage:

use nibbler::nibbles::Nibbles;
let mut tst = Nibbles::new();
assert_eq!(tst.len(), 1);
tst.set(0, 3.into());
assert!(tst.fits_u8());
assert_eq!(3, tst.clone().into());
tst.push(4.into());
assert!(tst.fits_u8());
assert_eq!(52, tst.clone().into());
tst.push(5.into());
assert!(!tst.fits_u8());
assert!(tst.fits_u16());
assert_eq!(837, tst.clone().into());

pub fn pop(&mut self) -> Nibble[src]

Pop an item off the end of the vector, if populated. If there's nothing to pop, standard error is issued.

Example Usage:

use nibbler::nibbles::Nibbles;
let mut tst = Nibbles::new();
assert_eq!(tst.len(), 1);
let pop = tst.pop();
assert_eq!(tst.len(), 0);
assert_eq!(0, pop.into());

pub fn remove(&mut self, idx: usize)[src]

Remove an item.

Example Usage:

use nibbler::nibbles::Nibbles;
let val: u64 = 18364758544493064720;
let t1 = Nibbles::from(val); // not mutable, not editable
let mut t2 = Nibbles::new_from_self(&t1);
t2.remove(0);
// MSB bits 2^63, 2^62, 2^61 and 2^60 have been removed (wholesale).
assert_eq!(1070935975390360080u64, t2.into());

pub fn shrink(&mut self)[src]

Shrink all MSNib==0000

Example Usage:

use nibbler::nibbles::Nibbles;
let mut nib = Nibbles::new(); // 1
assert_eq!(nib.len(), 1);
nib.push(0.into()); // 2
nib.push(0.into()); // 3
nib.push(0.into()); // 4
for i in (0..16_u8).rev() { // this line looks really, really bad...
// but for the fact the above line excludes 16, 
// and 15 is inclusive of a u8 (unsigned; byte).
    nib.push(i.into());
}
assert!(nib.is_bignum());
assert!(!nib.fits_u64());
assert_eq!(nib.len(), 16+4); //20*4-bits-per-nibble == 80-bits.
nib.shrink(); // get rid of all those pesky MSB==0000
assert_eq!(18364758544493064720u64, nib.clone().into());
assert_eq!(nib.len(), 16);
assert!(nib.fits_u64());

The shrink function will not remove right most, or LSB nibbles, as this would be inaccurate and give smaller numeric values. Truncating LSB should be an explicit activity. Removing extraneous leading zeros on the left side of the array still gives the same numeric value.

Trait Implementations

impl Clone for Nibbles[src]

impl Debug for Nibbles[src]

impl Display for Nibbles[src]

impl From<i16> for Nibbles[src]

impl From<i32> for Nibbles[src]

impl From<i64> for Nibbles[src]

impl From<i8> for Nibbles[src]

impl From<u16> for Nibbles[src]

impl From<u32> for Nibbles[src]

impl From<u64> for Nibbles[src]

impl From<u8> for Nibbles[src]

impl Into<i16> for Nibbles[src]

impl Into<i32> for Nibbles[src]

impl Into<i64> for Nibbles[src]

impl Into<i8> for Nibbles[src]

impl Into<u16> for Nibbles[src]

impl Into<u32> for Nibbles[src]

impl Into<u64> for Nibbles[src]

impl Into<u8> for Nibbles[src]

impl PartialEq<Nibbles> for Nibbles[src]

impl StructuralPartialEq for Nibbles[src]

Auto Trait Implementations

impl RefUnwindSafe for Nibbles

impl Send for Nibbles

impl Sync for Nibbles

impl Unpin for Nibbles

impl UnwindSafe for Nibbles

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.