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
impl Base64Encoder
Sourcepub fn standard() -> Self
pub fn standard() -> Self
Creates a strict standard Base64 encoder with padding.
The encoder requires reset before streaming and does not allocate.
Sourcepub fn standard_no_pad() -> Self
pub fn standard_no_pad() -> Self
Creates a strict standard Base64 encoder without padding.
The encoder requires reset before streaming and does not allocate.
Sourcepub fn url_safe() -> Self
pub fn url_safe() -> Self
Creates a strict URL-safe Base64 encoder with padding.
The encoder requires reset before streaming and does not allocate.
Sourcepub fn url_safe_no_pad() -> Self
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
impl Debug for Base64Encoder
Source§impl Default for Base64Encoder
impl Default for Base64Encoder
Source§impl TranscodeEncoder for Base64Encoder
impl TranscodeEncoder for Base64Encoder
Source§type EncodeError = !
type EncodeError = !
Source§impl Transcoder for Base64Encoder
impl Transcoder for Base64Encoder
Source§fn max_transcode_output_len(
&self,
input_len: usize,
) -> Result<usize, CapacityError>
fn max_transcode_output_len( &self, input_len: usize, ) -> Result<usize, CapacityError>
input_len units during the streaming phase. Read moreSource§fn max_finish_output_len(&self) -> Result<usize, CapacityError>
fn max_finish_output_len(&self) -> Result<usize, CapacityError>
Source§fn reset(
&mut self,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::Error>
fn reset( &mut self, output: &mut [u8], output_index: usize, ) -> Result<usize, Self::Error>
output. Read moreSource§fn transcode(
&mut self,
input: &[u8],
input_index: usize,
output: &mut [u8],
output_index: usize,
) -> Result<TranscodeProgress, Self::Error>
fn transcode( &mut self, input: &[u8], input_index: usize, output: &mut [u8], output_index: usize, ) -> Result<TranscodeProgress, Self::Error>
Source§fn finish(
&mut self,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::Error>
fn finish( &mut self, output: &mut [u8], output_index: usize, ) -> Result<usize, Self::Error>
Source§fn max_reset_output_len(&self) -> Result<usize, CapacityError>
fn max_reset_output_len(&self) -> Result<usize, CapacityError>
Source§fn max_total_output_len(&self, input_len: usize) -> Result<usize, CapacityError>
fn max_total_output_len(&self, input_len: usize) -> Result<usize, CapacityError>
reset -> transcode_eof -> finish
stream. Read more