Skip to main content

decode_into

Function decode_into 

Source
pub fn decode_into<T: AsRef<str>>(
    input: T,
    strict: bool,
    buffer: &mut Vec<u8>,
) -> Result<Base>
Expand description

Decode the base string, writing the result into an existing buffer.

This is a zero-copy variant of decode that reuses an existing Vec<u8> buffer, avoiding allocations when decoding multiple values in a loop.

The buffer will be cleared before decoding, then filled with the decoded bytes. Returns the base encoding that was detected from the prefix.

§Examples

use multi_base::{Base, decode_into};

let mut buffer = Vec::new();

// Decode multiple values reusing the same buffer
let base = decode_into("zCn8eVZg", true, &mut buffer).unwrap();
assert_eq!(base, Base::Base58Btc);
assert_eq!(buffer, b"hello");

let base = decode_into("md29ybGQ", true, &mut buffer).unwrap();
assert_eq!(base, Base::Base64);
assert_eq!(buffer, b"world");

§Performance

When decoding many values, this function can be significantly faster than decode as it reuses the allocated buffer instead of allocating a new Vec<u8> for each decoding operation.

§Errors

Returns an error if:

  • The input string is empty
  • The base code prefix is unknown
  • The encoded data is invalid for the specified base