use courierust::courierust_bytes::{Bytes, BytesMut};
fn main() {
let mut buf = BytesMut::with_capacity(4);
buf.put_u8(0x01);
buf.put_u16(0x0203);
buf.put_u8(0x04);
assert_eq!(buf.as_slice(), &[0x01, 0x02, 0x03, 0x04]);
println!("BytesMut put_u8/put_u16/put_u8 -> {:02x?}", buf.as_slice());
let bytes = buf.freeze();
assert_eq!(bytes.len(), 4);
println!("freeze -> len={} (single allocation, no copy)", bytes.len());
let head = bytes.slice(0..2);
let tail = bytes.slice_from(2);
assert_eq!(head.as_slice(), &[0x01, 0x02]);
assert_eq!(tail.as_slice(), &[0x03, 0x04]);
println!(
"slice(..2) -> {:02x?} slice_from(2) -> {:02x?}",
head.as_slice(),
tail.as_slice()
);
let mut packet = Bytes::from_static(b"hello world");
let hello = packet.split_to(5);
assert_eq!(hello.as_slice(), b"hello");
assert_eq!(packet.as_slice(), b" world");
println!(
"split_to(5) -> {:?} | remaining -> {:?}",
hello.as_slice(),
packet.as_slice()
);
let payload = b"GET / HTTP/1.1";
let mut frame = BytesMut::with_capacity(4 + payload.len());
frame.extend_from_slice(&(payload.len() as u32).to_be_bytes());
frame.extend_from_slice(payload);
let mut frame = frame.freeze();
let len_field = frame.split_to(4);
let len = u32::from_be_bytes(len_field.as_slice().try_into().unwrap()) as usize;
let body = frame.split_to(len);
assert_eq!(body.as_slice(), payload);
println!(
"length-prefixed frame: len={len} body={:?} (split, zero-copy)",
body.as_slice()
);
println!("all zero-copy window operations verified");
}