aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Donough Liu <ldm2993593805@163.com> 2021-11-24 17:20:21 +0800
committerGravatar GitHub <noreply@github.com> 2021-11-24 10:20:21 +0100
commit68afb40df9ea2e61a93d0e26f6f8a9f231979452 (patch)
tree5005aaad0d03bc553db2a3c78e44f11b90261cea /src
parentd946ef2e91651a4d73a04b48715ecf0cde6db75d (diff)
downloadbytes-68afb40df9ea2e61a93d0e26f6f8a9f231979452.tar.gz
bytes-68afb40df9ea2e61a93d0e26f6f8a9f231979452.tar.zst
bytes-68afb40df9ea2e61a93d0e26f6f8a9f231979452.zip
Add `BytesMut::zeroed` (#517)
Diffstat (limited to 'src')
-rw-r--r--src/bytes_mut.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs
index 147484d..f39b73e 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