[−][src]Function mqttrs::decode
pub fn decode(buf: &mut BytesMut) -> Result<Option<Packet>, Error>
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(&mut buf) { Ok(Some(Packet::Publish(p))) => { assert_eq!(p.payload, "hello".as_bytes().to_vec()); }, // In real code you probably don't want to panic like that ;) Ok(None) => panic!("not enough data"), other => panic!("unexpected {:?}", other), }