Skip to content

Commit a1316cd

Browse files
authored
io: impl std::io::BufRead on SyncIoBridge<T> (#5265)
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
1 parent 86ffabe commit a1316cd

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

tokio-util/src/io/sync_bridge.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use std::io::{Read, Write};
2-
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
1+
use std::io::{BufRead, Read, Write};
2+
use tokio::io::{
3+
AsyncBufRead, AsyncBufReadExt, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt,
4+
};
35

46
/// Use a [`tokio::io::AsyncRead`] synchronously as a [`std::io::Read`] or
57
/// a [`tokio::io::AsyncWrite`] as a [`std::io::Write`].
@@ -9,6 +11,28 @@ pub struct SyncIoBridge<T> {
911
rt: tokio::runtime::Handle,
1012
}
1113

14+
impl<T: AsyncBufRead + Unpin> BufRead for SyncIoBridge<T> {
15+
fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
16+
let src = &mut self.src;
17+
self.rt.block_on(AsyncBufReadExt::fill_buf(src))
18+
}
19+
20+
fn consume(&mut self, amt: usize) {
21+
let src = &mut self.src;
22+
AsyncBufReadExt::consume(src, amt)
23+
}
24+
25+
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> std::io::Result<usize> {
26+
let src = &mut self.src;
27+
self.rt
28+
.block_on(AsyncBufReadExt::read_until(src, byte, buf))
29+
}
30+
fn read_line(&mut self, buf: &mut String) -> std::io::Result<usize> {
31+
let src = &mut self.src;
32+
self.rt.block_on(AsyncBufReadExt::read_line(src, buf))
33+
}
34+
}
35+
1236
impl<T: AsyncRead + Unpin> Read for SyncIoBridge<T> {
1337
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
1438
let src = &mut self.src;

0 commit comments

Comments
 (0)