[][src]Function mqttrs::decode

pub fn decode(buffer: &mut BytesMut) -> Result<Option<Packet>, Error>

Decode bytes from a BytesMut buffer as a Packet enum.

// Fill a buffer with encoded data (probably from a `TcpStream`).
let mut buf = BytesMut::from(vec![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]);

// 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),
}