Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a tiny issue with BufWriter #222

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
Loading