Skip to content

Commit 329bf32

Browse files
committed
Use Box<[u8]> for buffer instead of Vec<u8>
1 parent ed621f4 commit 329bf32

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

src/lib.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ pub trait CompactEncoding<Decode: ?Sized = Self> {
202202
/// foo.encode(&mut buff)?;
203203
/// # Ok::<(), Box<dyn std::error::Error>>(())
204204
/// ```
205-
fn to_encoded_bytes(&self) -> Result<Vec<u8>, EncodingError> {
206-
let mut buff = vec![0; self.encoded_size()?];
205+
fn to_encoded_bytes(&self) -> Result<Box<[u8]>, EncodingError> {
206+
let mut buff = self.create_buffer()?;
207207
self.encode(&mut buff)?;
208208
Ok(buff)
209209
}
@@ -216,8 +216,8 @@ pub trait CompactEncoding<Decode: ?Sized = Self> {
216216
/// vec![0; foo.encoded_size()?];
217217
/// # Ok::<(), Box<dyn std::error::Error>>(())
218218
/// ```
219-
fn create_buffer(&self) -> Result<Vec<u8>, EncodingError> {
220-
Ok(vec![0; self.encoded_size()?])
219+
fn create_buffer(&self) -> Result<Box<[u8]>, EncodingError> {
220+
Ok(vec![0; self.encoded_size()?].into_boxed_slice())
221221
}
222222

223223
/// Like [`CompactEncoding::encode`] but also return the number of bytes encoded.
@@ -440,11 +440,6 @@ macro_rules! map_first {
440440
}};
441441
}
442442

443-
/// Returns a zerod `Box<[u8]>` where the slice is of length `encoded_size`.
444-
pub fn fixed_buffer_from_encoded_size(encoded_size: usize) -> Box<[u8]> {
445-
vec![0; encoded_size].into_boxed_slice()
446-
}
447-
448443
/// Split a slice in two at `mid`. Returns encoding error when `mid` is out of bounds.
449444
pub fn get_slices_checked(buffer: &[u8], mid: usize) -> Result<(&[u8], &[u8]), EncodingError> {
450445
buffer.split_at_checked(mid).ok_or_else(|| {

0 commit comments

Comments
 (0)