[][src]Function msb128::read_positive

pub fn read_positive<R, I>(reader: R) -> Result<I, ReadError> where
    R: Read,
    I: PrimInt

Read a variable length and MSB128-encoded integer from r. The returned integer is positive. Reading negative integers is not supported.

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 msb128::read_positive;

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

assert_eq!(10i16, read_positive(&mut readable)?);
assert_eq!(20i8, read_positive(&mut readable)?);
assert_eq!(30i32, read_positive(&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 msb128::read_positive;

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

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

// case (2): pass by mutable reference
assert_eq!(0x0Du64, read_positive(&mut readable)?);
assert_eq!(127u8, read_positive(&mut readable)?);
assert_eq!(256i32, read_positive(&mut readable)?);
assert_eq!(16383u16, read_positive(&mut readable)?);