1use std::{
5 io::{self, IoSlice, SeekFrom},
6 iter,
7 pin::Pin,
8 task::{Context, Poll},
9};
10
11use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite};
12
13use crate::VirtualFile;
14
15#[derive(Debug, Default)]
16pub struct ZeroFile {}
17
18impl AsyncSeek for ZeroFile {
19 fn start_seek(self: Pin<&mut Self>, _position: SeekFrom) -> io::Result<()> {
20 Ok(())
21 }
22 fn poll_complete(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
23 Poll::Ready(Ok(0))
24 }
25}
26
27impl AsyncWrite for ZeroFile {
28 fn poll_write(
29 self: Pin<&mut Self>,
30 _cx: &mut Context<'_>,
31 buf: &[u8],
32 ) -> Poll<io::Result<usize>> {
33 Poll::Ready(Ok(buf.len()))
34 }
35 fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
36 Poll::Ready(Ok(()))
37 }
38 fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
39 Poll::Ready(Ok(()))
40 }
41 fn poll_write_vectored(
42 self: Pin<&mut Self>,
43 _cx: &mut Context<'_>,
44 bufs: &[IoSlice<'_>],
45 ) -> Poll<io::Result<usize>> {
46 Poll::Ready(Ok(bufs.len()))
47 }
48 fn is_write_vectored(&self) -> bool {
49 false
50 }
51}
52
53impl AsyncRead for ZeroFile {
54 fn poll_read(
55 self: Pin<&mut Self>,
56 _cx: &mut Context<'_>,
57 buf: &mut tokio::io::ReadBuf<'_>,
58 ) -> Poll<io::Result<()>> {
59 let zeros: Vec<u8> = iter::repeat_n(0, buf.remaining()).collect();
60 buf.put_slice(&zeros[..]);
61 Poll::Ready(Ok(()))
62 }
63}
64
65impl VirtualFile for ZeroFile {
66 fn last_accessed(&self) -> u64 {
67 0
68 }
69 fn last_modified(&self) -> u64 {
70 0
71 }
72 fn created_time(&self) -> u64 {
73 0
74 }
75 fn size(&self) -> u64 {
76 0
77 }
78 fn set_len(&mut self, _new_size: u64) -> crate::Result<()> {
79 Ok(())
80 }
81 fn unlink(&mut self) -> crate::Result<()> {
82 Ok(())
83 }
84 fn poll_read_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
85 Poll::Ready(Ok(0))
86 }
87 fn poll_write_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
88 Poll::Ready(Ok(0))
89 }
90}