[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <F461629C-97D4-4A49-885E-BEE4AEB5C4FB@nyantec.com>
Date: Tue, 06 Dec 2022 13:55:56 +0100
From: Finn Behrens <fin@...ntec.com>
To: ojeda@...nel.org
Cc: Wedson Almeida Filho <wedsonaf@...il.com>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
patches@...ts.linux.dev
Subject: Re: [PATCH v2 13/28] rust: alloc: add
`Vec::try_with_capacity{,_in}()` constructors
On 2 Dec 2022, at 17:14, ojeda@...nel.org wrote:
> From: Miguel Ojeda <ojeda@...nel.org>
>
> 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@...yguo.net>
> Signed-off-by: Miguel Ojeda <ojeda@...nel.org>
Reviewed-by: Finn Behrens <fin@...ntec.com>
Regards,
Finn
> ---
> rust/alloc/raw_vec.rs | 1 -
> rust/alloc/vec/mod.rs | 89 +++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 89 insertions(+), 1 deletion(-)
>
> diff --git a/rust/alloc/raw_vec.rs b/rust/alloc/raw_vec.rs
> index c342f3843972..eb77db5def55 100644
> --- a/rust/alloc/raw_vec.rs
> +++ b/rust/alloc/raw_vec.rs
> @@ -135,7 +135,6 @@ impl<T, A: Allocator> RawVec<T, A> {
>
> /// Like `try_with_capacity`, but parameterized over the choice of
> /// allocator for the returned `RawVec`.
> - #[allow(dead_code)]
> #[inline]
> pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
> Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc)
> diff --git a/rust/alloc/vec/mod.rs b/rust/alloc/vec/mod.rs
> index 540787804cc2..8ac6c1e3b2a8 100644
> --- a/rust/alloc/vec/mod.rs
> +++ b/rust/alloc/vec/mod.rs
> @@ -472,6 +472,48 @@ impl<T> Vec<T> {
> Self::with_capacity_in(capacity, Global)
> }
>
> + /// Tries to construct a new, empty `Vec<T>` with the specified capacity.
> + ///
> + /// The vector will be able to hold exactly `capacity` elements without
> + /// reallocating. If `capacity` is 0, the vector will not allocate.
> + ///
> + /// It is important to note that although the returned vector has the
> + /// *capacity* specified, the vector will have a zero *length*. For an
> + /// explanation of the difference between length and capacity, see
> + /// *[Capacity and reallocation]*.
> + ///
> + /// [Capacity and reallocation]: #capacity-and-reallocation
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// let mut vec = Vec::try_with_capacity(10).unwrap();
> + ///
> + /// // The vector contains no items, even though it has capacity for more
> + /// assert_eq!(vec.len(), 0);
> + /// assert_eq!(vec.capacity(), 10);
> + ///
> + /// // These are all done without reallocating...
> + /// for i in 0..10 {
> + /// vec.push(i);
> + /// }
> + /// assert_eq!(vec.len(), 10);
> + /// assert_eq!(vec.capacity(), 10);
> + ///
> + /// // ...but this may make the vector reallocate
> + /// vec.push(11);
> + /// assert_eq!(vec.len(), 11);
> + /// assert!(vec.capacity() >= 11);
> + ///
> + /// let mut result = Vec::try_with_capacity(usize::MAX);
> + /// assert!(result.is_err());
> + /// ```
> + #[inline]
> + #[stable(feature = "kernel", since = "1.0.0")]
> + pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
> + Self::try_with_capacity_in(capacity, Global)
> + }
> +
> /// Creates a `Vec<T>` directly from the raw components of another vector.
> ///
> /// # Safety
> @@ -617,6 +659,53 @@ impl<T, A: Allocator> Vec<T, A> {
> Vec { buf: RawVec::with_capacity_in(capacity, alloc), len: 0 }
> }
>
> + /// Tries to construct a new, empty `Vec<T, A>` with the specified capacity
> + /// with the provided allocator.
> + ///
> + /// The vector will be able to hold exactly `capacity` elements without
> + /// reallocating. If `capacity` is 0, the vector will not allocate.
> + ///
> + /// It is important to note that although the returned vector has the
> + /// *capacity* specified, the vector will have a zero *length*. For an
> + /// explanation of the difference between length and capacity, see
> + /// *[Capacity and reallocation]*.
> + ///
> + /// [Capacity and reallocation]: #capacity-and-reallocation
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// #![feature(allocator_api)]
> + ///
> + /// use std::alloc::System;
> + ///
> + /// let mut vec = Vec::try_with_capacity_in(10, System).unwrap();
> + ///
> + /// // The vector contains no items, even though it has capacity for more
> + /// assert_eq!(vec.len(), 0);
> + /// assert_eq!(vec.capacity(), 10);
> + ///
> + /// // These are all done without reallocating...
> + /// for i in 0..10 {
> + /// vec.push(i);
> + /// }
> + /// assert_eq!(vec.len(), 10);
> + /// assert_eq!(vec.capacity(), 10);
> + ///
> + /// // ...but this may make the vector reallocate
> + /// vec.push(11);
> + /// assert_eq!(vec.len(), 11);
> + /// assert!(vec.capacity() >= 11);
> + ///
> + /// let mut result = Vec::try_with_capacity_in(usize::MAX, System);
> + /// assert!(result.is_err());
> + /// ```
> + #[inline]
> + #[stable(feature = "kernel", since = "1.0.0")]
> + pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
> + Ok(Vec { buf: RawVec::try_with_capacity_in(capacity, alloc)?, len: 0 })
> + }
> +
> /// Creates a `Vec<T, A>` directly from the raw components of another vector.
> ///
> /// # Safety
> --
> 2.38.1
Powered by blists - more mailing lists