Skip to content

Tracking Issue for Vec::recycle #148227

Open

Description

@llogiq

Feature gate: #![feature(vec_recycle)]

This is a tracking issue for the Vec::recycle method, which allows reusing a Vec in a loop even if the lifetime unification would otherwise disallow it.

Public API

// in alloc::vec
#![feature(transmutability)]
#![feature(trait_alias)]

use std::mem::{Assume, MaybeUninit, TransmuteFrom};

/// Implemented if the size and alignment `T` and `Self` are both equal.
trait Recyclable<T> =
    for<'a> TransmuteFrom<&'a MaybeUninit<T>, { Assume::SAFETY }> 
    where 
        T: for<'a> TransmuteFrom<&'a MaybeUninit<Self>, { Assume::SAFETY }>;

impl<T> Vec<T> {
    #[feature(vec_recycle)]
    pub fn recycle<U>(self) -> Vec<U> where U: Recyclable<T>;
}

Steps / History

Unresolved Questions

  • Make sure that we don't unintentionally create reliance on this as workaround for lack of stable TransmuteFrom, when this stabilizes.

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    on Oct 28, 2025
  2. GrigorenkoPV commented on Oct 31, 2025

    @GrigorenkoPV
    Contributor

    @rustbot claim
    (implementation)

  3. GrigorenkoPV commented on Oct 31, 2025

    @GrigorenkoPV
    Contributor

    Unfortunately, there are some issues with the TransmuteFrom approach.

    #![feature(transmutability)]
    
    use core::mem::{Assume, TransmuteFrom};
    
    const fn check<From, To>()
    where
        To: TransmuteFrom<From, { Assume::SAFETY }>,
    {
    }
    
    fn main() {
        const {
            assert!(align_of::<&[u8]>() == align_of::<&[&[u8]]>());
        }
        check::<&[&[u8]], &[u8]>();
        check::<&[u8], &[&[u8]]>();
    }

    When uncommenting the two lines, the latest nightly (2025-10-30) gives the following errors:

    error[E0277]: `[&[u8]]` cannot be safely transmuted into `[u8]`
      --> src/main.rs:15:23
       |
    15 |     check::<&[&[u8]], &[u8]>();
       |                       ^^^^^ analyzing the transmutability of `[&[u8]]` is not yet supported
       |
    
    error[E0277]: `&[u8]` cannot be safely transmuted into `&[&[u8]]`
      --> src/main.rs:16:20
       |
    16 |     check::<&[u8], &[&[u8]]>();
       |                    ^^^^^^^^ the minimum alignment of `&[u8]` (1) should be greater than that of `&[&[u8]]` (8)
       |
    

    So in one direction it's unimplemented and in the other direction it's seemingly wrong? Maybe i don't understand what it means by "minimum alignment", but the align_of gives the same values for both types.

  4. jswrenn commented on Nov 2, 2025

    @jswrenn
    Member

    @GrigorenkoPV Feel free to tag me in any TransmuteFrom issues! The second error is downstream of the first one, I think. We don't yet support analyzing the transmutability of slices, because we haven't decided on how to incorporate the fact we can't analyze the metadata component statically. (Relatedly, we also don't support analyzing trait objects yet.) TransmuteFrom will let you proof-of-concept a trait bound for 'easy' types, but we'll need to be a little more sophisticated for more unusual types.

    Use the TransmuteFrom-based Recyclable for now, and I'll see this week if I can whip up an intrinsic Recyclable trait that skips the validity checks that are unnecessary for this use-case.

  5. added 2 commits that reference this issue on Nov 15, 2025
  6. added a commit that references this issue on Nov 16, 2025
  7. added a commit that references this issue on Nov 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions