virtual_fs/
dual_write_file.rs1use super::*;
2
3use crate::VirtualFile;
4
5#[derive(derive_more::Debug)]
9pub struct DualWriteFile {
10 inner: Box<dyn VirtualFile + Send + Sync + 'static>,
11 #[allow(clippy::type_complexity)]
12 #[debug(ignore)]
13 extra_write: Box<dyn FnMut(&[u8]) + Send + Sync + 'static>,
14}
15
16impl DualWriteFile {
17 pub fn new(
18 inner: Box<dyn VirtualFile + Send + Sync + 'static>,
19 funct: impl FnMut(&[u8]) + Send + Sync + 'static,
20 ) -> Self {
21 Self {
22 inner,
23 extra_write: Box::new(funct),
24 }
25 }
26}
27
28impl VirtualFile for DualWriteFile {
29 fn last_accessed(&self) -> u64 {
30 self.inner.last_accessed()
31 }
32
33 fn last_modified(&self) -> u64 {
34 self.inner.last_modified()
35 }
36
37 fn created_time(&self) -> u64 {
38 self.inner.created_time()
39 }
40
41 fn set_times(&mut self, atime: Option<u64>, mtime: Option<u64>) -> crate::Result<()> {
42 self.inner.set_times(atime, mtime)
43 }
44
45 fn size(&self) -> u64 {
46 self.inner.size()
47 }
48
49 fn set_len(&mut self, new_size: u64) -> crate::Result<()> {
50 self.inner.set_len(new_size)
51 }
52
53 fn unlink(&mut self) -> Result<()> {
54 self.inner.unlink()
55 }
56
57 fn poll_read_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
58 Pin::new(self.inner.as_mut()).poll_read_ready(cx)
59 }
60
61 fn poll_write_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
62 Pin::new(self.inner.as_mut()).poll_write_ready(cx)
63 }
64}
65
66impl AsyncWrite for DualWriteFile {
67 fn poll_write(
68 mut self: Pin<&mut Self>,
69 cx: &mut std::task::Context<'_>,
70 buf: &[u8],
71 ) -> Poll<std::io::Result<usize>> {
72 match Pin::new(&mut self.inner).poll_write(cx, buf) {
73 Poll::Ready(Ok(amt)) => {
74 if amt > 0 {
75 (self.extra_write)(&buf[..amt]);
76 }
77 Poll::Ready(Ok(amt))
78 }
79 res => res,
80 }
81 }
82
83 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
84 Pin::new(&mut self.inner).poll_flush(cx)
85 }
86
87 fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
88 Pin::new(&mut self.inner).poll_shutdown(cx)
89 }
90}
91
92impl AsyncRead for DualWriteFile {
93 fn poll_read(
94 mut self: Pin<&mut Self>,
95 cx: &mut std::task::Context<'_>,
96 buf: &mut tokio::io::ReadBuf<'_>,
97 ) -> Poll<io::Result<()>> {
98 Pin::new(&mut self.inner).poll_read(cx, buf)
99 }
100}
101
102impl AsyncSeek for DualWriteFile {
103 fn start_seek(mut self: Pin<&mut Self>, position: io::SeekFrom) -> io::Result<()> {
104 Pin::new(&mut self.inner).start_seek(position)
105 }
106
107 fn poll_complete(
108 mut self: Pin<&mut Self>,
109 cx: &mut std::task::Context<'_>,
110 ) -> Poll<io::Result<u64>> {
111 Pin::new(&mut self.inner).poll_complete(cx)
112 }
113}