pub struct Engine { /* private fields */ }Expand description
A high-performance, stateless Base64 encoder/decoder.
This struct holds the configuration for encoding/decoding (alphabet choice and padding). It is designed to be immutable and thread-safe.
§Examples
use base64_turbo::STANDARD;
let data = b"Hello world";
// Encode to String
let encoded = STANDARD.encode(data);
assert_eq!(encoded, "SGVsbG8gd29ybGQ=");
// Decode to Result<Vec<u8>, Error>
let decoded = STANDARD.decode(&encoded).unwrap();
assert_eq!(decoded, data);Implementations§
Source§impl Engine
impl Engine
Sourcepub const fn encoded_len(&self, input_len: usize) -> usize
pub const fn encoded_len(&self, input_len: usize) -> usize
Calculates the exact buffer size required to encode input_len bytes.
This method computes the size based on the current configuration (padding vs. no padding).
§Examples
use base64_turbo::STANDARD;
assert_eq!(STANDARD.encoded_len(3), 4);
assert_eq!(STANDARD.encoded_len(1), 4); // With paddingSourcepub const fn estimate_decoded_len(&self, input_len: usize) -> usize
pub const fn estimate_decoded_len(&self, input_len: usize) -> usize
Calculates the maximum buffer size required to decode input_len bytes.
§Note
This is an upper-bound estimate. The actual number of bytes written during decoding will likely be smaller.
You should rely on the usize returned by decode_into
to determine the actual valid slice of the output buffer.
Sourcepub fn encode_into<T: AsRef<[u8]> + Sync>(
&self,
input: T,
output: &mut [u8],
) -> Result<usize, Error>
pub fn encode_into<T: AsRef<[u8]> + Sync>( &self, input: T, output: &mut [u8], ) -> Result<usize, Error>
Encodes input into the provided output buffer.
This is a “Zero-Allocation” API designed for hot paths. It writes directly
into the destination slice without creating intermediate Vec.
§Parallelism
If the parallel feature is enabled and the input size exceeds the
internal threshold (default: 512KB), this method automatically uses
Rayon to process chunks in parallel, saturating memory bandwidth.
§Arguments
input: The binary data to encode.output: A mutable slice to write the Base64 string into.
§Returns
Ok(usize): The actual number of bytes written tooutput.Err(Error::BufferTooSmall): Ifoutput.len()is less thanencoded_len.
Sourcepub fn decode_into<T: AsRef<[u8]> + Sync>(
&self,
input: T,
output: &mut [u8],
) -> Result<usize, Error>
pub fn decode_into<T: AsRef<[u8]> + Sync>( &self, input: T, output: &mut [u8], ) -> Result<usize, Error>
Decodes input into the provided output buffer.
§Performance
Like encoding, this method supports automatic parallelization for large payloads. It verifies the validity of the Base64 input while decoding.
§Returns
Ok(usize): The actual number of bytes written tooutput.Err(Error): If the input is invalid or the buffer is too small.
Sourcepub fn encode<T: AsRef<[u8]> + Sync>(&self, input: T) -> String
Available on crate feature std only.
pub fn encode<T: AsRef<[u8]> + Sync>(&self, input: T) -> String
std only.Allocates a new String and encodes the input data into it.
This is the most convenient method for general usage.
§Examples
use base64_turbo::STANDARD;
let b64 = STANDARD.encode(b"hello");
assert_eq!(b64, "aGVsbG8=");