cyfs_util/util/
io_bridge.rs

1use std::pin::Pin;
2use async_std::io::Read as AsyncRead;
3use async_std::io::ReadExt;
4use std::io::prelude::*;
5
6struct ReadBridge {
7    reader: Pin<Box<dyn AsyncRead + Send + Unpin + 'static>>,
8}
9
10impl Read for ReadBridge {
11    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
12        let mut reader = self.reader.as_mut();
13        async_std::task::block_on(async {
14            reader.read(buf).await
15        })
16    }
17}
18
19/// Bridge from AsyncRead to Read.
20pub fn async_read_to_sync<S: AsyncRead + Unpin + Send + 'static>(
21    reader: S,
22) -> impl Read + Send + Unpin + 'static {
23    let reader = Box::pin(reader);
24    ReadBridge { reader }
25}