Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support extracting underlying filled buffer from BorrowedBuf #473

Closed
SUPERCILEX opened this issue Nov 2, 2024 · 4 comments
Closed

Support extracting underlying filled buffer from BorrowedBuf #473

SUPERCILEX opened this issue Nov 2, 2024 · 4 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@SUPERCILEX
Copy link

SUPERCILEX commented Nov 2, 2024

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.

@Amanieu
Copy link
Member

Amanieu commented Nov 5, 2024

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

@Amanieu Amanieu closed this as completed Nov 5, 2024
@Amanieu Amanieu added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Nov 5, 2024
@joshtriplett
Copy link
Member

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

@joshtriplett
Copy link
Member

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

@SUPERCILEX
Copy link
Author

SUPERCILEX commented Nov 5, 2024

Oops, yeah sorry. Fixed.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 25, 2024
Add BorrowedBuf::into_filled{,_mut} methods to allow returning buffer with original lifetime

See rust-lang/libs-team#473 and tracking issue rust-lang#117693.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 25, 2024
Rollup merge of rust-lang#132533 - SUPERCILEX:patch-4, r=Mark-Simulacrum

Add BorrowedBuf::into_filled{,_mut} methods to allow returning buffer with original lifetime

See rust-lang/libs-team#473 and tracking issue rust-lang#117693.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants