Skip to content

Commit 02ef6b2

Browse files
ararogBerrysoft
andauthored
feat(net): allow peek TcpStream (#974)
* feat: allow peek from TcpStream * test: updated unit test for TcpStream peek. * test: updated unit test for TcpStream peek * docs: make method description succint * fix: make implementation simplier * docs: fix platform specific info Co-authored-by: Yuyi Wang <Strawberry_Str@hotmail.com> --------- Co-authored-by: Yuyi Wang <Strawberry_Str@hotmail.com>
1 parent 9fa4e89 commit 02ef6b2

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

compio-net/src/tcp.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,14 @@ impl TcpStream {
447447
)
448448
.await
449449
}
450+
451+
/// Peeks at data from this socket without consuming it
452+
///
453+
/// ## Platform-specific
454+
/// * Windows: this method may work, but is not ensured by Microsoft.
455+
pub async fn peek<T: IoBufMut>(&self, buffer: T) -> BufResult<usize, T> {
456+
self.inner.recv(buffer, RecvFlags::PEEK).await
457+
}
450458
}
451459

452460
impl AsyncRead for TcpStream {

compio-net/tests/incoming.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ async fn incoming_tcp() {
2727

2828
for i in 0..2 {
2929
let mut client = TcpStream::connect(&addr).await.unwrap();
30+
let (_, text) = client.peek([0u8; 8]).await.unwrap();
31+
assert_eq!(text, format!("Hello, {}", i).as_bytes());
3032
let (_, text) = client.read_exact([0u8; 8]).await.unwrap();
3133
assert_eq!(text, format!("Hello, {}", i).as_bytes());
3234
client.write_all(text).await.unwrap();

0 commit comments

Comments
 (0)