Toralf Wittner 5a465b7f50
Fix multiaddr::util::BytesWriter. (#1068)
The current implementation does not properly reserve enough space for
`Write::write_all` to succeed. The property test has been improved to
trigger that issue.
2019-04-18 14:28:47 +02:00

22 lines
582 B
Rust

use bytes::BytesMut;
/// An [`io::Write`] impl for [`BytesMut`].
///
/// In contrast to [`bytes::buf::Writer`] this [`io::Write] implementation
/// transparently reserves enough space for [`io::Write::write_all`] to
/// succeed, i.e. it does not require upfront reservation of space.
pub(crate) struct BytesWriter(pub(crate) BytesMut);
impl std::io::Write for BytesWriter {
fn write(&mut self, src: &[u8]) -> std::io::Result<usize> {
self.0.extend_from_slice(src);
Ok(src.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}