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

Should IoSliceMut::advance() be used when impl std::io::Read::read_vectored()? #128669

Open
zh-jq-b opened this issue Aug 5, 2024 · 1 comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-discussion Category: Discussion or questions that doesn't represent real issues. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@zh-jq-b
Copy link

zh-jq-b commented Aug 5, 2024

As of io_slice_advance is stable since 1.81, the following asserting may won't work if the users use IoSliceMut::advance in their impl of Read trait.

fn what_to_read<R>(reader: R)
where
    R: Read,
{
    let mut b1 = [0u8; 16];
    let mut b2 = [0u8; 32];

    let mut iov = [IoSliceMut::new(&mut b1), IoSliceMut::new(&mut b2)];
    let nr = reader.read_vectored(&mut iov).unwrap();

    if nr > 0 {
        assert_eq!(b1[0], iov[0][0]);
    }
    if nr >= b1.len() {
        assert_eq!(iov[0].len(), b1.len());
    }
}

It is unclear that whether the returned size of read_vectored is the real read data size, or the valid buffer size in the passed &mut [IoSliceMut<'_>].
Please clarify that so Read implementations can do the correct thing.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 5, 2024
@jieyouxu jieyouxu added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. C-discussion Category: Discussion or questions that doesn't represent real issues. A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` labels Aug 5, 2024
@zh-jq-b
Copy link
Author

zh-jq-b commented Aug 6, 2024

Use IoSliceMut::advance() inside read_vectored() can avoid copy when do HTTP chunks decode reading, or something encoded like that. But this will break code that always use the original buffer as the read address of the received data.

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-discussion Category: Discussion or questions that doesn't represent real issues. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants