Skip to content

ACP: Positioned reads into uninitialized buffers (FileExt::read_buf_at, ...) #581

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

Closed
niklasf opened this issue May 1, 2025 · 2 comments
Closed
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

@niklasf
Copy link

niklasf commented May 1, 2025

Proposal

Problem statement

The current APIs for positioned reads

take a &mut [u8] output buffer that must be initialized even though it will be immediately overwritten. Initialization may add significant overhead.

Motivating examples or use cases

Positioned reads from fast storage or even in-memory files, avoiding memory maps for more control over error handling.

Solution sketch

RFC 2930 outlines a solution to this same problem for normal unpositioned reads, introducing BorrowedCursor<'_>, an API for buffers to be incrementally filled and initialized.

Analogously to the proposed std::io::Read::read_buf which is equivalent to read, add trait methods:

// std::os::unix::fs
trait FileExt {
    /// Equivalent to read_at()
    fn read_buf_at(&self, buf: BorrowedCursor<'_>, offset: u64) -> io::Result<()>;

    /// Equivalent to read_exact_at()
    fn read_buf_exact_at(&self, buf: BorrowedCursor<'_>, offset: u64) -> io::Result<()>;
}

// std::os::windows::fs
trait FileExt {
    // Equivalent to seek_read()
    fn seek_read_buf(&self, buf: BorrowedCursor<'_>, offset: u64) -> io::Result<()>;
}

Alternatives

Alternative APIs for handling uninitialized buffers have been (and are still being) considered under the unstableread_buf and core_io_borrowed_buf features. However, in any case, positioned reads should likely use the same solution for consistency.

Links and related work

@niklasf niklasf added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels May 1, 2025
@niklasf niklasf changed the title ACP: Support positioned reads into uninitialzed buffers (FileExt::read_buf_at, ...) ACP: Positioned reads into uninitialzed buffers (FileExt::read_buf_at, ...) May 1, 2025
@niklasf niklasf changed the title ACP: Positioned reads into uninitialzed buffers (FileExt::read_buf_at, ...) ACP: Positioned reads into uninitialized buffers (FileExt::read_buf_at, ...) May 1, 2025
@Amanieu
Copy link
Member

Amanieu commented May 6, 2025

We discussed this in the @rust-lang/libs-api meeting and are happy to accept it.

Please open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature. You can close this ACP once the tracking issue has been created.

@Amanieu Amanieu added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label May 6, 2025
@niklasf
Copy link
Author

niklasf commented May 7, 2025

Thanks! Tracking issue: rust-lang/rust#140771

@niklasf niklasf closed this as completed May 7, 2025
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

2 participants