io::Write::write(_vectored)
and io::Read::read(_vectored)
do not guarantee processing the entire buffer, which might lead
to only partial writes/reads. This can result in bugs if not handled appropriately. Instead, use write_all
or read_exact
methods that ensure the whole buffer is processed, providing more reliable code especially in asynchronous contexts.
Code examples
Noncompliant code example
use std::io;
fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
w.write(b"foo")?; // Noncompliant: This might not write the entire buffer.
Ok(())
}
fn bar<R: io::Read>(r: &mut R, buffer: &mut [u8]) -> io::Result<()> {
r.read(buffer)?; // Noncompliant: This might not read the entire buffer.
Ok(())
}
Compliant solution
use std::io;
fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
w.write_all(b"foo")?; // Compliant: This writes the entire buffer.
Ok(())
}
fn bar<R: io::Read>(r: &mut R, buffer: &mut [u8]) -> io::Result<()> {
r.read_exact(buffer)?; // Compliant: This reads the entire buffer.
Ok(())
}