testing_support/
devices.rs1use core::time::Duration;
4use file_system::{
5 DirectBaseOperations, DirectCharacterDevice, IoErrorKind, MountOperations, Result, Size,
6};
7use std::time::{SystemTime, UNIX_EPOCH};
8
9pub struct TimeDevice;
10
11impl DirectBaseOperations for TimeDevice {
12 fn read(
13 &self,
14 _cx: &mut core::task::Context<'_>,
15 buffer: &mut [u8],
16 _: Size,
17 ) -> core::task::Poll<Result<usize>> {
18 file_system::ready(|| {
19 if buffer.len() < size_of::<Duration>() {
20 return Err(IoErrorKind::InvalidArgument);
21 }
22
23 let duration = SystemTime::now()
24 .duration_since(UNIX_EPOCH)
25 .map_err(|_| IoErrorKind::InputOutput)?;
26
27 let duration: Duration = Duration::new(duration.as_secs(), duration.subsec_nanos());
28
29 let duration_bytes = unsafe {
30 core::slice::from_raw_parts(
31 &duration as *const Duration as *const u8,
32 size_of::<Duration>(),
33 )
34 };
35
36 buffer[..duration_bytes.len()].copy_from_slice(duration_bytes);
37
38 Ok(buffer.len())
39 })
40 }
41
42 fn write(
43 &self,
44 _cx: &mut core::task::Context<'_>,
45 _: &[u8],
46 _: Size,
47 ) -> core::task::Poll<Result<usize>> {
48 file_system::ready(|| Err(IoErrorKind::Unsupported))
49 }
50}
51
52impl MountOperations for TimeDevice {}
53
54impl DirectCharacterDevice for TimeDevice {}
55
56impl time::RealtimeSource for TimeDevice {
58 fn read(&self) -> time::Result<time::RealtimeSample> {
59 let now = SystemTime::now();
60 let timestamp = match now.duration_since(UNIX_EPOCH) {
61 Ok(duration) => time::Timestamp::from(duration),
62 Err(before) => time::Timestamp::from_nanos(-(before.duration().as_nanos() as i128)),
63 };
64 Ok(time::RealtimeSample {
65 timestamp,
66 source: time::ClockSource::Host,
67 resolution: Duration::from_millis(1),
69 accuracy: None,
70 })
71 }
72}
73
74pub struct RandomDevice;
75
76impl DirectBaseOperations for RandomDevice {
77 fn read(
78 &self,
79 _cx: &mut core::task::Context<'_>,
80 buffer: &mut [u8],
81 _: Size,
82 ) -> core::task::Poll<file_system::Result<usize>> {
83 file_system::ready(|| {
84 getrandom::fill(buffer).map_err(|_| file_system::IoErrorKind::InputOutput)?;
85
86 Ok(buffer.len())
87 })
88 }
89
90 fn write(
91 &self,
92 _cx: &mut core::task::Context<'_>,
93 _: &[u8],
94 _: Size,
95 ) -> core::task::Poll<file_system::Result<usize>> {
96 file_system::ready(|| Err(file_system::IoErrorKind::Unsupported))
97 }
98
99 fn set_position(
100 &self,
101 _: Size,
102 _position: &file_system::Position,
103 ) -> file_system::Result<file_system::Size> {
104 Err(file_system::IoErrorKind::Unsupported)
105 }
106}
107
108impl MountOperations for RandomDevice {}
109
110impl DirectCharacterDevice for RandomDevice {}
111
112pub enum ConsoleDevice {
115 Input,
116 Output,
117 Error,
118}
119
120impl DirectBaseOperations for ConsoleDevice {
121 fn read(
122 &self,
123 _cx: &mut core::task::Context<'_>,
124 buffer: &mut [u8],
125 _: Size,
126 ) -> core::task::Poll<file_system::Result<usize>> {
127 use std::io::Read;
128 file_system::ready(|| match self {
129 Self::Input => std::io::stdin()
130 .read(buffer)
131 .map_err(|_| IoErrorKind::InputOutput),
132 _ => Err(IoErrorKind::Unsupported),
133 })
134 }
135
136 fn write(
137 &self,
138 _cx: &mut core::task::Context<'_>,
139 buffer: &[u8],
140 _: Size,
141 ) -> core::task::Poll<file_system::Result<usize>> {
142 use std::io::Write;
143 file_system::ready(|| match self {
144 Self::Input => Err(IoErrorKind::Unsupported),
145 Self::Output => std::io::stdout()
146 .write(buffer)
147 .map_err(|_| IoErrorKind::InputOutput),
148 Self::Error => std::io::stderr()
149 .write(buffer)
150 .map_err(|_| IoErrorKind::InputOutput),
151 })
152 }
153
154 fn flush(
155 &self,
156 _cx: &mut core::task::Context<'_>,
157 ) -> core::task::Poll<file_system::Result<()>> {
158 use std::io::Write;
159 file_system::ready(|| match self {
160 Self::Input => Ok(()),
161 Self::Output => std::io::stdout()
162 .flush()
163 .map_err(|_| IoErrorKind::InputOutput),
164 Self::Error => std::io::stderr()
165 .flush()
166 .map_err(|_| IoErrorKind::InputOutput),
167 })
168 }
169}
170
171impl MountOperations for ConsoleDevice {}
172
173impl DirectCharacterDevice for ConsoleDevice {}