Macro oasis_cbor::destructure_cbor_map [−][src]
macro_rules! destructure_cbor_map { (let { $($key : expr => $variable : ident,) + } = $map : expr ;) => { ... }; }
Expand description
This macro generates code to extract multiple values from a Vec<(Value, Value)>
at once
in an optimized manner, consuming the input vector.
It takes as input a Vec
as well as a list of identifiers and keys, and generates code
that assigns the corresponding values to new variables using the given identifiers. Each of
these variables has type Option<Value>
, to account for the case where keys aren’t found.
Important: Keys passed to the destructure_cbor_map!
macro must be sorted in increasing
order. If not, the algorithm can yield incorrect results, such a assigning None
to a variable
even if the corresponding key existed in the map. No runtime checks are made for this in the
destructure_cbor_map!
macro, in order to avoid overhead at runtime. However, assertions that
keys are sorted are added in cfg(test)
mode, so that unit tests can verify ahead of time that
the keys are indeed sorted. This macro is therefore not suitable for dynamic keys that can
change at runtime.
Example usage:
destructure_cbor_map! { let { 1 => x, "key" => y, } = map; }