Macro candid::Decode

source ·
macro_rules! Decode {
    ( $hex:expr $(,$ty:ty)* ) => { ... };
    ( [ $config:expr ] ; $hex:expr $(,$ty:ty)* ) => { ... };
    (@Debug [ $config:expr ] ; $hex:expr $(,$ty:ty)* ) => { ... };
    (@GetValue [$($ans:ident)*] $de:ident $ty:ty, $($tail:ty,)* ) => { ... };
    (@GetValue [$($ans:ident)*] $de:ident) => { ... };
}
Expand description

Decode Candid message into a tuple of Rust values of the given types. Produces Err if the message fails to decode at any given types. If the message contains only one value, it returns the value directly instead of a tuple.

use candid::{Encode, Decode, DecoderConfig};
let bytes = Encode!(&42u32, &"hello", &"extra arguments")?;
let (value1, value2) = Decode!(&bytes, u32, String)?;
assert_eq!(value1, 42);
assert_eq!(value2, "hello");

// Decode with quota
let mut config = DecoderConfig::new();
config.set_decoding_quota(1000).set_skipping_quota(50);
let (value1, value2) = Decode!([config]; &bytes, u32, String)?;
let ((value1, value2), cost) = Decode!(@Debug [config]; &bytes, u32, String)?;
// `cost` reports the decoding cost, not the remaining quota
assert_eq!(cost.decoding_quota, Some(846));
assert_eq!(cost.skipping_quota, Some(16));