Trait Engine
trait Engine: Send + Sync
An Engine provides low-level encoding and decoding operations that all other higher-level parts of the API use. Users of the library will generally not need to implement this.
Different implementations offer different characteristics. The library currently ships with [GeneralPurpose] that offers good speed and works on any CPU, with more choices coming later, like a constant-time one when side channel resistance is called for, and vendor-specific vectorized ones for more speed.
See [general_purpose::STANDARD_NO_PAD] if you just want standard base64. Otherwise, when possible, it's
recommended to store the engine in a const so that references to it won't pose any lifetime
issues, and to avoid repeating the cost of engine setup.
Since almost nobody will need to implement Engine, docs for internal methods are hidden.
Associated Types
type Config: TraitBound { trait_: Path { path: "Config", id: Id(242), args: None }, generic_params: [], modifier: None }The config type used by this engine
type DecodeEstimate: TraitBound { trait_: Path { path: "DecodeEstimate", id: Id(254), args: None }, generic_params: [], modifier: None }The decode estimate used by this engine
Required Methods
fn config(self: &Self) -> &<Self as >::ConfigReturns the config for this engine.
Provided Methods
fn encode<T: AsRef<[u8]>>(self: &Self, input: T) -> StringEncode arbitrary octets as base64 using the provided
Engine. Returns aString.Example
use ; let b64 = STANDARD.encode; println!; const CUSTOM_ENGINE: GeneralPurpose = new; let b64_url = CUSTOM_ENGINE.encode;fn encode_string<T: AsRef<[u8]>>(self: &Self, input: T, output_buf: &mut String)Encode arbitrary octets as base64 into a supplied
String. Writes into the suppliedString, which may allocate if its internal buffer isn't big enough.Example
use ; const CUSTOM_ENGINE: GeneralPurpose = new;fn encode_slice<T: AsRef<[u8]>>(self: &Self, input: T, output_buf: &mut [u8]) -> Result<usize, EncodeSliceError>Encode arbitrary octets as base64 into a supplied slice. Writes into the supplied output buffer.
This is useful if you wish to avoid allocation entirely (e.g. encoding into a stack-resident or statically-allocated buffer).
Example
use ; let s = b"hello internet!"; let mut buf = Vecnew; // make sure we'll have a slice big enough for base64 + padding buf.resize; let bytes_written = STANDARD.encode_slice.unwrap; // shorten our vec down to just what was written buf.truncate; assert_eq!;fn decode<T: AsRef<[u8]>>(self: &Self, input: T) -> Result<Vec<u8>, DecodeError>Decode the input into a new
Vec.Example
use ; let bytes = STANDARD .decode.unwrap; println!; // custom engine setup let bytes_url = new .decode.unwrap; println!;fn decode_vec<T: AsRef<[u8]>>(self: &Self, input: T, buffer: &mut Vec<u8>) -> Result<(), DecodeError>Decode the
inputinto the suppliedbuffer.Writes into the supplied
Vec, which may allocate if its internal buffer isn't big enough. Returns aResultcontaining an empty tuple, aka().Example
use ; const CUSTOM_ENGINE: GeneralPurpose = new;fn decode_slice<T: AsRef<[u8]>>(self: &Self, input: T, output: &mut [u8]) -> Result<usize, DecodeSliceError>Decode the input into the provided output slice.
Returns the number of bytes written to the slice, or an error if
outputis smaller than the estimated decoded length.This will not write any bytes past exactly what is decoded (no stray garbage bytes at the end).
See [crate::decoded_len_estimate] for calculating buffer sizes.
See [Engine::decode_slice_unchecked] for a version that panics instead of returning an error if the output buffer is too small.
fn decode_slice_unchecked<T: AsRef<[u8]>>(self: &Self, input: T, output: &mut [u8]) -> Result<usize, DecodeError>Decode the input into the provided output slice.
Returns the number of bytes written to the slice.
This will not write any bytes past exactly what is decoded (no stray garbage bytes at the end).
See [crate::decoded_len_estimate] for calculating buffer sizes.
See [Engine::decode_slice] for a version that returns an error instead of panicking if the output buffer is too small.
Panics
Panics if the provided output buffer is too small for the decoded data.
Implementors
impl Engine for GeneralPurpose