Skip to main content

encode

Function encode 

Source
pub fn encode<const UPPER: bool>(
    src: &[u8],
    dst: &mut [MaybeUninit<u8>],
) -> Result<(), InvalidInput>
Expand description

Encodes the input bytes to hexadecimal string and writes it to the output buffer.

§Examples

use core::mem::MaybeUninit;

let input = b"Hello, world!";
let mut output = vec![MaybeUninit::<u8>::uninit(); input.len() * 2];

fashex::encode::<false>(input, &mut output).expect("infallible: the length must be valid");

#[allow(
    unsafe_code,
    reason = "We have encoded the input to hexadecimal string"
)]
let output = unsafe { output.assume_init_ref() };

assert_eq!(output, b"48656c6c6f2c20776f726c6421");

let mut output = vec![MaybeUninit::<u8>::uninit(); input.len() * 2];

fashex::encode::<true>(input, &mut output).expect("infallible: the length must be valid");

#[allow(
    unsafe_code,
    reason = "We have encoded the input to hexadecimal string"
)]
let output = unsafe { output.assume_init_ref() };

assert_eq!(output, b"48656C6C6F2C20776F726C6421");

§Errors

The length of the output buffer must be twice the length of the input buffer.

We may relax this requirement in the future accepting a longer output buffer, but for now, we require it for simplicity and performance.