encode_into_unchecked

Function encode_into_unchecked 

Source
pub unsafe fn encode_into_unchecked(
    input: &[u8],
    output: &mut [MaybeUninit<u8>],
) -> usize
Expand description

Encode input as base64 into the provided slice without validating its length.

Returns the amount of bytes written. Written bytes are guaranteed to be ASCII.

§Safety

  • output’s length must be AT LEAST (input.len() * 4 + 2) / 3.

§Example

use weakauras_codec_base64::encode;

let input = b"Hello, world!";
let required_capacity = encode::calculate_encoded_len(input).unwrap();
let mut output = Vec::with_capacity(required_capacity);

// SAFETY:
// - buffer's capacity is enough for storing base64-encoded input;
// - encode_into_unchecked returns the amount of bytes written,
//   thus it is safe to call set_len using its return value.
unsafe {
    let bytes_written = encode::encode_into_unchecked(input, output.spare_capacity_mut());
    output.set_len(bytes_written);
}

assert_eq!(output, b"ivgBS9glGC3BYXgzHa");