Files
axum/src/handler/into_service.rs
T

81 lines
2.0 KiB
Rust
Raw Normal View History

2021-08-19 21:16:44 +02:00
use super::Handler;
use crate::body::BoxBody;
2021-08-19 21:16:44 +02:00
use http::{Request, Response};
use std::{
convert::Infallible,
fmt,
marker::PhantomData,
task::{Context, Poll},
};
2021-08-21 15:01:30 +02:00
use tower_service::Service;
2021-08-19 21:16:44 +02:00
/// An adapter that makes a [`Handler`] into a [`Service`].
///
/// Created with [`Handler::into_service`].
pub struct IntoService<H, B, T> {
2021-08-19 21:16:44 +02:00
handler: H,
_marker: PhantomData<fn() -> (B, T)>,
2021-08-19 21:16:44 +02:00
}
#[test]
fn traits() {
use crate::tests::*;
assert_send::<IntoService<(), NotSendSync, NotSendSync>>();
assert_sync::<IntoService<(), NotSendSync, NotSendSync>>();
}
impl<H, B, T> IntoService<H, B, T> {
2021-08-19 21:16:44 +02:00
pub(super) fn new(handler: H) -> Self {
Self {
handler,
_marker: PhantomData,
}
}
}
impl<H, B, T> fmt::Debug for IntoService<H, B, T> {
2021-08-19 21:16:44 +02:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("IntoService")
.field(&format_args!("..."))
.finish()
}
}
impl<H, B, T> Clone for IntoService<H, B, T>
2021-08-19 21:16:44 +02:00
where
H: Clone,
{
fn clone(&self) -> Self {
Self {
handler: self.handler.clone(),
_marker: PhantomData,
}
}
}
impl<H, T, B> Service<Request<B>> for IntoService<H, B, T>
2021-08-19 21:16:44 +02:00
where
H: Handler<B, T> + Clone + Send + 'static,
2021-08-19 21:16:44 +02:00
B: Send + 'static,
{
type Response = Response<BoxBody>;
type Error = Infallible;
type Future = super::future::IntoServiceFuture;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
// `IntoService` can only be constructed from async functions which are always ready, or from
// `Layered` which bufferes in `<Layered as Handler>::call` and is therefore also always
// ready.
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request<B>) -> Self::Future {
use futures_util::future::FutureExt;
2021-08-19 21:16:44 +02:00
let handler = self.handler.clone();
let future = Handler::call(handler, req).map(Ok::<_, Infallible> as _);
2021-08-19 21:16:44 +02:00
super::future::IntoServiceFuture { future }
}
}