rmp::decode

Function read_int

Source
pub fn read_int<T: FromPrimitive, R: RmpRead>(
    rd: &mut R,
) -> Result<T, NumValueReadError<R::Error>>
Expand description

Attempts to read up to 9 bytes from the given reader and to decode them as integral T value.

This function will try to read up to 9 bytes from the reader (1 for marker and up to 8 for data) and interpret them as a big-endian T.

Unlike read_*, this function weakens type restrictions, allowing you to safely decode packed values even if you aren’t sure about the actual integral type.

§Errors

This function will return NumValueReadError on any I/O error while reading either the marker or the data.

It also returns NumValueReadError::OutOfRange if the actual type is not an integer or it does not fit in the given numeric range.

§Examples

let buf = [0xcd, 0x1, 0x2c];

assert_eq!(300u16, rmp::decode::read_int(&mut &buf[..]).unwrap());
assert_eq!(300i16, rmp::decode::read_int(&mut &buf[..]).unwrap());
assert_eq!(300u32, rmp::decode::read_int(&mut &buf[..]).unwrap());
assert_eq!(300i32, rmp::decode::read_int(&mut &buf[..]).unwrap());
assert_eq!(300u64, rmp::decode::read_int(&mut &buf[..]).unwrap());
assert_eq!(300i64, rmp::decode::read_int(&mut &buf[..]).unwrap());
assert_eq!(300usize, rmp::decode::read_int(&mut &buf[..]).unwrap());
assert_eq!(300isize, rmp::decode::read_int(&mut &buf[..]).unwrap());