1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use derivative::Derivative;

use super::*;

use crate::VirtualFile;

#[derive(Derivative)]
#[derivative(Debug)]
pub struct CombineFile {
    tx: Box<dyn VirtualFile + Send + Sync + 'static>,
    rx: Box<dyn VirtualFile + Send + Sync + 'static>,
}

impl CombineFile {
    pub fn new(
        tx: Box<dyn VirtualFile + Send + Sync + 'static>,
        rx: Box<dyn VirtualFile + Send + Sync + 'static>,
    ) -> Self {
        Self { tx, rx }
    }
}

impl VirtualFile for CombineFile {
    fn last_accessed(&self) -> u64 {
        self.rx.last_accessed()
    }

    fn last_modified(&self) -> u64 {
        self.tx.last_modified()
    }

    fn created_time(&self) -> u64 {
        self.tx.created_time()
    }

    fn size(&self) -> u64 {
        self.rx.size()
    }

    fn set_len(&mut self, new_size: u64) -> Result<()> {
        self.tx.set_len(new_size)
    }

    fn unlink(&mut self) -> Result<()> {
        self.tx.unlink()
    }

    fn poll_read_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
        Pin::new(self.rx.as_mut()).poll_read_ready(cx)
    }

    fn poll_write_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
        Pin::new(self.tx.as_mut()).poll_write_ready(cx)
    }
}

impl AsyncWrite for CombineFile {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        Pin::new(&mut self.tx).poll_write(cx, buf)
    }

    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        Pin::new(&mut self.tx).poll_flush(cx)
    }

    fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        Pin::new(&mut self.tx).poll_shutdown(cx)
    }
}

impl AsyncRead for CombineFile {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        Pin::new(&mut self.rx).poll_read(cx, buf)
    }
}

impl AsyncSeek for CombineFile {
    fn start_seek(mut self: Pin<&mut Self>, position: io::SeekFrom) -> io::Result<()> {
        Pin::new(&mut self.tx).start_seek(position)?;
        Pin::new(&mut self.rx).start_seek(position)
    }

    fn poll_complete(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<io::Result<u64>> {
        if Pin::new(&mut self.tx).poll_complete(cx).is_pending() {
            return Poll::Pending;
        }
        Pin::new(&mut self.rx).poll_complete(cx)
    }
}