From 36f7278b1c420098b5c991a0d814c723ce01efa6 Mon Sep 17 00:00:00 2001 From: liudingming Date: Mon, 22 Nov 2021 02:20:39 +0800 Subject: [PATCH] Add `BytesMut::zeroed` --- src/bytes_mut.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs index 147484d53..f39b73ef8 100644 --- a/src/bytes_mut.rs +++ b/src/bytes_mut.rs @@ -8,6 +8,7 @@ use alloc::{ borrow::{Borrow, BorrowMut}, boxed::Box, string::String, + vec, vec::Vec, }; @@ -258,6 +259,22 @@ impl BytesMut { } } + /// Creates a new `BytesMut`, which is initialized with zero. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let zeros = BytesMut::zeroed(42); + /// + /// assert_eq!(zeros.len(), 42); + /// zeros.into_iter().for_each(|x| assert_eq!(x, 0)); + /// ``` + pub fn zeroed(len: usize) -> BytesMut { + BytesMut::from_vec(vec![0; len]) + } + /// Splits the bytes into two at the given index. /// /// Afterwards `self` contains elements `[0, at)`, and the returned