Skip to content

Add MaxCborLen trait and derive macro for compile-time max CBOR size#56

Open
menasheofd wants to merge 1 commit into
twittner:developfrom
menasheofd:max-cbor-len
Open

Add MaxCborLen trait and derive macro for compile-time max CBOR size#56
menasheofd wants to merge 1 commit into
twittner:developfrom
menasheofd:max-cbor-len

Conversation

@menasheofd

Copy link
Copy Markdown

Add MaxCborLen trait and #[derive(MaxCborLen)]

Adds compile-time maximum CBOR encoding size calculation, useful for
fixed-size stack buffers in no_std without alloc:

#[derive(Encode, MaxCborLen)]
#[cbor(array)]
struct Msg {
    #[n(0)] sensor_id: u8,
    #[n(1)] reading: u32,
}

let mut buf = [0u8; Msg::MAX_CBOR_LEN];
minicbor::encode(&msg, buf.as_mut_slice()).unwrap();

Useful for allocating fixed-size buffers in no_std without alloc.

Signed-off-by: David Menasheof <menasheofd@gmail.com>
@kotfind

kotfind commented Apr 28, 2026

Copy link
Copy Markdown

+1 for this feature.

I also think an integration with typenum crate would be cool. Currently rust's support for const generics in quite limited, but typenum + generic-array often solve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants