Skip to main content

Base64Encoder

Struct Base64Encoder 

Source
pub struct Base64Encoder { /* private fields */ }
Expand description

Encodes bytes incrementally using a strict Base64 configuration.

The encoder uses fixed-size state and does not allocate. Call reset before streaming. Input and output buffers remain caller-owned. It may retain up to two consumed input bytes until a complete quantum is available or finish encodes the final partial quantum. Retained bytes are already included in the reported input consumption and must not be submitted again. If output capacity is insufficient, use the returned progress to retry with the unconsumed input and a larger output buffer. NeedOutput.required is the minimum total capacity from the advanced output position (output_index + progress.written()) for the next step. When retaining the output buffer, its length must cover that position plus required; when switching to an empty buffer at index zero, provide at least required slots.

§Examples

use qubit_codec::Transcoder;
use qubit_codec_misc::Base64Encoder;

let mut encoder = Base64Encoder::standard();
let mut output = [0; 4];
encoder.reset(&mut output, 0).unwrap();
let progress = encoder.transcode(b"M", 0, &mut output, 0).unwrap();
assert_eq!(progress.read(), 1);
assert_eq!(progress.written(), 0);
let written = encoder.finish(&mut output, 0).unwrap();
assert_eq!(&output[..written], b"TQ==");

Retrying after output pressure must advance both indices. The encoder has already consumed the first five bytes of ManMan, so only the final n is submitted again:

use qubit_codec::TranscodeStatus;
use qubit_codec::Transcoder;
use qubit_codec_misc::Base64Encoder;

let mut encoder = Base64Encoder::standard();
encoder.reset(&mut [], 0).unwrap();
let mut output = [0; 4];
let first = encoder.transcode(b"ManMan", 0, &mut output, 0).unwrap();
assert_eq!((first.read(), first.written()), (5, 4));
assert_eq!(&output, b"TWFu");
let required = match first.status() {
    TranscodeStatus::NeedOutput { required } => required.get(),
    status => panic!("expected NeedOutput, got {status:?}"),
};
let mut larger = [0; 8];
larger[..4].copy_from_slice(&output);
let next = encoder.transcode(b"ManMan", first.read(), &mut larger, first.written()).unwrap();
assert_eq!(next.read(), 1);
assert_eq!(next.written(), 4);
assert!(required <= larger.len() - first.written());
assert_eq!(encoder.finish(&mut larger, first.written() + next.written()).unwrap(), 0);
assert_eq!(&larger, b"TWFuTWFu");

Implementations§

Source§

impl Base64Encoder

Source

pub fn standard() -> Self

Creates a strict standard Base64 encoder with padding.

The encoder requires reset before streaming and does not allocate.

Source

pub fn standard_no_pad() -> Self

Creates a strict standard Base64 encoder without padding.

The encoder requires reset before streaming and does not allocate.

Source

pub fn url_safe() -> Self

Creates a strict URL-safe Base64 encoder with padding.

The encoder requires reset before streaming and does not allocate.

Source

pub fn url_safe_no_pad() -> Self

Creates a strict URL-safe Base64 encoder without padding.

The encoder requires reset before streaming and does not allocate.

Trait Implementations§

Source§

impl Debug for Base64Encoder

Source§

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

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

impl Default for Base64Encoder

Source§

fn default() -> Self

Creates the default strict standard Base64 encoder with padding.

The encoder must be reset before streaming.

Source§

impl TranscodeEncoder for Base64Encoder

Source§

type EncodeError = !

The encoder cannot fail while converting an input byte.

Source§

impl Transcoder for Base64Encoder

Source§

type Input = u8

Base64 encoding consumes bytes.

Source§

type Output = u8

Base64 encoding produces bytes.

Source§

type Error = TranscodeEncodeError<!, u8>

Reports streaming state or output-capacity failures; encoding itself is infallible.

Source§

fn max_transcode_output_len( &self, input_len: usize, ) -> Result<usize, CapacityError>

Returns an upper bound for encoding input_len input bytes.

§Parameters
  • input_len: Number of input bytes to encode.
§Returns

The maximum encoded length, or a capacity error if that length overflows.

Source§

fn max_finish_output_len(&self) -> Result<usize, CapacityError>

Returns the maximum output needed to encode the retained final bytes.

§Returns

Four bytes for padded output, or three bytes for unpadded output.

§Errors

This method always returns Ok; its constant bound cannot overflow.

Source§

fn reset( &mut self, output: &mut [u8], output_index: usize, ) -> Result<usize, Self::Error>

Starts a new stream and validates the output position.

Any bytes retained from an earlier stream are discarded.

§Parameters
  • output: Caller-owned output storage; it is not modified.
  • output_index: Position at which subsequent output will be written.
§Returns

Zero bytes written.

§Errors

Returns an error if output_index is outside output.

Source§

fn transcode( &mut self, input: &[u8], input_index: usize, output: &mut [u8], output_index: usize, ) -> Result<TranscodeProgress, Self::Error>

Encodes complete Base64 quanta and retains up to two trailing bytes.

Retained bytes count as consumed; resuming must use the returned input position so those bytes are not submitted twice. On output pressure, continue with the reported indices and sufficient output capacity.

§Parameters
  • input: Caller-owned input bytes.
  • input_index: Position of the first input byte to consume.
  • output: Caller-owned destination storage.
  • output_index: Position of the first output byte to write.
§Returns

Progress containing the input consumed, output written, and whether more output capacity is required.

§Errors

Returns an error for invalid indices or when the stream has not been reset or has already been finished.

Source§

fn finish( &mut self, output: &mut [u8], output_index: usize, ) -> Result<usize, Self::Error>

Encodes any retained final bytes and marks the stream as finished.

§Parameters
  • output: Caller-owned destination storage.
  • output_index: Position at which final encoded bytes are written.
§Returns

The number of bytes written for the final partial quantum.

§Errors

Returns an error for an invalid output index, insufficient capacity, or a stream that was not reset or was already finished.

Source§

fn max_reset_output_len(&self) -> Result<usize, CapacityError>

Returns an upper bound for output units emitted when resetting stream state. Read more
Source§

fn max_total_output_len(&self, input_len: usize) -> Result<usize, CapacityError>

Returns an upper bound for a complete reset -> transcode_eof -> finish stream. Read more
Source§

fn transcode_eof( &mut self, input: &[Self::Input], input_index: usize, output: &mut [Self::Output], output_index: usize, ) -> Result<TranscodeProgress, Self::Error>

Converts source units after the caller has established end of input. Read more
Source§

fn transcode_complete_into( &mut self, input: &[Self::Input], output: &mut [Self::Output], ) -> Result<usize, Self::Error>

Runs a complete one-shot reset -> transcode -> finish stream. Read more

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.