Repository: tokio-rs/axum
File: axum/src/routing/mod.rs (Rust)

Code before:

    fn fallback_endpoint(self, endpoint: Endpoint<S>) -> Self {
        // TODO make this better.
        // We need the returned `Service` to be `Clone` and the function inside `service_fn` to be
        // `FnMut` so instead of just using the owned service, we do this trick with `Option`. We
        // know this will be called just once so it's fine. We're doing that so that we avoid one
        // clone inside `oneshot_inner` so that the `Router` and subsequently the `State` is not
        // cloned too much.
        tap_inner!(self, mut this => {
            _ = this.path_router.route_endpoint(
                "/",
                endpoint.clone().layer(
                    layer_fn(
                        |service: Route| {
                            let mut service = Some(service);
                            service_fn(
                                #[cfg_attr(not(feature = "matched-path"), allow(unused_mut))]
                                move |mut request: Request| {
                                    #[cfg(feature = "matched-path")]
                                    request.extensions_mut().remove::<MatchedPath>();
                                    let route = service.take().unwrap_or_else(|| {
                                        debug_assert!(
                                            false,
                                            "TakeOnceRoute called more than once; if this was not triggered by an intentional test, this should never happen. Please file an issue.",
                                        );
                                        Route::new(service_fn(|_req: Request| async move {
                                            Ok::<_, Infallible>(
                                                http::StatusCode::INTERNAL_SERVER_ERROR
                                                    .into_response(),
                                            )
                                        }))
                                    });
                                    route.oneshot_inner_owned(request)
                                }
                            )
                        }
                    )
                )
            );

            _ = this.path_router.route_endpoint(
                FALLBACK_PARAM_PATH,
                endpoint.layer(
                    layer_fn(
                        |service: Route| {
                            let mut service = Some(service);
                            service_fn(
                                #[cfg_attr(not(feature = "matched-path"), allow(unused_mut))]
                                move |mut request: Request| {
                                    #[cfg(feature = "matched-path")]
                                    request.extensions_mut().remove::<MatchedPath>();

Review comment (bug):
I think `if cfg!(debug_assertions) { panic!("..."); }` would be cleaner.

Code after:
    }

    /// Reset the fallback to its default.
    ///
    /// Useful to merge two routers with fallbacks, as [`merge`] doesn't allow
    /// both routers to have an explicit fallback. Use this method to remove the
    /// one you want to discard before merging.
    ///
    /// [`merge`]: Self::merge
    pub fn reset_fallback(self) -> Self {
        tap_inner!(self, mut this => {
            this.default_fallback = true;
            this.catch_all_fallback = Fallback::Default(Route::new(NotFound));
        })
    }

    fn fallback_endpoint(self, endpoint: Endpoint<S>) -> Self {
        // TODO make this better.
        // We need the returned `Service` to be `Clone` and the function inside `service_fn` to be
        // `FnMut` so instead of just using the owned service, we do this trick with `Option`. We
        // know this will be called just once so it's fine. We're doing that so that we avoid one
        // clone inside `oneshot_inner` so that the `Router` and subsequently the `State` is not
        // cloned too much.
        tap_inner!(self, mut this => {
            _ = this.path_router.route_endpoint(
                "/",
                endpoint.clone().layer(
                    layer_fn(
                        |service: Route| {
                            let mut service = Some(service);
                            service_fn(
                                #[cfg_attr(not(feature = "matched-path"), allow(unused_mut))]
                                move |mut request: Request| {
                                    #[cfg(feature = "matched-path")]
                                    request.extensions_mut().remove::<MatchedPath>();
                                    let route = take_route_or_internal_error(&mut service);
                                    route.oneshot_inner_owned(request)
                                }
                            )
                        }
                    )
                )
            );

            _ = this.path_router.route_endpoint(
                FALLBACK_PARAM_PATH,
                endpoint.layer(
                    layer_fn(
                        |service: Route| {
                            let mut service = Some(service);
                            service_fn(