1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::{TypedAsyncRead, TypedAsyncWrite, TypedTransport};
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Serialize};
use std::io;
pub trait UntypedTransport: UntypedTransportRead + UntypedTransportWrite {}
#[async_trait]
pub trait UntypedTransportRead: Send + Unpin {
async fn read<T>(&mut self) -> io::Result<Option<T>>
where
T: DeserializeOwned;
}
#[async_trait]
pub trait UntypedTransportWrite: Send + Unpin {
async fn write<T>(&mut self, data: T) -> io::Result<()>
where
T: Serialize + Send + 'static;
}
impl<T, W, R> TypedTransport<W, R> for T
where
T: UntypedTransport + Send,
W: Serialize + Send + 'static,
R: DeserializeOwned,
{
}
#[async_trait]
impl<W, T> TypedAsyncWrite<T> for W
where
W: UntypedTransportWrite + Send,
T: Serialize + Send + 'static,
{
async fn write(&mut self, data: T) -> io::Result<()> {
W::write(self, data).await
}
}
#[async_trait]
impl<R, T> TypedAsyncRead<T> for R
where
R: UntypedTransportRead + Send,
T: DeserializeOwned,
{
async fn read(&mut self) -> io::Result<Option<T>> {
R::read(self).await
}
}