use std::collections::VecDeque;
use std::io::{self, Read, Write};
use mio::Token;
use parking_lot::Mutex;
use crate::device::Result;
use crate::device::console::Console;
use crate::sync::notifier::Notifier;
#[derive(Debug)]
pub struct TestConsole {
pub notifier: Mutex<Notifier>,
pub inbound: Mutex<VecDeque<u8>>,
pub outbound: Mutex<VecDeque<u8>>,
}
impl TestConsole {
pub fn new() -> Result<Self> {
Ok(Self {
notifier: Mutex::new(Notifier::new()?),
inbound: Mutex::new(VecDeque::new()),
outbound: Mutex::new(VecDeque::new()),
})
}
}
impl<'a> Read for &'a TestConsole {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
Read::read(&mut *self.inbound.lock(), buf)
}
}
impl<'a> Write for &'a TestConsole {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Write::write(&mut *self.outbound.lock(), buf)
}
fn flush(&mut self) -> io::Result<()> {
Write::flush(&mut *self.outbound.lock())
}
}
impl Console for TestConsole {
const TOKEN_INPUT: Token = Token(0);
fn activate(&self, registry: &mio::Registry) -> io::Result<()> {
registry.register(
&mut *self.notifier.lock(),
Self::TOKEN_INPUT,
mio::Interest::READABLE,
)
}
fn deactivate(&self, registry: &mio::Registry) -> io::Result<()> {
registry.deregister(&mut *self.notifier.lock())
}
}