You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rsh/src/bin.rs

38 lines
941 B

//! Binary / byte maniuplation
use bytes::BufMut;
/// Concatenate an iterator of byte slices into a buffer.
///
/// # Returns
/// The number of bytes written
/// # Panics
/// If the buffer cannot hold all the slices
pub fn collect_slices_into<B: BufMut + ?Sized, I, T>(into: &mut B, from: I) -> usize
where I: IntoIterator<Item=T>,
T: AsRef<[u8]>
{
let mut done =0;
for slice in from.into_iter()
{
let s = slice.as_ref();
into.put_slice(s);
done+=s.len();
}
done
}
/// Collect an iterator of byte slices into a new exact-size buffer.
///
/// # Returns
/// The number of bytes written, and the new array
///
/// # Panics
/// If the total bytes in all slices exceeds `SIZE`.
pub fn collect_slices_exact<T, I, const SIZE: usize>(from: I) -> (usize, [u8; SIZE])
where I: IntoIterator<Item=T>,
T: AsRef<[u8]>
{
let mut output = [0u8; SIZE];
(collect_slices_into(&mut &mut output[..], from), output)
}