Function mqttrs::decode_slice [−][src]
pub fn decode_slice<'a>(buf: &'a [u8]) -> Result<Option<Packet<'a>>, Error>
Expand description
Decode bytes from a BytesMut buffer as a Packet enum.
The buf is never actually written to, it only takes a BytesMut
instead of a Bytes
to
allow using the same buffer to read bytes from network.
// Fill a buffer with encoded data (probably from a `TcpStream`). let mut buf = BytesMut::from(&[0b00110000, 11, 0, 4, 't' as u8, 'e' as u8, 's' as u8, 't' as u8, 'h' as u8, 'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8] as &[u8]); // Parse the bytes and check the result. match decode_slice(&mut buf) { Ok(Some(Packet::Publish(p))) => { assert_eq!(p.payload, b"hello"); }, // In real code you probably don't want to panic like that ;) Ok(None) => panic!("not enough data"), other => panic!("unexpected {:?}", other), }