1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::{Buffer, BufferError};
impl Buffer {
/// Returns a new [`Buffer`] which is the result of concatenating all the slices in the list together.
///
/// If the list has no items, then a new zero-length [`Buffer`] is returned.
///
/// # Errors
/// - [`BufferError::AllocationError`] is returned if allocation fails.
pub fn concat(list: &[&[u8]]) -> Result<Self, BufferError> {
let mut length = 0;
for item in list {
length += item.len();
}
let mut buffer = Self::with_capacity(length)?;
for item in list {
buffer.extend_from_slice(item)?;
}
Ok(buffer)
}
}
#[cfg(test)]
mod tests {
use crate::{Buffer, BufferError};
#[test]
fn try_concat() -> Result<(), BufferError> {
let buffer = Buffer::concat(&[b"Hello,", b" ", b"world", b"!"])?;
assert_eq!(&buffer.slice(..), b"Hello, world!");
Ok(())
}
}