encode_into

Function encode_into 

Source
pub fn encode_into(
    dst: &mut [u8],
    src: &[u8],
    algo: EncodeAlgo,
) -> Result<u32, Error>
Expand description

Performs in-place encoding of the source slice using the specified encoding algorithm.

The function calls into a potentially unsafe C binding to perform the encoding, and it may return an error if the encoding process encounters any issues.

§Arguments

  • dst: A mutable byte slice where the encoded data will be written. This slice should be pre-allocated with sufficient capacity.

  • src: A byte slice that contains the data to be encoded.

  • algo: The encoding algorithm to be used.

§Returns

  • Ok(u32): The length of the encoded data written to dst.

  • Err(Error): An error encountered during the encoding process.

§Safety

This function uses unsafe code to call C bindings. Ensure that dst has enough capacity to hold the encoded data to prevent memory corruption or undefined behavior.

§Examples

let src = b"some data to encode";
let algorithm = szs::EncodeAlgo::MK8; // Mario Kart 8

// Determine the upper bound for the encoded data size
let max_encoded_size = szs::encoded_upper_bound(src.len() as u32) as usize;

let mut dst: Vec<u8> = vec![0; max_encoded_size];

match szs::encode_into(&mut dst, src, algorithm) {
    Ok(encoded_len) => {
        println!("Encoded {} bytes", encoded_len);
        dst.truncate(encoded_len as usize);  // Adjust the vector to the actual encoded length
    },
    Err(err) => println!("Error: {}", err),
}