bytes
```rust
fn testbytesbuffernormal() { let mut bb = Buffer::new(); bb.push(Bytes::from(vec![0x01, 0x02])); asserteq!(2, bb.len()); asserteq!(0x01, bb[0]); asserteq!(0x02, bb[1]); bb.push(Bytes::from(vec![0x03, 0x04])); asserteq!(4, bb.len()); asserteq!(0x03, bb[2]); asserteq!(0x04, bb[3]); bb.push(Bytes::from(vec![])); asserteq!(4, bb.len()); bb.push(Bytes::from(vec![0x5])); asserteq!(5, bb.len()); bb.push(Bytes::from(vec![0x06, 0x07, 0x08])); asserteq!(8, bb.len()); }
fn testbytesbufferslice() { let mut bb = Buffer::new(); bb.push(Bytes::from(&[0x01][..])); bb.push(Bytes::from(&[0x02][..])); bb.push(Bytes::from(&[0x03][..])); bb.push(Bytes::from(&[0x04][..])); bb.push(Bytes::from(&[0x05][..])); bb.push(Bytes::from(&[0x06, 0x07, 0x08, 0x09, 0x0a][..])); let bb2 = bb.slice(1, 5); asserteq!(4, bb2.len()); asserteq!(0x2, bb2[0]); asserteq!(0x5, bb2[3]); let bb3 = bb.slice(3, 8); assert_eq!(5, bb3.len()); }
fn testbuffertruncate() { let mut bb = Buffer::new(); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 bb.push(Bytes::from(&[0x00, 0x01][..])); bb.push(Bytes::from(&[0x02][..])); bb.push(Bytes::from(&[0x03][..])); bb.push(Bytes::from(&[0x04][..])); bb.push(Bytes::from(&[0x05][..])); bb.push(Bytes::from(&[0x06, 0x07, 0x08, 0x09, 0x0a][..])); asserteq!(11, bb.len()); println!("{:?}", bb); bb.truncate(10); // 0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9 println!("{:?}", bb); asserteq!(10, bb.len()); asserteq!(0, bb[0]); asserteq!(9, bb[9]); bb.advance(1); // 1, 2, 3, 4, 5, 6, 7, 8, 9 asserteq!(9, bb.len()); asserteq!(1, bb[0]); asserteq!(9, bb[8]); bb.truncate(8); bb.advance(1); // 2, 3, 4, 5, 6, 7, 8 asserteq!(7, bb.len()); asserteq!(2, bb[0]); asserteq!(8, bb[6]); }
fn testnumberforbuffer() { use crate::*; use bytes::Bytes; let mut buf = Buffer::new(); buf.push(Bytes::from(vec![0x01u8, 0x02])); buf.push(Bytes::from(vec![3, 4, 5, 6, 7, 8, 9, 10])); asserteq!(1, buf.u8(0)); asserteq!(0x04030201, buf.u32le(0)); asserteq!(0x01020304, buf.u32be(0)); } ```