Function bao::encode::encode_in_place

source ·
pub fn encode_in_place(buf: &mut [u8], content_len: usize) -> Hash
Expand description

Encode the first content_len bytes from the input buffer in the combined mode, overwriting the input buffer. buf.len() must be exactly encoded_size(content_len as u64).

If the std feature is enabled, as it is by default, this will use multiple threads via Rayon. This function is slower than encode, however, because only the hashing can be parallelized; copying the input bytes around has to be done on a single thread.

Panics

Panics if the buffer is the wrong length.

Example

let input = b"some bytes";
let encoded_size = bao::encode::encoded_size(input.len() as u64);
assert!(encoded_size <= usize::max_value() as u128);
let mut buffer = input.to_vec();
buffer.resize(encoded_size as usize, 0);
bao::encode::encode_in_place(&mut buffer, input.len());