You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The documentation for the derive macro says that enum variants are encoded as a two-element array, with the first element being the variant id and the second element being the contents.
However, I couldn't find information on how/if the variant payload is encoded when it is a tuple struct.
For example, suppose, such I have an encoding like this:
#[cbor(transparent)] is not possible for enum variants, so I wanted to ask: is the S directly encoded into the second element of the array or is there some additional overhead involved by treating it as a tuple that wraps an S? In other words, is the tuple transparent?
The text was updated successfully, but these errors were encountered:
However, I couldn't find information on how/if the variant payload is encoded when it is a tuple struct.
The documentation states: "By default or if a struct has the #[cbor(array)] attribute, it will be represented as a CBOR array. Its index numbers are represened by the position of the field value in this array. [...] If a struct has the #[cbor(map)] attribute attached, then it will be represented as a CBOR map with keys corresponding to the numeric index value [...]"
This applies regardless of whether a struct is nested in an enum or not. In your example, S is encoded as a CBOR array. Encoding E results in a 2-element array, with the second element being the variant with an array of two elements.
Whenever you are not sure how types are encoded, you could also try minicbor::display to get a textual representation, e.g.
use minicbor::{Encode,Decode};#[derive(Encode,Decode)]structS{#[n(0)]a:u8,#[n(1)]b:u8,}#[derive(Encode,Decode)]enumE{#[n(0)]Foo(#[n(0)]S),#[n(1)]Bar(#[n(0)]S)}fnmain(){let bytes = minicbor::to_vec(E::Bar(S{a:42,b:99})).unwrap();println!("{}", minicbor::display(&bytes));}
gives:
[1, [[42, 99]]]
#[cbor(transparent)] is not possible for enum variants, so I wanted to ask: is the S directly encoded into the second element of the array or is there some additional overhead involved by treating it as a tuple that wraps an S? In other words, is the tuple transparent?
The encoding of S is independent of E, so the section about enum encoding applies as stated. The struct members of S are currently not encoded without their array, i.e. like [1, [42, 99]] in the example above.
The documentation for the derive macro says that enum variants are encoded as a two-element array, with the first element being the variant id and the second element being the contents.
However, I couldn't find information on how/if the variant payload is encoded when it is a tuple struct.
For example, suppose, such I have an encoding like this:
#[cbor(transparent)]
is not possible for enum variants, so I wanted to ask: is theS
directly encoded into the second element of the array or is there some additional overhead involved by treating it as a tuple that wraps anS
? In other words, is the tuple transparent?The text was updated successfully, but these errors were encountered: