virtual_fs/
random_file.rs1use std::io::{self, *};
5use std::pin::Pin;
6use std::task::{Context, Poll};
7
8use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite};
9
10use crate::VirtualFile;
11
12#[derive(Debug, Default)]
13pub struct RandomFile {}
14
15impl AsyncSeek for RandomFile {
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 RandomFile {
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 RandomFile {
51 fn poll_read(
52 self: Pin<&mut Self>,
53 _cx: &mut Context<'_>,
54 buf: &mut tokio::io::ReadBuf<'_>,
55 ) -> Poll<io::Result<()>> {
56 let mut data = vec![0u8; buf.remaining()];
57 getrandom::getrandom(&mut data).ok();
58 buf.put_slice(&data[..]);
59 Poll::Ready(Ok(()))
60 }
61}
62
63impl VirtualFile for RandomFile {
64 fn last_accessed(&self) -> u64 {
65 0
66 }
67 fn last_modified(&self) -> u64 {
68 0
69 }
70 fn created_time(&self) -> u64 {
71 0
72 }
73 fn size(&self) -> u64 {
74 0
75 }
76 fn set_len(&mut self, _new_size: u64) -> crate::Result<()> {
77 Ok(())
78 }
79 fn unlink(&mut self) -> crate::Result<()> {
80 Ok(())
81 }
82 fn poll_read_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
83 Poll::Ready(Ok(0))
84 }
85 fn poll_write_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
86 Poll::Ready(Ok(0))
87 }
88}