Skip to content

Support extracting underlying filled buffer from BorrowedBuf #473

@SUPERCILEX

Description

@SUPERCILEX

Proposal

Problem statement

I'd like to be able to modify a MaybeUninit buffer and return the contents to the caller.

Motivating examples or use cases

Currently, if a function wishes to safely modified a MaybeUninit buffer passed in by reference by the caller, they cannot return the filled buffer to the caller.

Fixing this would let the following code compile:

pub fn direct_file_name(
    buf: &mut [MaybeUninit<u8>; DIRECT_FILE_NAME_LEN + 1],
    to: RingKind,
    index: u32,
) -> DirectFileNameToken<()> {
    let mut buf = BorrowedBuf::from(buf.as_mut_slice());
    write!(buf.unfilled(), "{:0>13}\0", composite_id(to, index)).unwrap();
    DirectFileNameToken(buf.filled_mut(), PhantomData) // Doesn't compile!
}

Solution sketch

Add two methods:

fn into_filled(self) -> &'data [u8]
fn into_filled_mut(self) -> &'data mut [u8]

Alternatives

Transmute to get lifetime extension or manually convert the MaybeUninits to u8s by using the filled length. Both of these solutions are very error prone and annoying to write.

Activity

Amanieu

Amanieu commented on Nov 5, 2024

@Amanieu
Member

We discussed this in the libs-api meeting and we're happy with these methods.

added
ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)
on Nov 5, 2024
joshtriplett

joshtriplett commented on Nov 5, 2024

@joshtriplett
Member

Quick correction: into_filled_mut has the wrong signature, it should take self.

joshtriplett

joshtriplett commented on Nov 5, 2024

@joshtriplett
Member

Looks like rust-lang/rust#132533 is correct and this was just a typo in the ACP.

SUPERCILEX

SUPERCILEX commented on Nov 5, 2024

@SUPERCILEX
Author

Oops, yeah sorry. Fixed.

added a commit that references this issue on Nov 25, 2024
813d3e7
added a commit that references this issue on Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)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

        Participants

        @joshtriplett@Amanieu@SUPERCILEX

        Issue actions

          Support extracting underlying filled buffer from BorrowedBuf · Issue #473 · rust-lang/libs-team