Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback inheritance, attempt 2 #1531

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions axum-extra/src/routing/spa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ use tower_service::Service;
/// - `GET /some/other/path` will serve `index.html` since there isn't another
/// route for it
/// - `GET /api/foo` will serve the `api_foo` handler function
pub struct SpaRouter<B = Body, T = (), F = fn(io::Error) -> Ready<StatusCode>> {
pub struct SpaRouter<S = (), B = Body, T = (), F = fn(io::Error) -> Ready<StatusCode>> {
paths: Arc<Paths>,
handle_error: F,
_marker: PhantomData<fn() -> (B, T)>,
_marker: PhantomData<fn() -> (S, B, T)>,
}

#[derive(Debug)]
Expand All @@ -63,7 +63,7 @@ struct Paths {
index_file: PathBuf,
}

impl<B> SpaRouter<B, (), fn(io::Error) -> Ready<StatusCode>> {
impl<S, B> SpaRouter<S, B, (), fn(io::Error) -> Ready<StatusCode>> {
/// Create a new `SpaRouter`.
///
/// Assets will be served at `GET /{serve_assets_at}` from the directory at `assets_dir`.
Expand All @@ -86,7 +86,7 @@ impl<B> SpaRouter<B, (), fn(io::Error) -> Ready<StatusCode>> {
}
}

impl<B, T, F> SpaRouter<B, T, F> {
impl<S, B, T, F> SpaRouter<S, B, T, F> {
/// Set the path to the index file.
///
/// `path` must be relative to `assets_dir` passed to [`SpaRouter::new`].
Expand Down Expand Up @@ -138,7 +138,7 @@ impl<B, T, F> SpaRouter<B, T, F> {
/// let app = Router::new().merge(spa);
/// # let _: Router = app;
/// ```
pub fn handle_error<T2, F2>(self, f: F2) -> SpaRouter<B, T2, F2> {
pub fn handle_error<T2, F2>(self, f: F2) -> SpaRouter<S, B, T2, F2> {
SpaRouter {
paths: self.paths,
handle_error: f,
Expand All @@ -147,20 +147,21 @@ impl<B, T, F> SpaRouter<B, T, F> {
}
}

impl<B, F, T> From<SpaRouter<B, T, F>> for Router<(), B>
impl<S, B, F, T> From<SpaRouter<S, B, T, F>> for Router<S, B>
where
F: Clone + Send + Sync + 'static,
HandleError<Route<B, io::Error>, F, T>: Service<Request<B>, Error = Infallible>,
<HandleError<Route<B, io::Error>, F, T> as Service<Request<B>>>::Response: IntoResponse + Send,
<HandleError<Route<B, io::Error>, F, T> as Service<Request<B>>>::Future: Send,
B: HttpBody + Send + 'static,
T: 'static,
S: Clone + Send + Sync + 'static,
{
fn from(spa: SpaRouter<B, T, F>) -> Self {
fn from(spa: SpaRouter<S, B, T, F>) -> Router<S, B> {
let assets_service = get_service(ServeDir::new(&spa.paths.assets_dir))
.handle_error(spa.handle_error.clone());

Router::new()
Router::inherit_state()
.nest_service(&spa.paths.assets_path, assets_service)
.fallback_service(
get_service(ServeFile::new(&spa.paths.index_file)).handle_error(spa.handle_error),
Expand Down Expand Up @@ -195,7 +196,7 @@ where
fn clone(&self) -> Self {
Self {
paths: self.paths.clone(),
handle_error: self.handle_error.clone(),
handle_error: self.handle_error,
_marker: self._marker,
}
}
Expand Down
192 changes: 192 additions & 0 deletions axum/src/boxed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
use std::convert::Infallible;

use crate::{body::HttpBody, handler::Handler, routing::Route, Router};

pub(crate) struct BoxedIntoRoute<S, B, E>(Box<dyn ErasedIntoRoute<S, B, E>>);

impl<S, B> BoxedIntoRoute<S, B, Infallible>
where
S: Clone + Send + Sync + 'static,
B: Send + 'static,
{
pub(crate) fn from_handler<H, T>(handler: H) -> Self
where
H: Handler<T, S, B>,
T: 'static,
{
Self(Box::new(MakeErasedHandler {
handler,
into_route: |handler, state| Route::new(Handler::with_state(handler, state)),
}))
}

pub(crate) fn from_router(router: Router<S, B>) -> Self
where
B: HttpBody + Send + 'static,
S: Clone + Send + Sync + 'static,
{
Self(Box::new(MakeErasedRouter {
router,
into_route: |router, _state| Route::new(router.into_service()),
}))
}
}

impl<S, B, E> BoxedIntoRoute<S, B, E> {
pub(crate) fn map<F, B2, E2>(self, f: F) -> BoxedIntoRoute<S, B2, E2>
where
S: 'static,
B: 'static,
E: 'static,
F: FnOnce(Route<B, E>) -> Route<B2, E2> + Clone + Send + 'static,
B2: 'static,
E2: 'static,
{
BoxedIntoRoute(Box::new(Map {
inner: self.0,
layer: Box::new(f),
}))
}

pub(crate) fn into_route(self, state: S) -> Route<B, E> {
self.0.into_route(state)
}

pub(crate) fn inherit_fallback(&mut self, fallback: Route<B>) {
self.0.inherit_fallback(fallback);
}
}

impl<S, B, E> Clone for BoxedIntoRoute<S, B, E> {
fn clone(&self) -> Self {
Self(self.0.clone_box())
}
}

pub(crate) trait ErasedIntoRoute<S, B, E>: Send {
fn clone_box(&self) -> Box<dyn ErasedIntoRoute<S, B, E>>;

fn into_route(self: Box<Self>, state: S) -> Route<B, E>;

fn inherit_fallback(&mut self, fallback: Route<B>);
}

pub(crate) struct MakeErasedHandler<H, S, B> {
pub(crate) handler: H,
pub(crate) into_route: fn(H, S) -> Route<B>,
}

impl<H, S, B> ErasedIntoRoute<S, B, Infallible> for MakeErasedHandler<H, S, B>
where
H: Clone + Send + 'static,
S: 'static,
B: 'static,
{
fn clone_box(&self) -> Box<dyn ErasedIntoRoute<S, B, Infallible>> {
Box::new(self.clone())
}

fn into_route(self: Box<Self>, state: S) -> Route<B> {
(self.into_route)(self.handler, state)
}

fn inherit_fallback(&mut self, _fallback: Route<B>) {
// handlers don't have fallbacks, nothing to do here
}
}

impl<H, S, B> Clone for MakeErasedHandler<H, S, B>
where
H: Clone,
{
fn clone(&self) -> Self {
Self {
handler: self.handler.clone(),
into_route: self.into_route,
}
}
}

pub(crate) struct MakeErasedRouter<S, B> {
pub(crate) router: Router<S, B>,
pub(crate) into_route: fn(Router<S, B>, S) -> Route<B>,
}

impl<S, B> ErasedIntoRoute<S, B, Infallible> for MakeErasedRouter<S, B>
where
S: Clone + Send + 'static,
B: 'static,
{
fn clone_box(&self) -> Box<dyn ErasedIntoRoute<S, B, Infallible>> {
Box::new(self.clone())
}

fn into_route(self: Box<Self>, state: S) -> Route<B> {
(self.into_route)(self.router, state)
}

fn inherit_fallback(&mut self, fallback: Route<B>) {
self.router.inherit_fallback(fallback);
}
}

impl<S, B> Clone for MakeErasedRouter<S, B>
where
S: Clone,
{
fn clone(&self) -> Self {
Self {
router: self.router.clone(),
into_route: self.into_route,
}
}
}

pub(crate) struct Map<S, B, E, B2, E2> {
pub(crate) inner: Box<dyn ErasedIntoRoute<S, B, E>>,
pub(crate) layer: Box<dyn LayerFn<B, E, B2, E2>>,
}

impl<S, B, E, B2, E2> ErasedIntoRoute<S, B2, E2> for Map<S, B, E, B2, E2>
where
S: 'static,
B: 'static,
E: 'static,
B2: 'static,
E2: 'static,
{
fn clone_box(&self) -> Box<dyn ErasedIntoRoute<S, B2, E2>> {
Box::new(Self {
inner: self.inner.clone_box(),
layer: self.layer.clone_box(),
})
}

fn into_route(self: Box<Self>, state: S) -> Route<B2, E2> {
(self.layer)(self.inner.into_route(state))
}

fn inherit_fallback(&mut self, fallback: Route<B2>) {
// Ideally we'd be able to do this but that doesn't work since `inner` uses body type `B`
// whereas the fallback has `B2`.
//
// Using `B2` makes sense since thats the type after the layer has been applied.
//
// However we cannot apply the layer because then we get a `Route` which we cannot
// add fallbacks to :(
self.inner.inherit_fallback(fallback);
}
}

pub(crate) trait LayerFn<B, E, B2, E2>: FnOnce(Route<B, E>) -> Route<B2, E2> + Send {
fn clone_box(&self) -> Box<dyn LayerFn<B, E, B2, E2>>;
}

impl<F, B, E, B2, E2> LayerFn<B, E, B2, E2> for F
where
F: FnOnce(Route<B, E>) -> Route<B2, E2> + Clone + Send + 'static,
{
fn clone_box(&self) -> Box<dyn LayerFn<B, E, B2, E2>> {
Box::new(self.clone())
}
}
123 changes: 0 additions & 123 deletions axum/src/handler/boxed.rs

This file was deleted.