technocreatives / core2

The bare essentials of std::io for use in no_std. Alloc support is optional.
https://docs.rs/core2
Apache License 2.0
71 stars 23 forks source link

Implementation of Write for &mut [u8] does not return error when the buffer length is less than the data length #16

Closed dobromyslov closed 2 years ago

dobromyslov commented 2 years ago

See https://github.com/technocreatives/core2/blob/7bf2611bd9a52b438300c640e142ad5c1298b05b/src/io/impls.rs#L130

There is no check if the buffer is less than the data to be written.

Maybe it should check this case and return ErrorKind::WriteZero?https://github.com/technocreatives/core2/blob/7bf2611bd9a52b438300c640e142ad5c1298b05b/src/io/error.rs#L181

As a workaround I have to do cursor.write() and then compare the result with the data length like this:

let data = &[1_u8; 2];
match cursor.write(data) {
    Err(_) => Err(MqttFixedHeaderError::WriteError),
    Ok(size) => if size < data.len() {
        Err(MqttFixedHeaderError::WriteError)
    } else {
        Ok(size)
    }
}
dobromyslov commented 2 years ago

Or should I use write_all() instead?

dobromyslov commented 2 years ago

Definitely. That's my fault. I've read official docs https://doc.rust-lang.org/std/io/trait.Write.html#tymethod.write and understood that I need write_all().