Function basenc::decode [] [src]

pub fn decode<C: Encoding, B: DecodeBuf>(
    string: &str,
    encoding: C,
    buffer: B
) -> Result<B::Output, Error>

Directly decode into a decode buffer.

Convenient as it doesn't require the Encoding trait to be imported.

Decoding may fail and produce an Error instead.

Examples

assert_eq!(
    basenc::decode("aGVsbG8gd29ybGQ=", basenc::Base64Std, Vec::new()),
    Ok(b"hello world"[..].to_vec())
);
// Note that the buffer is swallowed on error
assert_eq!(
    basenc::decode("&'nv@l!d", basenc::Base64Std, Vec::new()),
    Err(basenc::Error::InvalidChar('&'))
);

Convenience, appends to a by-value buffer and returns that buffer.

let mut byte_buf = vec![0x11, 0x22, 0x33];
assert_eq!(
    basenc::decode("QnVGZkVyIFJlVXNFIQ", basenc::Base64Url, &mut byte_buf),
    Ok(&b"BuFfEr ReUsE!"[..])
);
assert_eq!(byte_buf, b"\x11\x22\x33BuFfEr ReUsE!");

Appends to an existing buffer, returns a reference to the decoded input.

let mut stack_buf = [0u8; 16];
assert_eq!(
    basenc::decode("0080FFDC", basenc::UpperHex, &mut stack_buf[..]),
    Ok(&b"\x00\x80\xFF\xDC"[..])
);

Uses fixed-size arrays on the stack as a buffer, available with #[no_std].

Panics if the buffer is too small to fit the output.