virtual_fs/
arc_box_file.rs1use std::{
5 io::{self, *},
6 pin::Pin,
7 sync::{Arc, Mutex},
8 task::{Context, Poll},
9};
10
11use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite};
12
13use crate::VirtualFile;
14
15#[derive(Debug, Clone)]
16pub struct ArcBoxFile {
17 inner: Arc<Mutex<Box<dyn VirtualFile + Send + Sync + 'static>>>,
18}
19
20impl ArcBoxFile {
21 pub fn new(inner: Box<dyn VirtualFile + Send + Sync + 'static>) -> Self {
22 Self {
23 inner: Arc::new(Mutex::new(inner)),
24 }
25 }
26}
27
28impl AsyncSeek for ArcBoxFile {
29 fn start_seek(self: Pin<&mut Self>, position: SeekFrom) -> io::Result<()> {
30 let mut guard = self.inner.lock().unwrap();
31 let file = Pin::new(guard.as_mut());
32 file.start_seek(position)
33 }
34 fn poll_complete(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
35 let mut guard = self.inner.lock().unwrap();
36 let file = Pin::new(guard.as_mut());
37 file.poll_complete(cx)
38 }
39}
40
41impl AsyncWrite for ArcBoxFile {
42 fn poll_write(
43 self: Pin<&mut Self>,
44 cx: &mut Context<'_>,
45 buf: &[u8],
46 ) -> Poll<io::Result<usize>> {
47 let mut guard = self.inner.lock().unwrap();
48 let file = Pin::new(guard.as_mut());
49 file.poll_write(cx, buf)
50 }
51 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
52 let mut guard = self.inner.lock().unwrap();
53 let file = Pin::new(guard.as_mut());
54 file.poll_flush(cx)
55 }
56 fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
57 let mut guard = self.inner.lock().unwrap();
58 let file = Pin::new(guard.as_mut());
59 file.poll_shutdown(cx)
60 }
61 fn poll_write_vectored(
62 self: Pin<&mut Self>,
63 cx: &mut Context<'_>,
64 bufs: &[IoSlice<'_>],
65 ) -> Poll<io::Result<usize>> {
66 let mut guard = self.inner.lock().unwrap();
67 let file = Pin::new(guard.as_mut());
68 file.poll_write_vectored(cx, bufs)
69 }
70 fn is_write_vectored(&self) -> bool {
71 let mut guard = self.inner.lock().unwrap();
72 let file = Pin::new(guard.as_mut());
73 file.is_write_vectored()
74 }
75}
76
77impl AsyncRead for ArcBoxFile {
78 fn poll_read(
79 self: Pin<&mut Self>,
80 cx: &mut Context<'_>,
81 buf: &mut tokio::io::ReadBuf<'_>,
82 ) -> Poll<io::Result<()>> {
83 let mut guard = self.inner.lock().unwrap();
84 let file = Pin::new(guard.as_mut());
85 file.poll_read(cx, buf)
86 }
87}
88
89impl VirtualFile for ArcBoxFile {
90 fn last_accessed(&self) -> u64 {
91 let inner = self.inner.lock().unwrap();
92 inner.last_accessed()
93 }
94 fn last_modified(&self) -> u64 {
95 let inner = self.inner.lock().unwrap();
96 inner.last_modified()
97 }
98 fn created_time(&self) -> u64 {
99 let inner = self.inner.lock().unwrap();
100 inner.created_time()
101 }
102 fn set_times(&mut self, atime: Option<u64>, mtime: Option<u64>) -> crate::Result<()> {
103 let mut inner = self.inner.lock().unwrap();
104 inner.set_times(atime, mtime)
105 }
106 fn size(&self) -> u64 {
107 let inner = self.inner.lock().unwrap();
108 inner.size()
109 }
110 fn set_len(&mut self, new_size: u64) -> crate::Result<()> {
111 let mut inner = self.inner.lock().unwrap();
112 inner.set_len(new_size)
113 }
114 fn unlink(&mut self) -> crate::Result<()> {
115 let mut inner = self.inner.lock().unwrap();
116 inner.unlink()
117 }
118 fn is_open(&self) -> bool {
119 let inner = self.inner.lock().unwrap();
120 inner.is_open()
121 }
122 fn get_special_fd(&self) -> Option<u32> {
123 let inner = self.inner.lock().unwrap();
124 inner.get_special_fd()
125 }
126 fn poll_read_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
127 let mut inner = self.inner.lock().unwrap();
128 let inner = Pin::new(inner.as_mut());
129 inner.poll_read_ready(cx)
130 }
131 fn poll_write_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
132 let mut inner = self.inner.lock().unwrap();
133 let inner = Pin::new(inner.as_mut());
134 inner.poll_write_ready(cx)
135 }
136}
137
138impl From<Box<dyn VirtualFile + Send + Sync + 'static>> for ArcBoxFile {
139 fn from(val: Box<dyn VirtualFile + Send + Sync + 'static>) -> Self {
140 ArcBoxFile::new(val)
141 }
142}