Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: alloc: add `RawVec::try_with_capacity_in()` constructor

Add the `RawVec::try_with_capacity_in()` constructor as the fallible
version of `RawVec::with_capacity_in()`.

The implementation follows the original.

The infallible constructor is implemented in terms of the private
`RawVec::allocate_in()` constructor, thus also add the private
`RawVec::try_allocate_in()` constructor following the other.

It will be used to implement `Vec::try_with_capacity{,_in}()` in
the next patch.

Reviewed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

+33 -1
+33 -1
rust/alloc/raw_vec.rs
··· 20 20 #[cfg(test)] 21 21 mod tests; 22 22 23 - #[cfg(not(no_global_oom_handling))] 24 23 enum AllocInit { 25 24 /// The contents of the new memory are uninitialized. 26 25 Uninitialized, 27 26 /// The new memory is guaranteed to be zeroed. 27 + #[allow(dead_code)] 28 28 Zeroed, 29 29 } 30 30 ··· 133 133 Self::allocate_in(capacity, AllocInit::Uninitialized, alloc) 134 134 } 135 135 136 + /// Like `try_with_capacity`, but parameterized over the choice of 137 + /// allocator for the returned `RawVec`. 138 + #[allow(dead_code)] 139 + #[inline] 140 + pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> { 141 + Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc) 142 + } 143 + 136 144 /// Like `with_capacity_zeroed`, but parameterized over the choice 137 145 /// of allocator for the returned `RawVec`. 138 146 #[cfg(not(no_global_oom_handling))] ··· 209 201 alloc, 210 202 } 211 203 } 204 + } 205 + 206 + fn try_allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Result<Self, TryReserveError> { 207 + // Don't allocate here because `Drop` will not deallocate when `capacity` is 0. 208 + if mem::size_of::<T>() == 0 || capacity == 0 { 209 + return Ok(Self::new_in(alloc)); 210 + } 211 + 212 + let layout = Layout::array::<T>(capacity).map_err(|_| CapacityOverflow)?; 213 + alloc_guard(layout.size())?; 214 + let result = match init { 215 + AllocInit::Uninitialized => alloc.allocate(layout), 216 + AllocInit::Zeroed => alloc.allocate_zeroed(layout), 217 + }; 218 + let ptr = result.map_err(|_| AllocError { layout, non_exhaustive: () })?; 219 + 220 + // Allocators currently return a `NonNull<[u8]>` whose length 221 + // matches the size requested. If that ever changes, the capacity 222 + // here should change to `ptr.len() / mem::size_of::<T>()`. 223 + Ok(Self { 224 + ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) }, 225 + cap: capacity, 226 + alloc, 227 + }) 212 228 } 213 229 214 230 /// Reconstitutes a `RawVec` from a pointer, capacity, and allocator.