From: Alice Ryhl <aliceryhl@google.com>
To: David Rheinsberg <david@readahead.eu>
Cc: rust-for-linux@vger.kernel.org,
"Lorenzo Stoakes" <lorenzo.stoakes@oracle.com>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
"Uladzislau Rezki" <urezki@gmail.com>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust/alloc: add Vec::into_boxed_slice()
Date: Thu, 26 Mar 2026 10:33:04 +0000 [thread overview]
Message-ID: <acULYP6ejIQAIdX-@google.com> (raw)
In-Reply-To: <20260326095621.846840-1-david@readahead.eu>
On Thu, Mar 26, 2026 at 10:56:20AM +0100, David Rheinsberg wrote:
> Add `Vec::into_boxed_slice()` similar to
> `std::vec::Vec::into_boxed_slice()` [1].
>
> There is currently no way to easily consume the allocation of a vector.
> However, it is very convenient to use `Vec` to initialize a dynamically
> sized array and then "seal" it, so it can be passed along as a Box:
>
> fn create_from(src: &[T]) -> Result<KBox<[U]>, AllocError> {
> let v = Vec::with_capacity(n, GFP_KERNEL)?;
>
> for i in src {
> v.push(foo(i)?, GFP_KERNEL)?;
> }
>
> Ok(v.into_boxed_slice())
> }
>
> A valid alternative is to use `Box::new_uninit()` rather than
> `Vec::with_capacity()`, and eventually convert the box via
> `Box::assume_init()`. This works but needlessly requires unsafe code,
> awkward drop handling, etc. Using `Vec` is the much simpler solution.
>
> [1] https://doc.rust-lang.org/std/vec/struct.Vec.html#method.into_boxed_slice
>
> Signed-off-by: David Rheinsberg <david@readahead.eu>
> + /// Excess capacity is retained in the allocation, but lost until the box
> + /// is dropped.
We currently say that you should provide the right length when freeing
an allocation, but this is going to violate that. You should probably
invoke realloc here, like stdlib does.
Alice
next prev parent reply other threads:[~2026-03-26 10:33 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-26 9:56 [PATCH] rust/alloc: add Vec::into_boxed_slice() David Rheinsberg
2026-03-26 10:33 ` Alice Ryhl [this message]
2026-03-26 10:45 ` David Rheinsberg
2026-03-26 10:55 ` Danilo Krummrich
2026-03-26 10:53 ` Danilo Krummrich
2026-03-26 12:20 ` David Rheinsberg
2026-03-26 12:40 ` Danilo Krummrich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=acULYP6ejIQAIdX-@google.com \
--to=aliceryhl@google.com \
--cc=Liam.Howlett@oracle.com \
--cc=a.hindborg@kernel.org \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=david@readahead.eu \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=lossin@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=urezki@gmail.com \
--cc=vbabka@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox