Struct lozizol::model::Vuint[][src]

pub struct Vuint<'a> { /* fields omitted */ }

Implementations

impl<'a> Vuint<'a>[src]

pub fn try_from_slice(value: &'a [u8]) -> Result<Self, VuintError>[src]

Create vuint from vuint bytes Vec. It validates the bytes to conform to standard

pub fn to_uint<I: UnsignedInt>(&self) -> Result<I, VuintError>[src]

pub fn bytes_needed<I: UnsignedInt>(number: &I) -> usize[src]

impl Vuint<'static>[src]

pub fn try_from_bytes(value: Vec<u8>) -> Result<Self, VuintError>[src]

Create vuint from vuint bytes Vec. It validates the bytes to conform to standard

impl<'a> Vuint<'a>[src]

pub fn decode<R, I>(reader: R) -> Result<I, VuintError> where
    R: Buf,
    I: UnsignedInt
[src]

Read a variable length encoded unsigned integer from r.

After a successful read, the read integer is returned.

Errors

The interger primitive used in the function and returned by the function is defined by the caller. If the integer primitive overflows while reading the variable length integer, a ReadError::Overflow is returned.

Examples

use lozizol::model::Vuint;

// 10, 20, 30
let data = [0x0A, 0x14, 0x1E];
let mut readable = &data[..];

assert_eq!(10u16, Vuint::decode(&mut readable)?);
assert_eq!(20u8, Vuint::decode(&mut readable)?);
assert_eq!(30u32, Vuint::decode(&mut readable)?);

The reader can either be passed (1) as value or (2) as mutable reference. See C-RW-VALUE. With case (1), the function returns the first variable length integer from the data on each call. With the mutable reader reference from case (2), successive calls return the next value each time. Case (2) is the standard reader use-case.

use lozizol::model::Vuint;

let data = [
    0x0D,       // 13
    0x7F,       // 127
    0x81, 0x00, // 128
    0xFE, 0x7F  // 16255
];
let mut readable = &data[..];

// case (1): pass by value
assert_eq!(0x0Du8, Vuint::decode(readable)?);
assert_eq!(0x0Du8, Vuint::decode(readable)?);

// case (2): pass by mutable reference
assert_eq!(0x0Du64, Vuint::decode(&mut readable)?);
assert_eq!(127u8, Vuint::decode(&mut readable)?);
assert_eq!(128u32, Vuint::decode(&mut readable)?);
assert_eq!(16255u16, Vuint::decode(&mut readable)?);

impl<'a> Vuint<'a>[src]

pub fn encode<W, I>(writer: W, input: &I) -> usize where
    W: BufMut,
    I: UnsignedInt
[src]

Write val to the std::io::Write stream w as an vuint-encoded integer.

Returns

After a successful write, the number of bytes written to w is returned.

Examples

Writing a u8 and an i128 into three bytes.

use lozizol::model::Vuint;

let mut buffer = [0u8; 3];
let mut writeable = &mut buffer[..];

let bytes_written = Vuint::encode(&mut writeable, &127u8);
assert_eq!(bytes_written, 1);

let bytes_written = Vuint::encode(&mut writeable, &256u128);
assert_eq!(bytes_written, 2);

let mut readable = &buffer[..];
assert_eq!(127u8, Vuint::decode(&mut readable)?);
assert_eq!(256u16, Vuint::decode(&mut readable)?);
}

impl<'a> Vuint<'a>[src]

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

pub fn validate(bytes: &[u8]) -> Result<(), VuintError>[src]

Trait Implementations

impl<'a> AsRef<[u8]> for Vuint<'a>[src]

fn as_ref(&self) -> &[u8][src]

exposes the internal vuint byte slice

impl<'a> Binary for Vuint<'a>[src]

Enables binary formatting of Vuint with "{:b}"

  • Supports alternate formating "{:#b}"
  • Supports padding "{:>100b}" Examples:
assert_eq!(format!("{:b}", Vuint::from(0xFFFFu16)), "10000011_11111111_01111111");
assert_eq!(format!("{:#b}", Vuint::from(0xFFFFu16)), "[0b_10000011, 0b_11111111, 0b_01111111]");
assert_eq!(format!("{:>30b}", Vuint::from(0xFFFFu16)), "    10000011_11111111_01111111");

impl<'a> Clone for Vuint<'a>[src]

impl<'a> Debug for Vuint<'a>[src]

impl Default for Vuint<'static>[src]

impl<'a> Eq for Vuint<'a>[src]

impl<'a, T> From<&'a T> for Vuint<'static> where
    T: UnsignedInt
[src]

impl From<bool> for Vuint<'static>[src]

impl From<u128> for Vuint<'static>[src]

impl From<u16> for Vuint<'static>[src]

impl From<u32> for Vuint<'static>[src]

impl From<u64> for Vuint<'static>[src]

impl From<u8> for Vuint<'static>[src]

impl From<usize> for Vuint<'static>[src]

impl FromStr for Vuint<'static>[src]

type Err = ParseError

The associated error which can be returned from parsing.

impl<'a> PartialEq<Vuint<'a>> for Vuint<'a>[src]

impl<'a> StructuralEq for Vuint<'a>[src]

impl<'a> StructuralPartialEq for Vuint<'a>[src]

Auto Trait Implementations

impl<'a> RefUnwindSafe for Vuint<'a>

impl<'a> Send for Vuint<'a>

impl<'a> Sync for Vuint<'a>

impl<'a> Unpin for Vuint<'a>

impl<'a> UnwindSafe for Vuint<'a>

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> ToHex for T where
    T: AsRef<[u8]>, 
[src]

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

type Owned = T

The resulting type after obtaining ownership.

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.