Skip to content

Commit 68afb40

Browse files
authored
Add BytesMut::zeroed (#517)
1 parent d946ef2 commit 68afb40

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/bytes_mut.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use alloc::{
88
borrow::{Borrow, BorrowMut},
99
boxed::Box,
1010
string::String,
11+
vec,
1112
vec::Vec,
1213
};
1314

@@ -258,6 +259,22 @@ impl BytesMut {
258259
}
259260
}
260261

262+
/// Creates a new `BytesMut`, which is initialized with zero.
263+
///
264+
/// # Examples
265+
///
266+
/// ```
267+
/// use bytes::BytesMut;
268+
///
269+
/// let zeros = BytesMut::zeroed(42);
270+
///
271+
/// assert_eq!(zeros.len(), 42);
272+
/// zeros.into_iter().for_each(|x| assert_eq!(x, 0));
273+
/// ```
274+
pub fn zeroed(len: usize) -> BytesMut {
275+
BytesMut::from_vec(vec![0; len])
276+
}
277+
261278
/// Splits the bytes into two at the given index.
262279
///
263280
/// Afterwards `self` contains elements `[0, at)`, and the returned

0 commit comments

Comments
 (0)