virtual_fs/
combine_file.rs

1use super::*;
2
3use crate::VirtualFile;
4
5#[derive(Debug)]
6pub struct CombineFile {
7    tx: Box<dyn VirtualFile + Send + Sync + 'static>,
8    rx: Box<dyn VirtualFile + Send + Sync + 'static>,
9}
10
11impl CombineFile {
12    pub fn new(
13        tx: Box<dyn VirtualFile + Send + Sync + 'static>,
14        rx: Box<dyn VirtualFile + Send + Sync + 'static>,
15    ) -> Self {
16        Self { tx, rx }
17    }
18}
19
20impl VirtualFile for CombineFile {
21    fn last_accessed(&self) -> u64 {
22        self.rx.last_accessed()
23    }
24
25    fn last_modified(&self) -> u64 {
26        self.tx.last_modified()
27    }
28
29    fn created_time(&self) -> u64 {
30        self.tx.created_time()
31    }
32
33    fn set_times(&mut self, atime: Option<u64>, mtime: Option<u64>) -> crate::Result<()> {
34        self.tx.set_times(atime, mtime)
35    }
36
37    fn size(&self) -> u64 {
38        self.rx.size()
39    }
40
41    fn set_len(&mut self, new_size: u64) -> Result<()> {
42        self.tx.set_len(new_size)
43    }
44
45    fn unlink(&mut self) -> Result<()> {
46        self.tx.unlink()
47    }
48
49    fn poll_read_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
50        Pin::new(self.rx.as_mut()).poll_read_ready(cx)
51    }
52
53    fn poll_write_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
54        Pin::new(self.tx.as_mut()).poll_write_ready(cx)
55    }
56}
57
58impl AsyncWrite for CombineFile {
59    fn poll_write(
60        mut self: Pin<&mut Self>,
61        cx: &mut std::task::Context<'_>,
62        buf: &[u8],
63    ) -> Poll<std::io::Result<usize>> {
64        Pin::new(&mut self.tx).poll_write(cx, buf)
65    }
66
67    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
68        Pin::new(&mut self.tx).poll_flush(cx)
69    }
70
71    fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
72        Pin::new(&mut self.tx).poll_shutdown(cx)
73    }
74}
75
76impl AsyncRead for CombineFile {
77    fn poll_read(
78        mut self: Pin<&mut Self>,
79        cx: &mut std::task::Context<'_>,
80        buf: &mut tokio::io::ReadBuf<'_>,
81    ) -> Poll<io::Result<()>> {
82        Pin::new(&mut self.rx).poll_read(cx, buf)
83    }
84}
85
86impl AsyncSeek for CombineFile {
87    fn start_seek(mut self: Pin<&mut Self>, position: io::SeekFrom) -> io::Result<()> {
88        Pin::new(&mut self.tx).start_seek(position)?;
89        Pin::new(&mut self.rx).start_seek(position)
90    }
91
92    fn poll_complete(
93        mut self: Pin<&mut Self>,
94        cx: &mut std::task::Context<'_>,
95    ) -> Poll<io::Result<u64>> {
96        if Pin::new(&mut self.tx).poll_complete(cx).is_pending() {
97            return Poll::Pending;
98        }
99        Pin::new(&mut self.rx).poll_complete(cx)
100    }
101}