Expand description
The seqbytes library
The seqbytes crate provides the traits ESeqByteReader
and SeqByteReader
used for reading bytes sequentially. The SeqByteReader
trait convert the bytes into the
specified generic type, denoted U
, which must implement SizedNumber
. The trait SizedNumber
represents a type which can be converted to and from bytes, with a
fixed size in bytes.
The trait ESeqByteReader
is used for reading bytes sequentially, converting to a type with a specific endianness. The type converted to must implement EndianNumber
,
which represents a type which can be converted to and from bytes with a specific endianness.
Implementation
The traits E$eqByteReader
and SeqByteReader
are implemented by default on types implementing Read
+ Seek
.
Example 1
Using [SizedNumber
] trait to convert numbers.
use seqbytes::traits::*;
let a = 22.4f64;
let b = a.to_bytes();
let c = f64::from_bytes(&b);
let d = u32::from_bytes(&[2, 4, 6]);
assert_eq!(c, Some(22.4));
assert_eq!(d, None);
Example 2
Using [seqbytes::bytes::SeqByteReader
] trait to read a Vector sequentially
use seqbytes::prelude::*;
use std::io::Cursor;
use std::str::FromStr;
let a = vec![69, 96, 255, 255, 0x68, 0x65, 0x6C, 0x6C, 0x6F];
let mut cursor = Cursor::new(a);
let num : i32 = cursor.shift().unwrap();
let s = &*cursor.shift_string(5).unwrap();
assert_eq!(num, -40891);
assert_eq!(*s, *"hello");