bytes icon indicating copy to clipboard operation
bytes copied to clipboard

Returned Writer might fail

Open vorner opened this issue 3 years ago • 0 comments

The documentation of the BufMut::writer claims that the returned writer will never fail:

This function returns a new value which implements Write by adapting the Write trait functions to the BufMut trait functions. Given that BufMut operations are infallible, none of the Write functions will return with Err.

This is, however, not true, it may error on not enough capacity in the buffer:

use std::io::Write;
use bytes::BufMut;

fn main() {
    let mut buffer = [0; 10];
    let mut w = buffer.writer();
    w.write_all(b"Hello world").unwrap();
}

I believe a similar can be achieved with the reader's read_exact and its documentation makes a similar claim.

To be clear, I think the actual behaviour is expected and the only reasonable way it could act. But maybe the documentation should be adjusted to claim something little weaker (like, returning errors related to end of buffer only)?

vorner avatar Jan 16 '21 16:01 vorner