Skip to main content

Engine

Struct Engine 

Source
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

Source

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 padding
Source

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.

Source

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 to output.
  • Err(Error::BufferTooSmall): If output.len() is less than encoded_len.
Source

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 to output.
  • Err(Error): If the input is invalid or the buffer is too small.
Source

pub fn encode<T: AsRef<[u8]> + Sync>(&self, input: T) -> String

Available on crate feature 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=");
Source

pub fn decode<T: AsRef<[u8]> + Sync>(&self, input: T) -> Result<Vec<u8>, Error>

Available on crate feature std only.

Allocates a new Vec<u8> and decodes the input data into it.

§Errors

Returns Error if the input contains invalid characters or has an invalid length.

§Examples
use base64_turbo::STANDARD;
let bytes = STANDARD.decode("aGVsbG8=").unwrap();
assert_eq!(bytes, b"hello");

Trait Implementations§

Source§

impl Clone for Engine

Source§

fn clone(&self) -> Engine

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Engine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for Engine

Auto Trait Implementations§

§

impl Freeze for Engine

§

impl RefUnwindSafe for Engine

§

impl Send for Engine

§

impl Sync for Engine

§

impl Unpin for Engine

§

impl UnwindSafe for Engine

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.