cyfs_util/util/
read_seek.rs

1use async_std::io::{Write, Read, Seek};
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5pub trait SyncWriteWithSeek: std::io::Seek + std::io::Write {}
6
7pub trait AsyncWriteWithSeek: Write + Seek {}
8
9pub trait SyncReadWithSeek: std::io::Seek + std::io::Read {}
10
11pub trait AsyncReadWithSeek: Read + Seek {}
12
13impl AsyncReadWithSeek for async_std::fs::File {}
14impl<T> AsyncReadWithSeek for async_std::io::Cursor<T> where T: AsRef<[u8]> + Unpin {}
15
16// TODO just for trait upcasting
17pub struct AsyncReadWithSeekAdapter {
18    reader: Box<dyn AsyncReadWithSeek + Unpin + Send + Sync + 'static>,
19}
20
21impl AsyncReadWithSeekAdapter {
22    pub fn new(reader: Box<dyn AsyncReadWithSeek + Unpin + Send + Sync + 'static>) -> Self {
23        Self { reader }
24    }
25
26    pub fn into_reader(self) -> Box<dyn Read + Unpin + Send + Sync + 'static> {
27        Box::new(self)
28    }
29}
30
31impl Read for AsyncReadWithSeekAdapter {
32    fn poll_read(
33        mut self: std::pin::Pin<&mut Self>,
34        cx: &mut Context<'_>,
35        buf: &mut [u8],
36    ) -> Poll<std::io::Result<usize>> {
37        Pin::new(&mut self.reader).poll_read(cx, buf)
38    }
39}