Skip to content

Commit

Permalink
Fix a tiny issue with BufWriter (#222)
Browse files Browse the repository at this point in the history
* fix: tiny fix

Signed-off-by: Anthony Griffon <[email protected]>

* test: add buf_writter test

Signed-off-by: Anthony Griffon <[email protected]>

---------

Signed-off-by: Anthony Griffon <[email protected]>
  • Loading branch information
Miaxos authored Jan 7, 2024
1 parent 4c3fed5 commit f684db1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion monoio/src/io/util/buf_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<W: AsyncWriteRent> AsyncWriteRent for BufWriter<W> {
let owned_buf = self.buf.as_mut().unwrap();
owned_buf
.as_mut_ptr()
.add(self.pos)
.add(self.cap)
.copy_from_nonoverlapping(buf.read_ptr(), amt);
}
self.cap += amt;
Expand Down
31 changes: 31 additions & 0 deletions monoio/tests/buf_writter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use monoio::{
io::{AsyncReadRent, AsyncWriteRent, BufReader, BufWriter, Splitable},
net::{TcpListener, TcpStream},
};

#[monoio::test_all]
async fn ensure_buf_writter_write_properly() {
let srv = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = srv.local_addr().unwrap();

monoio::spawn(async move {
let stream = TcpStream::connect(&addr).await.unwrap();
let (_, stream_write) = stream.into_split();

let mut buf_w = BufWriter::new(stream_write);
assert!(buf_w.write(&[b'1']).await.0.is_ok());
assert!(buf_w.write(&[b'2']).await.0.is_ok());
assert!(buf_w.write(&[b'3']).await.0.is_ok());
assert!(buf_w.flush().await.is_ok());
});

let (stream, _) = srv.accept().await.unwrap();
let (rd, _) = stream.into_split();

let s: Vec<u8> = Vec::with_capacity(16);
let mut buf = BufReader::new(rd);
let (size, s) = buf.read(s).await;

assert!(size.is_ok());
assert_eq!(s, b"123");
}

0 comments on commit f684db1

Please sign in to comment.