Macro const_decoder::decode

source ·
macro_rules! decode {
    ($decoder:expr, $bytes:expr $(,)?) => { ... };
}
Expand description

Computes the output length in compile time and decodes the input. This allows to skip specifying output length manually.

The macro accepts two comma-separate expressions. The first arg must evaluate to Decoder, SkipWhitespace, or Pem. The second argument must evaluate to &[u8]. Both expressions must be assignable to constants. The output of a macro is an array [u8; N] with the decoded bytes.

§Examples

§Usage with Decoders

use const_decoder::{decode, Decoder};

const HEX: &[u8] = &decode!(Decoder::Hex, b"c0ffee");
const BASE64: &[u8] = &decode!(Decoder::Base64, b"VGVzdCBzdHJpbmc=");
// Can be used with custom decoders as well
const BASE32: &[u8] = &decode!(
    Decoder::custom("qpzry9x8gf2tvdw0s3jn54khce6mua7l"),
    b"rp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q",
);

§Usage with SkipWhitespace

const HEX: &[u8] = &decode!(
    Decoder::Hex.skip_whitespace(),
    b"c0ff ee00 beef",
);

§Usage with Pem

const PRIVATE_KEY: &[u8] = &decode!(
    Pem,
    b"-----BEGIN PRIVATE KEY-----
      MC4CAQAwBQYDK2VuBCIEINAOV4yAyaoM2wmJPApQs3byDhw7oJRG47V0VHwGnctD
      -----END PRIVATE KEY-----",
);