1
0

get rid of async_trait in (un)marshal

This commit is contained in:
EAimTY 2023-01-27 01:41:44 +09:00
parent 437776abb0
commit 011e397c60
6 changed files with 27 additions and 48 deletions

View File

@ -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"] } futures-util = { version = "0.3.25", default-features = false, features = ["io", "std"] }
quinn = { version = "0.9.3", default-features = false, features = ["futures-io"] } quinn = { version = "0.9.3", default-features = false, features = ["futures-io"] }
thiserror = { version = "1.0.38", default-features = false } 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"] }

View File

@ -17,7 +17,7 @@ use tuic::{
AssembleError, Connect as ConnectModel, Connection as ConnectionModel, AssembleError, Connect as ConnectModel, Connection as ConnectionModel,
Packet as PacketModel, Packet as PacketModel,
}, },
Address, Header, Marshal, Unmarshal, UnmarshalError, Address, Header, UnmarshalError,
}; };
pub mod side { pub mod side {
@ -51,7 +51,7 @@ impl<'conn, Side> Connection<'conn, Side> {
for (header, frag) in model.into_fragments(pkt) { for (header, frag) in model.into_fragments(pkt) {
let mut buf = Cursor::new(vec![0; header.len() + frag.len()]); 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(); buf.write_all(frag).await.unwrap();
self.conn.send_datagram(Bytes::from(buf.into_inner()))?; self.conn.send_datagram(Bytes::from(buf.into_inner()))?;
} }
@ -71,7 +71,7 @@ impl<'conn, Side> Connection<'conn, Side> {
assert!(frags.next().is_none()); assert!(frags.next().is_none());
let mut send = self.conn.open_uni().await?; 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?; AsyncWriteExt::write_all(&mut send, frag).await?;
send.close().await?; send.close().await?;
@ -123,7 +123,7 @@ impl<'conn> Connection<'conn, side::Client> {
pub async fn authenticate(&self, token: [u8; 8]) -> Result<(), Error> { pub async fn authenticate(&self, token: [u8; 8]) -> Result<(), Error> {
let mut send = self.conn.open_uni().await?; let mut send = self.conn.open_uni().await?;
let model = self.model.send_authenticate(token); let model = self.model.send_authenticate(token);
model.header().marshal(&mut send).await?; model.header().async_marshal(&mut send).await?;
send.close().await?; send.close().await?;
Ok(()) Ok(())
} }
@ -131,20 +131,20 @@ impl<'conn> Connection<'conn, side::Client> {
pub async fn connect(&self, addr: Address) -> Result<Connect, Error> { pub async fn connect(&self, addr: Address) -> Result<Connect, Error> {
let (mut send, recv) = self.conn.open_bi().await?; let (mut send, recv) = self.conn.open_bi().await?;
let model = self.model.send_connect(addr); 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)) Ok(Connect::new(Side::Client(model), send, recv))
} }
pub async fn heartbeat(&self) -> Result<(), Error> { pub async fn heartbeat(&self) -> Result<(), Error> {
let model = self.model.send_heartbeat(); let model = self.model.send_heartbeat();
let mut buf = Vec::with_capacity(model.header().len()); 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))?; self.conn.send_datagram(Bytes::from(buf))?;
Ok(()) Ok(())
} }
pub async fn accept_uni_stream(&self, mut recv: RecvStream) -> Result<Task, Error> { 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::Authenticate(_) => Err(Error::BadCommand("authenticate")),
Header::Connect(_) => Err(Error::BadCommand("connect")), Header::Connect(_) => Err(Error::BadCommand("connect")),
Header::Packet(pkt) => { Header::Packet(pkt) => {
@ -164,7 +164,7 @@ impl<'conn> Connection<'conn, side::Client> {
_send: SendStream, _send: SendStream,
mut recv: RecvStream, mut recv: RecvStream,
) -> Result<Task, Error> { ) -> Result<Task, Error> {
match Header::unmarshal(&mut recv).await? { match Header::async_unmarshal(&mut recv).await? {
Header::Authenticate(_) => Err(Error::BadCommand("authenticate")), Header::Authenticate(_) => Err(Error::BadCommand("authenticate")),
Header::Connect(_) => Err(Error::BadCommand("connect")), Header::Connect(_) => Err(Error::BadCommand("connect")),
Header::Packet(_) => Err(Error::BadCommand("packet")), 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> { pub async fn accept_datagram(&self, dg: Bytes) -> Result<Task, Error> {
let mut dg = Cursor::new(dg); 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::Authenticate(_) => Err(Error::BadCommand("authenticate")),
Header::Connect(_) => Err(Error::BadCommand("connect")), Header::Connect(_) => Err(Error::BadCommand("connect")),
Header::Packet(pkt) => { 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> { 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) => { Header::Authenticate(auth) => {
let model = self.model.recv_authenticate(auth); let model = self.model.recv_authenticate(auth);
Ok(Task::Authenticate(*model.token())) Ok(Task::Authenticate(*model.token()))
@ -229,7 +229,7 @@ impl<'conn> Connection<'conn, side::Server> {
send: SendStream, send: SendStream,
mut recv: RecvStream, mut recv: RecvStream,
) -> Result<Task, Error> { ) -> Result<Task, Error> {
match Header::unmarshal(&mut recv).await? { match Header::async_unmarshal(&mut recv).await? {
Header::Authenticate(_) => Err(Error::BadCommand("authenticate")), Header::Authenticate(_) => Err(Error::BadCommand("authenticate")),
Header::Connect(conn) => { Header::Connect(conn) => {
let model = self.model.recv_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> { pub async fn accept_datagram(&self, dg: Bytes) -> Result<Task, Error> {
let mut dg = Cursor::new(dg); 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::Authenticate(_) => Err(Error::BadCommand("authenticate")),
Header::Connect(_) => Err(Error::BadCommand("connect")), Header::Connect(_) => Err(Error::BadCommand("connect")),
Header::Packet(pkt) => { Header::Packet(pkt) => {

View File

@ -4,14 +4,13 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[features] [features]
marshal = ["async-trait", "futures-io"] async_marshal = ["futures-io"]
model = ["parking_lot", "thiserror"] model = ["parking_lot", "thiserror"]
[dependencies] [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 } futures-io = { version = "0.3.25", default-features = false, features = ["std"], optional = true }
parking_lot = { version = "0.12.1", default-features = false, optional = true } parking_lot = { version = "0.12.1", default-features = false, optional = true }
thiserror = { version = "1.0.38", default-features = false, optional = true } thiserror = { version = "1.0.38", default-features = false, optional = true }
[dev-dependencies] [dev-dependencies]
tuic = { path = ".", features = ["marshal", "model"] } tuic = { path = ".", features = ["async_marshal", "model"] }

View File

@ -2,21 +2,18 @@
mod protocol; mod protocol;
#[cfg(feature = "marshal")]
mod marshal;
#[cfg(feature = "marshal")]
mod unmarshal;
pub use self::protocol::{ pub use self::protocol::{
Address, Authenticate, Command, Connect, Dissociate, Header, Heartbeat, Packet, VERSION, Address, Authenticate, Command, Connect, Dissociate, Header, Heartbeat, Packet, VERSION,
}; };
#[cfg(feature = "marshal")] #[cfg(feature = "async_marshal")]
pub use self::{ mod marshal;
marshal::Marshal,
unmarshal::{Unmarshal, UnmarshalError}, #[cfg(feature = "async_marshal")]
}; mod unmarshal;
#[cfg(feature = "async_marshal")]
pub use self::unmarshal::UnmarshalError;
#[cfg(feature = "model")] #[cfg(feature = "model")]
pub mod model; pub mod model;

View File

@ -1,16 +1,9 @@
use crate::protocol::Header; use crate::protocol::Header;
use async_trait::async_trait;
use futures_io::AsyncWrite; use futures_io::AsyncWrite;
use std::io::Error as IoError; use std::io::Error as IoError;
#[async_trait] impl Header {
pub trait Marshal { pub async fn async_marshal(&self, s: &mut impl AsyncWrite) -> Result<(), IoError> {
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> {
todo!() todo!()
} }
} }

View File

@ -1,19 +1,9 @@
use crate::protocol::Header; use crate::protocol::Header;
use async_trait::async_trait;
use futures_io::AsyncRead; use futures_io::AsyncRead;
use thiserror::Error; use thiserror::Error;
#[async_trait] impl Header {
pub trait Unmarshal pub async fn async_unmarshal(s: &mut impl AsyncRead) -> Result<Self, UnmarshalError> {
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> {
todo!() todo!()
} }
} }