channels_io/impls/
embedded_io.rs

1use super::prelude::*;
2
3use ::embedded_io::{Error as _, ErrorKind};
4
5impl IoError for ErrorKind {
6	fn should_retry(&self) -> bool {
7		self.kind() == ErrorKind::Interrupted
8	}
9}
10
11impl ReadError for ErrorKind {
12	fn eof() -> Self {
13		ErrorKind::BrokenPipe
14	}
15}
16
17impl WriteError for ErrorKind {
18	fn write_zero() -> Self {
19		ErrorKind::WriteZero
20	}
21}
22
23/// Wrapper IO type for [`embedded_io::Read`] and [`embedded_io::Write`].
24///
25/// [`embedded_io::Read`]: ::embedded_io::Read
26/// [`embedded_io::Write`]: ::embedded_io::Write
27#[derive(Debug)]
28pub struct EmbeddedIo<T>(pub T);
29
30impl_newtype! { EmbeddedIo }
31
32impl_newtype_read! { EmbeddedIo: ::embedded_io::Read }
33
34impl<T> Read for EmbeddedIo<T>
35where
36	T: ::embedded_io::Read,
37{
38	type Error = ErrorKind;
39
40	fn read_slice(
41		&mut self,
42		buf: &mut [u8],
43	) -> Result<usize, Self::Error> {
44		self.0.read(buf).map_err(|x| x.kind())
45	}
46}
47
48impl_newtype_write! { EmbeddedIo: ::embedded_io::Write }
49
50impl<T> Write for EmbeddedIo<T>
51where
52	T: ::embedded_io::Write,
53{
54	type Error = ErrorKind;
55
56	fn write_slice(
57		&mut self,
58		buf: &[u8],
59	) -> Result<usize, Self::Error> {
60		self.0.write(buf).map_err(|x| x.kind())
61	}
62
63	fn flush_once(&mut self) -> Result<(), Self::Error> {
64		self.0.flush().map_err(|x| x.kind())
65	}
66}