#![allow(clippy::manual_async_fn)]
use crate::{
DecodeError, PlainOldData, RawRepr, TryFromRawRepr,
eio::{EioReadExactError, EioReadableError},
};
use core::net::{IpAddr, SocketAddr};
use core::ops::Bound;
pub trait EioAsyncReader {
type Error;
fn eio_read(&mut self, buf: &mut [u8]) -> impl Future<Output = Result<usize, Self::Error>>;
fn eio_read_exact(
&mut self,
buf: &mut [u8],
) -> impl Future<Output = Result<(), EioReadExactError<Self::Error>>>;
}
pub trait EioAsyncWriter {
type Error;
fn eio_write(&mut self, buf: &[u8]) -> impl Future<Output = Result<usize, Self::Error>>;
fn eio_write_all(&mut self, buf: &[u8]) -> impl Future<Output = Result<(), Self::Error>>;
fn eio_flush(&mut self) -> impl Future<Output = Result<(), Self::Error>>;
}
impl<T: ::embedded_io_async::Read + ?Sized> EioAsyncReader for T {
type Error = T::Error;
#[inline]
fn eio_read(&mut self, buf: &mut [u8]) -> impl Future<Output = Result<usize, Self::Error>> {
::embedded_io_async::Read::read(self, buf)
}
#[inline]
fn eio_read_exact(
&mut self,
buf: &mut [u8],
) -> impl Future<Output = Result<(), EioReadExactError<Self::Error>>> {
async move {
::embedded_io_async::Read::read_exact(self, buf)
.await
.map_err(|e| match e {
::embedded_io::ReadExactError::UnexpectedEof => {
EioReadExactError::UnexpectedEof
}
::embedded_io::ReadExactError::Other(err) => EioReadExactError::Other(err),
})
}
}
}
impl<T: ::embedded_io_async::Write + ?Sized> EioAsyncWriter for T {
type Error = T::Error;
#[inline]
fn eio_write(&mut self, buf: &[u8]) -> impl Future<Output = Result<usize, Self::Error>> {
::embedded_io_async::Write::write(self, buf)
}
#[inline]
fn eio_write_all(&mut self, buf: &[u8]) -> impl Future<Output = Result<(), Self::Error>> {
::embedded_io_async::Write::write_all(self, buf)
}
#[inline]
fn eio_flush(&mut self) -> impl Future<Output = Result<(), Self::Error>> {
::embedded_io_async::Write::flush(self)
}
}
pub trait EioAsyncFixedReadable: Sized {
fn read_fixed_from<R: EioAsyncReader + ?Sized>(
reader: &mut R,
) -> impl Future<Output = Result<Self, EioReadableError<R::Error>>>;
}
impl<T: TryFromRawRepr> EioAsyncFixedReadable for T {
#[inline]
fn read_fixed_from<R: EioAsyncReader + ?Sized>(
reader: &mut R,
) -> impl Future<Output = Result<Self, EioReadableError<R::Error>>> {
async move {
let mut b = T::Raw::zeroed();
reader.eio_read_exact(b.as_bytes_mut()).await?;
let r = T::try_from_raw(b)?;
Ok(r)
}
}
}
pub trait EioAsyncReadable: Sized {
fn read_from<R: EioAsyncReader + ?Sized>(
reader: &mut R,
) -> impl Future<Output = Result<Self, EioReadableError<R::Error>>>;
}
impl<T: EioAsyncFixedReadable> EioAsyncReadable for T {
#[inline]
fn read_from<R: EioAsyncReader + ?Sized>(
reader: &mut R,
) -> impl Future<Output = Result<Self, EioReadableError<R::Error>>> {
T::read_fixed_from(reader)
}
}
pub trait EioAsyncFixedWritable {
fn write_fixed_to<W: EioAsyncWriter + ?Sized>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<(), W::Error>>;
}
impl<T: RawRepr> EioAsyncFixedWritable for T {
#[inline]
fn write_fixed_to<W: EioAsyncWriter + ?Sized>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<(), W::Error>> {
async move {
let raw = self.to_raw();
writer.eio_write_all(raw.as_bytes()).await
}
}
}
pub trait EioAsyncWritable {
fn write_to<W: EioAsyncWriter + ?Sized>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<(), W::Error>>;
}
impl<T: EioAsyncFixedWritable> EioAsyncWritable for T {
#[inline]
fn write_to<W: EioAsyncWriter + ?Sized>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<(), W::Error>> {
self.write_fixed_to(writer)
}
}
pub trait EioAsyncReadFixed: EioAsyncReader {
#[inline]
fn read_fixed<T: EioAsyncFixedReadable>(
&mut self,
) -> impl Future<Output = Result<T, EioReadableError<Self::Error>>> {
T::read_fixed_from(self)
}
}
impl<T: EioAsyncReader + ?Sized> EioAsyncReadFixed for T {}
pub trait EioAsyncReadValue: EioAsyncReader {
#[inline]
fn read_value<T: EioAsyncReadable>(
&mut self,
) -> impl Future<Output = Result<T, EioReadableError<Self::Error>>> {
T::read_from(self)
}
}
impl<T: EioAsyncReader + ?Sized> EioAsyncReadValue for T {}
pub trait EioAsyncWriteFixed: EioAsyncWriter {
#[inline]
fn write_fixed(
&mut self,
val: &impl EioAsyncFixedWritable,
) -> impl Future<Output = Result<(), Self::Error>> {
val.write_fixed_to(self)
}
}
impl<T: EioAsyncWriter + ?Sized> EioAsyncWriteFixed for T {}
pub trait EioAsyncWriteValue: EioAsyncWriter {
#[inline]
fn write_value<T: EioAsyncWritable + ?Sized>(
&mut self,
data: &T,
) -> impl Future<Output = Result<(), Self::Error>> {
data.write_to(self)
}
}
impl<T: EioAsyncWriter + ?Sized> EioAsyncWriteValue for T {}
impl<T: EioAsyncReadable> EioAsyncReadable for Option<T> {
fn read_from<R: EioAsyncReader + ?Sized>(
reader: &mut R,
) -> impl Future<Output = Result<Self, EioReadableError<R::Error>>> {
async move {
let tag: u8 = reader.read_fixed().await?;
match tag {
0 => Ok(None),
1 => Ok(Some(reader.read_value().await?)),
_ => Err(EioReadableError::DecodeError(DecodeError::InvalidTag {
raw: tag,
type_name: "Option",
})),
}
}
}
}
impl<T: EioAsyncWritable> EioAsyncWritable for Option<T> {
fn write_to<W: EioAsyncWriter + ?Sized>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<(), W::Error>> {
async move {
match self {
None => writer.write_fixed(&0u8).await,
Some(val) => {
writer.write_fixed(&1u8).await?;
writer.write_value(val).await
}
}
}
}
}
impl<V: EioAsyncReadable, E: EioAsyncReadable> EioAsyncReadable for Result<V, E> {
fn read_from<R: EioAsyncReader + ?Sized>(
reader: &mut R,
) -> impl Future<Output = Result<Self, EioReadableError<R::Error>>> {
async move {
let discriminator: u8 = reader.read_fixed().await?;
match discriminator {
0 => Ok(Ok(reader.read_value().await?)),
1 => Ok(Err(reader.read_value().await?)),
_ => Err(EioReadableError::DecodeError(DecodeError::InvalidTag {
raw: discriminator,
type_name: "Result",
})),
}
}
}
}
impl<V: EioAsyncWritable, Er: EioAsyncWritable> EioAsyncWritable for Result<V, Er> {
fn write_to<W: EioAsyncWriter + ?Sized>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<(), W::Error>> {
async move {
match self {
Ok(val) => {
writer.write_fixed(&0u8).await?;
writer.write_value(val).await
}
Err(err) => {
writer.write_fixed(&1u8).await?;
writer.write_value(err).await
}
}
}
}
}
impl EioAsyncReadable for IpAddr {
fn read_from<R: EioAsyncReader + ?Sized>(
reader: &mut R,
) -> impl Future<Output = Result<Self, EioReadableError<R::Error>>> {
async move {
let tag: u8 = reader.read_fixed().await?;
match tag {
0 => Ok(IpAddr::V4(reader.read_fixed().await?)),
1 => Ok(IpAddr::V6(reader.read_fixed().await?)),
_ => Err(EioReadableError::DecodeError(DecodeError::InvalidTag {
raw: tag,
type_name: "IpAddr",
})),
}
}
}
}
impl EioAsyncWritable for IpAddr {
fn write_to<W: EioAsyncWriter + ?Sized>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<(), W::Error>> {
async move {
match self {
IpAddr::V4(addr) => {
writer.write_fixed(&0u8).await?;
writer.write_fixed(addr).await
}
IpAddr::V6(addr) => {
writer.write_fixed(&1u8).await?;
writer.write_fixed(addr).await
}
}
}
}
}
impl EioAsyncReadable for SocketAddr {
fn read_from<R: EioAsyncReader + ?Sized>(
reader: &mut R,
) -> impl Future<Output = Result<Self, EioReadableError<R::Error>>> {
async move {
let tag: u8 = reader.read_fixed().await?;
match tag {
0 => Ok(SocketAddr::V4(reader.read_fixed().await?)),
1 => Ok(SocketAddr::V6(reader.read_fixed().await?)),
_ => Err(EioReadableError::DecodeError(DecodeError::InvalidTag {
raw: tag,
type_name: "SocketAddr",
})),
}
}
}
}
impl EioAsyncWritable for SocketAddr {
fn write_to<W: EioAsyncWriter + ?Sized>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<(), W::Error>> {
async move {
match self {
SocketAddr::V4(addr) => {
writer.write_fixed(&0u8).await?;
writer.write_fixed(addr).await
}
SocketAddr::V6(addr) => {
writer.write_fixed(&1u8).await?;
writer.write_fixed(addr).await
}
}
}
}
}
impl<T: EioAsyncReadable> EioAsyncReadable for Bound<T> {
fn read_from<R: EioAsyncReader + ?Sized>(
reader: &mut R,
) -> impl Future<Output = Result<Self, EioReadableError<R::Error>>> {
async move {
let tag: u8 = reader.read_fixed().await?;
match tag {
0 => Ok(Bound::Included(reader.read_value().await?)),
1 => Ok(Bound::Excluded(reader.read_value().await?)),
2 => Ok(Bound::Unbounded),
_ => Err(EioReadableError::DecodeError(DecodeError::InvalidTag {
raw: tag,
type_name: "Bound",
})),
}
}
}
}
impl<T: EioAsyncWritable> EioAsyncWritable for Bound<T> {
fn write_to<W: EioAsyncWriter + ?Sized>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<(), W::Error>> {
async move {
match self {
Bound::Included(val) => {
writer.write_fixed(&0u8).await?;
writer.write_value(val).await
}
Bound::Excluded(val) => {
writer.write_fixed(&1u8).await?;
writer.write_value(val).await
}
Bound::Unbounded => writer.write_fixed(&2u8).await,
}
}
}
}
macro_rules! impl_tuple {
($($T:ident),+) => {
impl<$($T: EioAsyncReadable),+> EioAsyncReadable for ($($T,)+) {
fn read_from<R: EioAsyncReader + ?Sized>(
reader: &mut R,
) -> impl Future<Output = Result<Self, EioReadableError<R::Error>>> {
async move {
Ok(($(reader.read_value::<$T>().await?,)+))
}
}
}
impl<$($T: EioAsyncWritable),+> EioAsyncWritable for ($($T,)+) {
fn write_to<W: EioAsyncWriter + ?Sized>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<(), W::Error>> {
async move {
#[allow(non_snake_case)]
let ($($T,)+) = self;
$( writer.write_value($T).await?; )+
Ok(())
}
}
}
};
}
impl_tuple!(A);
impl_tuple!(A, B);
impl_tuple!(A, B, C);
impl_tuple!(A, B, C, D);
impl_tuple!(A, B, C, D, E);
impl_tuple!(A, B, C, D, E, F);
impl_tuple!(A, B, C, D, E, F, G);
impl_tuple!(A, B, C, D, E, F, G, H);
impl_tuple!(A, B, C, D, E, F, G, H, I);
impl_tuple!(A, B, C, D, E, F, G, H, I, J);
impl_tuple!(A, B, C, D, E, F, G, H, I, J, K);
impl_tuple!(A, B, C, D, E, F, G, H, I, J, K, L);
impl<T: EioAsyncWritable> EioAsyncWritable for [T] {
fn write_to<W: EioAsyncWriter + ?Sized>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<(), W::Error>> {
async move {
let len: u64 = self
.len()
.try_into()
.expect("could not convert usize to u64");
writer.write_fixed(&len).await?;
for el in self {
writer.write_value(el).await?;
}
Ok(())
}
}
}
impl EioAsyncWritable for str {
fn write_to<W: EioAsyncWriter + ?Sized>(
&self,
writer: &mut W,
) -> impl Future<Output = Result<(), W::Error>> {
async move {
let len: u64 = self
.len()
.try_into()
.expect("could not convert usize to u64");
writer.write_fixed(&len).await?;
writer.eio_write_all(self.as_bytes()).await
}
}
}