Skip to content

Commit c7a6744

Browse files
committed
Add some more raw vec methods so AllocInit::Zeroed is always used
1 parent 313490a commit c7a6744

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

library/alloc/src/raw_vec.rs

+13
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ impl<T> RawVec<T, Global> {
111111
Self::with_capacity_zeroed_in(capacity, Global)
112112
}
113113

114+
/// Like `try_with_capacity`, but guarantees a successfully allocated buffer is zeroed.
115+
#[inline]
116+
pub fn try_with_capacity_zeroed(capacity: usize) -> Result<Self, TryReserveError> {
117+
Self::try_with_capacity_zeroed_in(capacity, Global)
118+
}
119+
114120
/// Reconstitutes a `RawVec` from a pointer and capacity.
115121
///
116122
/// # Safety
@@ -170,6 +176,13 @@ impl<T, A: Allocator> RawVec<T, A> {
170176
Self::allocate_in(capacity, AllocInit::Zeroed, alloc)
171177
}
172178

179+
/// Like `with_capacity_zeroed`, but parameterized over the choice
180+
/// of allocator for the returned `RawVec`.
181+
#[inline]
182+
pub fn try_with_capacity_zeroed_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
183+
Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc)
184+
}
185+
173186
/// Converts a `Box<[T]>` into a `RawVec<T>`.
174187
pub fn from_box(slice: Box<[T], A>) -> Self {
175188
unsafe {

0 commit comments

Comments
 (0)