Skip to content

Commit feadd06

Browse files
committed
rust: alloc: add Vec::try_with_capacity{,_in}() constructors
Add `Vec::try_with_capacity()` and `Vec::try_with_capacity_in()` as the fallible versions of `Vec::with_capacity()` and `Vec::with_capacity_in()`, respectively. The implementations follow the originals and use the previously added `RawVec::try_with_capacity_in()`. In turn, `Vec::try_with_capacity()` will be used to implement the `CString` type (which wraps a `Vec<u8>`) in a later patch. Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 51d3a25 commit feadd06

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

rust/alloc/raw_vec.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ impl<T, A: Allocator> RawVec<T, A> {
135135

136136
/// Like `try_with_capacity`, but parameterized over the choice of
137137
/// allocator for the returned `RawVec`.
138-
#[allow(dead_code)]
139138
#[inline]
140139
pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
141140
Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc)

rust/alloc/vec/mod.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,48 @@ impl<T> Vec<T> {
472472
Self::with_capacity_in(capacity, Global)
473473
}
474474

475+
/// Tries to construct a new, empty `Vec<T>` with the specified capacity.
476+
///
477+
/// The vector will be able to hold exactly `capacity` elements without
478+
/// reallocating. If `capacity` is 0, the vector will not allocate.
479+
///
480+
/// It is important to note that although the returned vector has the
481+
/// *capacity* specified, the vector will have a zero *length*. For an
482+
/// explanation of the difference between length and capacity, see
483+
/// *[Capacity and reallocation]*.
484+
///
485+
/// [Capacity and reallocation]: #capacity-and-reallocation
486+
///
487+
/// # Examples
488+
///
489+
/// ```
490+
/// let mut vec = Vec::try_with_capacity(10).unwrap();
491+
///
492+
/// // The vector contains no items, even though it has capacity for more
493+
/// assert_eq!(vec.len(), 0);
494+
/// assert_eq!(vec.capacity(), 10);
495+
///
496+
/// // These are all done without reallocating...
497+
/// for i in 0..10 {
498+
/// vec.push(i);
499+
/// }
500+
/// assert_eq!(vec.len(), 10);
501+
/// assert_eq!(vec.capacity(), 10);
502+
///
503+
/// // ...but this may make the vector reallocate
504+
/// vec.push(11);
505+
/// assert_eq!(vec.len(), 11);
506+
/// assert!(vec.capacity() >= 11);
507+
///
508+
/// let mut result = Vec::try_with_capacity(usize::MAX);
509+
/// assert!(result.is_err());
510+
/// ```
511+
#[inline]
512+
#[stable(feature = "kernel", since = "1.0.0")]
513+
pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
514+
Self::try_with_capacity_in(capacity, Global)
515+
}
516+
475517
/// Creates a `Vec<T>` directly from the raw components of another vector.
476518
///
477519
/// # Safety
@@ -617,6 +659,53 @@ impl<T, A: Allocator> Vec<T, A> {
617659
Vec { buf: RawVec::with_capacity_in(capacity, alloc), len: 0 }
618660
}
619661

662+
/// Tries to construct a new, empty `Vec<T, A>` with the specified capacity
663+
/// with the provided allocator.
664+
///
665+
/// The vector will be able to hold exactly `capacity` elements without
666+
/// reallocating. If `capacity` is 0, the vector will not allocate.
667+
///
668+
/// It is important to note that although the returned vector has the
669+
/// *capacity* specified, the vector will have a zero *length*. For an
670+
/// explanation of the difference between length and capacity, see
671+
/// *[Capacity and reallocation]*.
672+
///
673+
/// [Capacity and reallocation]: #capacity-and-reallocation
674+
///
675+
/// # Examples
676+
///
677+
/// ```
678+
/// #![feature(allocator_api)]
679+
///
680+
/// use std::alloc::System;
681+
///
682+
/// let mut vec = Vec::try_with_capacity_in(10, System).unwrap();
683+
///
684+
/// // The vector contains no items, even though it has capacity for more
685+
/// assert_eq!(vec.len(), 0);
686+
/// assert_eq!(vec.capacity(), 10);
687+
///
688+
/// // These are all done without reallocating...
689+
/// for i in 0..10 {
690+
/// vec.push(i);
691+
/// }
692+
/// assert_eq!(vec.len(), 10);
693+
/// assert_eq!(vec.capacity(), 10);
694+
///
695+
/// // ...but this may make the vector reallocate
696+
/// vec.push(11);
697+
/// assert_eq!(vec.len(), 11);
698+
/// assert!(vec.capacity() >= 11);
699+
///
700+
/// let mut result = Vec::try_with_capacity_in(usize::MAX, System);
701+
/// assert!(result.is_err());
702+
/// ```
703+
#[inline]
704+
#[stable(feature = "kernel", since = "1.0.0")]
705+
pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
706+
Ok(Vec { buf: RawVec::try_with_capacity_in(capacity, alloc)?, len: 0 })
707+
}
708+
620709
/// Creates a `Vec<T, A>` directly from the raw components of another vector.
621710
///
622711
/// # Safety

0 commit comments

Comments
 (0)