Compare commits

...

2 commits

4 changed files with 36 additions and 11 deletions

View file

@ -23,3 +23,4 @@ tower = { version = "0.4", features = ["full"] }
[dev-dependencies]
eyre = "0.6"
tracing-subscriber = "0.3"
console-subscriber = "0.1"

View file

@ -14,15 +14,18 @@ async fn handle_req(req: Request) -> eyre::Result<Response> {
#[tokio::main]
async fn main() -> eyre::Result<()> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();
setup_logging();
let incoming = AddrIncoming::new("127.0.0.1:4567").await?;
let make_service = tower::service_fn(|addr: &AddrStream| {
tracing::debug!("Accept: {} -> {}", addr.remote_addr, addr.local_addr);
futures::future::ok::<_, std::convert::Infallible>(tower::service_fn(handle_req))
tracing::debug!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept");
let service = tower::ServiceBuilder::new()
.buffer(16)
.service_fn(handle_req);
futures::future::ok::<_, std::convert::Infallible>(service)
});
let server = Server::new(incoming).serve(make_service);
@ -30,3 +33,23 @@ async fn main() -> eyre::Result<()> {
Ok(())
}
// Don't mind this, this is just for debugging.
fn setup_logging() {
use tracing_subscriber::prelude::*;
tracing_subscriber::registry()
.with(console_subscriber::spawn())
.with(
tracing_subscriber::fmt::layer().with_filter(
tracing_subscriber::filter::Targets::new()
.with_default(tracing::Level::DEBUG)
.with_target("boitalettres", tracing::Level::TRACE)
.with_target("simple", tracing::Level::TRACE)
.with_target("tower", tracing::Level::TRACE)
.with_target("tokio_tower", tracing::Level::TRACE)
.with_target("mio", tracing::Level::TRACE),
),
)
.init();
}

View file

@ -20,7 +20,7 @@ pub enum Error<A> {
#[error("Error occured when accepting new connections")]
Accept(#[source] A),
#[error("Error occured on service creation")]
MakeService(#[source] Box<dyn std::error::Error + Send + Sync + 'static>),
MakeService(#[source] tower::BoxError),
}
#[derive(Debug, Default, Clone)]
@ -51,7 +51,7 @@ where
I: Accept,
I::Conn: AsyncRead + AsyncWrite + Unpin + Send + 'static,
S: MakeServiceRef<I::Conn, Request, Response = Response>,
S::MakeError: Into<Box<dyn std::error::Error + Send + Sync + 'static>> + std::fmt::Display,
S::MakeError: Into<tower::BoxError> + std::fmt::Display,
S::Error: std::fmt::Display,
S::Future: Send + 'static,
S::Service: Send + 'static,

View file

@ -8,7 +8,8 @@ use futures::stream::Stream;
use crate::proto::{Request, Response};
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
type Error = tower::BoxError;
type Result<T, E = Error> = std::result::Result<T, E>;
#[pin_project::pin_project]
pub struct Connection<C> {
@ -32,7 +33,7 @@ impl<C> Stream for Connection<C>
where
C: AsyncRead + Unpin,
{
type Item = Result<Request, Error>;
type Item = Result<Request>;
fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> {
use imap_codec::parse::command::command as parse_command;
@ -75,7 +76,7 @@ impl<C> Connection<C>
where
C: AsyncWrite,
{
fn poll_flush_buffer(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Result<(), Error>> {
fn poll_flush_buffer(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Result<()>> {
use bytes::Buf;
let mut this = self.project();
@ -89,7 +90,7 @@ where
Poll::Ready(Ok(()))
}
pub(crate) fn send(&mut self, item: Response) -> Result<(), Error> {
pub(crate) fn send(&mut self, item: Response) -> Result<()> {
use bytes::BufMut;
use imap_codec::codec::Encode;