virtual_fs/
null_file.rs

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