Skip to content

Vec::recycle #674

@llogiq

Description

@llogiq

Proposal

Problem statement

Re-using a Vec as buffer sometimes runs into lifetime issues. A recycle method that clears the Vec and essentially transmutes it into another Vec (using into_raw_parts & from_raw_parts) with an item type that has the same length and alignment can help here.

Motivating examples or use cases

A blog post at databento shows the following:

let mut buffer: Vec<&[u8]> = Vec::new();
for source in sources {
    let data: Vec<u8> = source.fetch_data(); // ← binding `data` declared here
    buffer.extend(data.split(splitter)); // ← borrowed value does not live long enough
    process_data(&buffer);
    buffer.clear();
}

They cite this as one of the reasons not to rewrite their code in Rust.

Solution sketch

In the closed RFC PR #2802, @golddranks suggests a Vec::recycle function (implementation is fairly trivial and omitted for brevity):

impl<T> Vec<T> {
    /// This clears out this `Vec` and recycles the allocation into a new `Vec`.
    /// The item type of the resulting `Vec` needs to have the same size and
    /// alignment as this `Vec`.
    ///
    /// # Panics
    /// This function will panic if the item type of the result does not have
    /// the same size or alignment (per [`std::mem::size_of`] and
    /// [`std::mem::align_of`], respectively) as the consumed type's items.
    ///
    /// # Examples
    /// ```
    ///# let inputs = ["a b c", "d e f"];
    ///# fn process(_: &[&str]) {}
    /// let mut storage: Vec<&[&str]> = Vec::new();
    /// for input in inputs {
    ///     let mut buffer = storage.recycle();
    ///     buffer.extend(input.split(" "));
    ///     process(&buffer);
    ///     storage = buffer.recycle();
    /// }
    /// ```
    pub fn recycle<U>(self) -> Vec<U> {
        ..
    }

Alternatives

An alternative shorter name would be "reuse". Both names work for me. I decided to suggest recycle because that was what @golddranks' RFC went with, and that had already some discussion.

Yes, this can be written using existing APIs. The wild linker blog even has a section on "Buffer reuse" that shares a fully safe implementation using an iterator-based hack. However, that hack is very much not obvious nor discoverable. And yes, there is a recycle_vec crate that implements the pattern.

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions