use super::{Read, Write, error::LibcError};
use crate::Device;
use winter_maybe_async::maybe_async;
pub struct ReadWriteDevice<I: Read, O: Write, E: Write> {
stdin: I,
stdout: O,
stderr: E,
}
impl<I: Read, O: Write, E: Write> ReadWriteDevice<I, O, E> {
pub const fn new(stdin: I, stdout: O, stderr: E) -> Self {
Self {
stdin,
stdout,
stderr,
}
}
pub const fn stdin(&self) -> &I {
&self.stdin
}
pub const fn stdout(&self) -> &O {
&self.stdout
}
pub const fn stderr(&self) -> &E {
&self.stderr
}
}
impl<I: Read, O: Write, E: Write> Device for ReadWriteDevice<I, O, E> {
type Error = LibcError;
#[maybe_async]
fn read(&mut self) -> Result<Option<u8>, Self::Error> {
self.stdin.read().map_err(|_| LibcError::Stdin)
}
#[maybe_async]
fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
self.stdout.write(byte).map_err(|_| LibcError::Stdout)
}
#[maybe_async]
fn write_error(&mut self, byte: u8) -> Result<(), Self::Error> {
self.stderr.write(byte).map_err(|_| LibcError::Stderr)
}
}