stak_device/
device.rs

1mod buffer_error;
2mod fixed_buffer;
3#[cfg(feature = "libc")]
4pub mod libc;
5#[cfg(feature = "std")]
6mod read_write;
7#[cfg(feature = "std")]
8mod stdio;
9mod void;
10
11pub use buffer_error::BufferError;
12use core::error::Error;
13pub use fixed_buffer::FixedBufferDevice;
14#[cfg(feature = "std")]
15pub use read_write::ReadWriteDevice;
16#[cfg(feature = "std")]
17pub use stdio::StdioDevice;
18pub use void::*;
19use winter_maybe_async::{maybe_async, maybe_await};
20
21/// A device.
22pub trait Device {
23    /// An error.
24    type Error: Error;
25
26    /// Reads from standard input.
27    #[maybe_async]
28    fn read(&mut self) -> Result<Option<u8>, Self::Error>;
29
30    /// Writes to standard output.
31    #[maybe_async]
32    fn write(&mut self, byte: u8) -> Result<(), Self::Error>;
33
34    /// Writes to standard error.
35    #[maybe_async]
36    fn write_error(&mut self, byte: u8) -> Result<(), Self::Error>;
37}
38
39impl<T: Device> Device for &mut T {
40    type Error = T::Error;
41
42    #[maybe_async]
43    fn read(&mut self) -> Result<Option<u8>, Self::Error> {
44        maybe_await!((**self).read())
45    }
46
47    #[maybe_async]
48    fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
49        maybe_await!((**self).write(byte))
50    }
51
52    #[maybe_async]
53    fn write_error(&mut self, byte: u8) -> Result<(), Self::Error> {
54        maybe_await!((**self).write_error(byte))
55    }
56}