Skip to main content

fwd_write/
fwd_write.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
3
4/*!
5 * @brief One concept: a remote write is one FWD frame — `op`, the `dst` route, the
6 * `src` reply route, then the VALUE (RFC-0004 §B).
7 *
8 * Run: `cargo run --example fwd_write` (from `bindings/rust/`).
9 */
10
11use libtracer::fwd::{fwd_dst_path, fwd_op};
12use libtracer::{decode_fwd, encode_fwd_bytes, value_u32, FwdRequest};
13
14fn main() {
15    // Write 21500 (a temperature in milli-degrees) to /sensor/temp on a remote node.
16    let mut req = FwdRequest::new(fwd_op::WRITE, &["sensor", "temp"], &["client"]);
17    req.payload = Some(value_u32(21_500));
18    let frame = encode_fwd_bytes(&req).expect("a valid request");
19    println!("FWD{{WRITE}} ({} bytes): {:02x?}", frame.len(), frame);
20
21    // The receiving node reads the same four children back, positionally.
22    let parsed = decode_fwd(&frame).expect("a well-formed FWD");
23    assert_eq!(parsed.op, fwd_op::WRITE);
24    let dst = fwd_dst_path(&parsed).unwrap();
25    assert_eq!(dst, "/sensor/temp");
26    let value = parsed.payload.as_ref().expect("a WRITE carries its value");
27    assert_eq!(value.payload_uint(), 21_500);
28    println!("dst {dst} <- {}", value.payload_uint());
29
30    // The frame carries no correlation id: the reply finds its way home along `src`.
31    assert_eq!(parsed.src.payload, [6, b'c', b'l', b'i', b'e', b'n', b't']);
32}