Skip to main content

decode_exact_no_alloc/
decode_exact_no_alloc.rs

1//! Decode and re-encode exactly one frame without allocation.
2//!
3//! The exact decoder requires a slice containing one complete frame. The
4//! decoded frame is written back into a fixed-size caller-owned buffer.
5//!
6//! Run with:
7//!
8//! ```sh
9//! cargo run -p meterbus-wired-datalink --example decode_exact_no_alloc
10//! ```
11
12use meterbus_wired_datalink::{
13    CommunicationType, Frame, LongFrame,
14    decoder::exact::{DecodeError, decode},
15};
16
17fn main() -> Result<(), DecodeError> {
18    let frame = decode(&[0x10, 0x5b, 0x01, 0x5c, 0x16])?;
19
20    if let Frame::Short(short) = &frame {
21        assert_eq!(
22            short.control().communication_type(),
23            CommunicationType::ReqUd2
24        );
25        assert_eq!(short.address().value(), 1);
26    }
27
28    let mut output = [0_u8; LongFrame::MAX_LEN];
29    assert_eq!(
30        frame
31            .encode_into(&mut output)
32            .expect("maximum frame buffer is large enough"),
33        [0x10, 0x5b, 0x01, 0x5c, 0x16]
34    );
35
36    Ok(())
37}