get rid of async_trait
in (un)marshal
This commit is contained in:
parent
437776abb0
commit
011e397c60
@ -8,4 +8,4 @@ bytes = { version = "1.3.0", default-features = false, features = ["std"] }
|
||||
futures-util = { version = "0.3.25", default-features = false, features = ["io", "std"] }
|
||||
quinn = { version = "0.9.3", default-features = false, features = ["futures-io"] }
|
||||
thiserror = { version = "1.0.38", default-features = false }
|
||||
tuic = { path = "../tuic", default-features = false, features = ["marshal", "model"] }
|
||||
tuic = { path = "../tuic", default-features = false, features = ["async_marshal", "model"] }
|
||||
|
@ -17,7 +17,7 @@ use tuic::{
|
||||
AssembleError, Connect as ConnectModel, Connection as ConnectionModel,
|
||||
Packet as PacketModel,
|
||||
},
|
||||
Address, Header, Marshal, Unmarshal, UnmarshalError,
|
||||
Address, Header, UnmarshalError,
|
||||
};
|
||||
|
||||
pub mod side {
|
||||
@ -51,7 +51,7 @@ impl<'conn, Side> Connection<'conn, Side> {
|
||||
|
||||
for (header, frag) in model.into_fragments(pkt) {
|
||||
let mut buf = Cursor::new(vec![0; header.len() + frag.len()]);
|
||||
header.marshal(&mut buf).await?;
|
||||
header.async_marshal(&mut buf).await?;
|
||||
buf.write_all(frag).await.unwrap();
|
||||
self.conn.send_datagram(Bytes::from(buf.into_inner()))?;
|
||||
}
|
||||
@ -71,7 +71,7 @@ impl<'conn, Side> Connection<'conn, Side> {
|
||||
assert!(frags.next().is_none());
|
||||
|
||||
let mut send = self.conn.open_uni().await?;
|
||||
header.marshal(&mut send).await?;
|
||||
header.async_marshal(&mut send).await?;
|
||||
AsyncWriteExt::write_all(&mut send, frag).await?;
|
||||
send.close().await?;
|
||||
|
||||
@ -123,7 +123,7 @@ impl<'conn> Connection<'conn, side::Client> {
|
||||
pub async fn authenticate(&self, token: [u8; 8]) -> Result<(), Error> {
|
||||
let mut send = self.conn.open_uni().await?;
|
||||
let model = self.model.send_authenticate(token);
|
||||
model.header().marshal(&mut send).await?;
|
||||
model.header().async_marshal(&mut send).await?;
|
||||
send.close().await?;
|
||||
Ok(())
|
||||
}
|
||||
@ -131,20 +131,20 @@ impl<'conn> Connection<'conn, side::Client> {
|
||||
pub async fn connect(&self, addr: Address) -> Result<Connect, Error> {
|
||||
let (mut send, recv) = self.conn.open_bi().await?;
|
||||
let model = self.model.send_connect(addr);
|
||||
model.header().marshal(&mut send).await?;
|
||||
model.header().async_marshal(&mut send).await?;
|
||||
Ok(Connect::new(Side::Client(model), send, recv))
|
||||
}
|
||||
|
||||
pub async fn heartbeat(&self) -> Result<(), Error> {
|
||||
let model = self.model.send_heartbeat();
|
||||
let mut buf = Vec::with_capacity(model.header().len());
|
||||
model.header().marshal(&mut buf).await.unwrap();
|
||||
model.header().async_marshal(&mut buf).await.unwrap();
|
||||
self.conn.send_datagram(Bytes::from(buf))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn accept_uni_stream(&self, mut recv: RecvStream) -> Result<Task, Error> {
|
||||
match Header::unmarshal(&mut recv).await? {
|
||||
match Header::async_unmarshal(&mut recv).await? {
|
||||
Header::Authenticate(_) => Err(Error::BadCommand("authenticate")),
|
||||
Header::Connect(_) => Err(Error::BadCommand("connect")),
|
||||
Header::Packet(pkt) => {
|
||||
@ -164,7 +164,7 @@ impl<'conn> Connection<'conn, side::Client> {
|
||||
_send: SendStream,
|
||||
mut recv: RecvStream,
|
||||
) -> Result<Task, Error> {
|
||||
match Header::unmarshal(&mut recv).await? {
|
||||
match Header::async_unmarshal(&mut recv).await? {
|
||||
Header::Authenticate(_) => Err(Error::BadCommand("authenticate")),
|
||||
Header::Connect(_) => Err(Error::BadCommand("connect")),
|
||||
Header::Packet(_) => Err(Error::BadCommand("packet")),
|
||||
@ -177,7 +177,7 @@ impl<'conn> Connection<'conn, side::Client> {
|
||||
pub async fn accept_datagram(&self, dg: Bytes) -> Result<Task, Error> {
|
||||
let mut dg = Cursor::new(dg);
|
||||
|
||||
match Header::unmarshal(&mut dg).await? {
|
||||
match Header::async_unmarshal(&mut dg).await? {
|
||||
Header::Authenticate(_) => Err(Error::BadCommand("authenticate")),
|
||||
Header::Connect(_) => Err(Error::BadCommand("connect")),
|
||||
Header::Packet(pkt) => {
|
||||
@ -203,7 +203,7 @@ impl<'conn> Connection<'conn, side::Server> {
|
||||
}
|
||||
|
||||
pub async fn accept_uni_stream(&self, mut recv: RecvStream) -> Result<Task, Error> {
|
||||
match Header::unmarshal(&mut recv).await? {
|
||||
match Header::async_unmarshal(&mut recv).await? {
|
||||
Header::Authenticate(auth) => {
|
||||
let model = self.model.recv_authenticate(auth);
|
||||
Ok(Task::Authenticate(*model.token()))
|
||||
@ -229,7 +229,7 @@ impl<'conn> Connection<'conn, side::Server> {
|
||||
send: SendStream,
|
||||
mut recv: RecvStream,
|
||||
) -> Result<Task, Error> {
|
||||
match Header::unmarshal(&mut recv).await? {
|
||||
match Header::async_unmarshal(&mut recv).await? {
|
||||
Header::Authenticate(_) => Err(Error::BadCommand("authenticate")),
|
||||
Header::Connect(conn) => {
|
||||
let model = self.model.recv_connect(conn);
|
||||
@ -245,7 +245,7 @@ impl<'conn> Connection<'conn, side::Server> {
|
||||
pub async fn accept_datagram(&self, dg: Bytes) -> Result<Task, Error> {
|
||||
let mut dg = Cursor::new(dg);
|
||||
|
||||
match Header::unmarshal(&mut dg).await? {
|
||||
match Header::async_unmarshal(&mut dg).await? {
|
||||
Header::Authenticate(_) => Err(Error::BadCommand("authenticate")),
|
||||
Header::Connect(_) => Err(Error::BadCommand("connect")),
|
||||
Header::Packet(pkt) => {
|
||||
|
@ -4,14 +4,13 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
marshal = ["async-trait", "futures-io"]
|
||||
async_marshal = ["futures-io"]
|
||||
model = ["parking_lot", "thiserror"]
|
||||
|
||||
[dependencies]
|
||||
async-trait = { version = "0.1.62", default-features = false, optional = true }
|
||||
futures-io = { version = "0.3.25", default-features = false, features = ["std"], optional = true }
|
||||
parking_lot = { version = "0.12.1", default-features = false, optional = true }
|
||||
thiserror = { version = "1.0.38", default-features = false, optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tuic = { path = ".", features = ["marshal", "model"] }
|
||||
tuic = { path = ".", features = ["async_marshal", "model"] }
|
||||
|
@ -2,21 +2,18 @@
|
||||
|
||||
mod protocol;
|
||||
|
||||
#[cfg(feature = "marshal")]
|
||||
mod marshal;
|
||||
|
||||
#[cfg(feature = "marshal")]
|
||||
mod unmarshal;
|
||||
|
||||
pub use self::protocol::{
|
||||
Address, Authenticate, Command, Connect, Dissociate, Header, Heartbeat, Packet, VERSION,
|
||||
};
|
||||
|
||||
#[cfg(feature = "marshal")]
|
||||
pub use self::{
|
||||
marshal::Marshal,
|
||||
unmarshal::{Unmarshal, UnmarshalError},
|
||||
};
|
||||
#[cfg(feature = "async_marshal")]
|
||||
mod marshal;
|
||||
|
||||
#[cfg(feature = "async_marshal")]
|
||||
mod unmarshal;
|
||||
|
||||
#[cfg(feature = "async_marshal")]
|
||||
pub use self::unmarshal::UnmarshalError;
|
||||
|
||||
#[cfg(feature = "model")]
|
||||
pub mod model;
|
||||
|
@ -1,16 +1,9 @@
|
||||
use crate::protocol::Header;
|
||||
use async_trait::async_trait;
|
||||
use futures_io::AsyncWrite;
|
||||
use std::io::Error as IoError;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Marshal {
|
||||
async fn marshal(&self, s: &mut impl AsyncWrite) -> Result<(), IoError>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Marshal for Header {
|
||||
async fn marshal(&self, s: &mut impl AsyncWrite) -> Result<(), IoError> {
|
||||
impl Header {
|
||||
pub async fn async_marshal(&self, s: &mut impl AsyncWrite) -> Result<(), IoError> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,9 @@
|
||||
use crate::protocol::Header;
|
||||
use async_trait::async_trait;
|
||||
use futures_io::AsyncRead;
|
||||
use thiserror::Error;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Unmarshal
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
async fn unmarshal(s: &mut impl AsyncRead) -> Result<Self, UnmarshalError>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Unmarshal for Header {
|
||||
async fn unmarshal(s: &mut impl AsyncRead) -> Result<Self, UnmarshalError> {
|
||||
impl Header {
|
||||
pub async fn async_unmarshal(s: &mut impl AsyncRead) -> Result<Self, UnmarshalError> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user