rust-lang / rust-lang/rust

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

Open
#128,669 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-docs A-io C-discussion T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the documented contracts for Read::read_vectored and IoSliceMut::advance, then compare them with the example in this issue. Clarify whether the returned count is the number of bytes read and how the passed slices may be modified, so implementations and callers have an unambiguous contract.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
operating-systems
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.