use serial::SerialPort;
use super::embedded_serial;
use super::{std, Error};
pub struct SerialWrap<T>
where T: SerialPort,
{
inner: T,
}
impl<T> SerialWrap<T>
where T: SerialPort,
{
pub fn new(port: T) -> SerialWrap<T> {
SerialWrap {inner: port}
}
}
impl<T> embedded_serial::NonBlockingRx for SerialWrap<T>
where T: SerialPort,
{
type Error=Error;
fn getc_try(&mut self) -> Result<Option<u8>, Self::Error> {
let mut buf: [u8;1] = [0];
match self.inner.read(&mut buf) {
Ok(1) => Ok(Some(buf[0])),
Ok(n_bytes) => return Err(Error::new(format!("no error, but {} bytes read.", n_bytes))),
Err(e) => {
match e.kind() {
std::io::ErrorKind::TimedOut => {Ok(None)},
_ => return Err(Error::new(format!("Can't read, err {:?}", e))),
}
},
}
}
}
impl<T> embedded_serial::NonBlockingTx for SerialWrap<T>
where T: SerialPort,
{
type Error=Error;
fn putc_try(&mut self, ch: u8) -> Result<Option<u8>, Self::Error> {
let buf: [u8; 1] = [ch];
match self.inner.write(&buf) {
Ok(0) => {
return Ok(None);
},
Ok(1) => {
return Ok(Some(ch));
},
Ok(_) => {
unreachable!();
},
Err(e) => {
return Err(Error::new(format!("write error {:?}",e)));
},
}
}
}