Skip to main content

unbounded_fields/
unbounded_fields.rs

1//! Fields with no spec-given bound (no `maxLength`/`maxItems`): a caller-
2//! chosen const generic by default, or a plain `alloc` collection with the
3//! `alloc` feature enabled.
4//!
5//! Run with: `cargo run --example unbounded_fields -p ocpp-types`
6//! Or:       `cargo run --example unbounded_fields -p ocpp-types --features alloc`
7
8use ocpp_types::v16::DataTransferResponse;
9use ocpp_types::v16::common::DataTransferResponseStatus;
10
11fn main() {
12    #[cfg(not(feature = "alloc"))]
13    {
14        // Uses the default capacity (1024).
15        let response: DataTransferResponse = DataTransferResponse {
16            data: Some(heapless::String::try_from("vendor payload").unwrap()),
17            status: DataTransferResponseStatus::Accepted,
18        };
19        println!(
20            "default capacity: {}",
21            response.data.as_ref().unwrap().capacity()
22        );
23
24        // Or pick a smaller one explicitly.
25        let response: DataTransferResponse<64> = DataTransferResponse {
26            data: Some(heapless::String::try_from("vendor payload").unwrap()),
27            status: DataTransferResponseStatus::Accepted,
28        };
29        println!(
30            "chosen capacity: {}",
31            response.data.as_ref().unwrap().capacity()
32        );
33    }
34
35    #[cfg(feature = "alloc")]
36    {
37        // With `alloc`, the const generic disappears entirely -- this is
38        // a plain, growable string (`alloc::string::String`, the same type
39        // as `std::string::String`).
40        let response = DataTransferResponse {
41            data: Some(String::from("vendor payload")),
42            status: DataTransferResponseStatus::Accepted,
43        };
44        println!("alloc mode, no capacity limit: {:?}", response.data);
45    }
46
47    // Timestamps are *not* in this category any more. Every OCPP version
48    // types them `{"type": "string", "format": "date-time"}` with no
49    // `maxLength`, so they used to take the 1024-byte default too -- they
50    // are now `OcppTimestamp`, which is 16 bytes and needs no parameter in
51    // either build.
52    let heartbeat = ocpp_types::v16::HeartbeatResponse {
53        current_time: ocpp_types::OcppTimestamp::parse_rfc3339("2024-01-01T00:00:00Z").unwrap(),
54    };
55    println!(
56        "timestamp: {} ({} bytes)",
57        heartbeat.current_time,
58        core::mem::size_of::<ocpp_types::OcppTimestamp>()
59    );
60}