/rust/registry/src/index.crates.io-1949cf8c6b5b557f/axum-0.8.9/src/boxed.rs
Line | Count | Source |
1 | | use std::{convert::Infallible, fmt}; |
2 | | |
3 | | use crate::extract::Request; |
4 | | use tower::Service; |
5 | | |
6 | | use crate::{ |
7 | | handler::Handler, |
8 | | routing::{future::RouteFuture, Route}, |
9 | | Router, |
10 | | }; |
11 | | |
12 | | pub(crate) struct BoxedIntoRoute<S, E>(Box<dyn ErasedIntoRoute<S, E>>); |
13 | | |
14 | | impl<S> BoxedIntoRoute<S, Infallible> |
15 | | where |
16 | | S: Clone + Send + Sync + 'static, |
17 | | { |
18 | 0 | pub(crate) fn from_handler<H, T>(handler: H) -> Self |
19 | 0 | where |
20 | 0 | H: Handler<T, S>, |
21 | 0 | T: 'static, |
22 | | { |
23 | 0 | Self(Box::new(MakeErasedHandler { |
24 | 0 | handler, |
25 | 0 | into_route: |handler, state| Route::new(Handler::with_state(handler, state)), Unexecuted instantiation: <axum::boxed::BoxedIntoRoute<(), core::convert::Infallible>>::from_handler::<tonic::service::router::unimplemented, ((),)>::{closure#0}Unexecuted instantiation: <axum::boxed::BoxedIntoRoute<_, core::convert::Infallible>>::from_handler::<_, _>::{closure#0} |
26 | | })) |
27 | 0 | } Unexecuted instantiation: <axum::boxed::BoxedIntoRoute<(), core::convert::Infallible>>::from_handler::<tonic::service::router::unimplemented, ((),)> Unexecuted instantiation: <axum::boxed::BoxedIntoRoute<_, core::convert::Infallible>>::from_handler::<_, _> |
28 | | } |
29 | | |
30 | | impl<S, E> BoxedIntoRoute<S, E> { |
31 | 0 | pub(crate) fn map<F, E2>(self, f: F) -> BoxedIntoRoute<S, E2> |
32 | 0 | where |
33 | 0 | S: 'static, |
34 | 0 | E: 'static, |
35 | 0 | F: FnOnce(Route<E>) -> Route<E2> + Clone + Send + Sync + 'static, |
36 | 0 | E2: 'static, |
37 | | { |
38 | 0 | BoxedIntoRoute(Box::new(Map { |
39 | 0 | inner: self.0, |
40 | 0 | layer: Box::new(f), |
41 | 0 | })) |
42 | 0 | } |
43 | | |
44 | 0 | pub(crate) fn into_route(self, state: S) -> Route<E> { |
45 | 0 | self.0.into_route(state) |
46 | 0 | } |
47 | | } |
48 | | |
49 | | impl<S, E> Clone for BoxedIntoRoute<S, E> { |
50 | 0 | fn clone(&self) -> Self { |
51 | 0 | Self(self.0.clone_box()) |
52 | 0 | } |
53 | | } |
54 | | |
55 | | impl<S, E> fmt::Debug for BoxedIntoRoute<S, E> { |
56 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
57 | 0 | f.debug_tuple("BoxedIntoRoute").finish() |
58 | 0 | } |
59 | | } |
60 | | |
61 | | pub(crate) trait ErasedIntoRoute<S, E>: Send + Sync { |
62 | | fn clone_box(&self) -> Box<dyn ErasedIntoRoute<S, E>>; |
63 | | |
64 | | fn into_route(self: Box<Self>, state: S) -> Route<E>; |
65 | | |
66 | | #[allow(dead_code)] |
67 | | fn call_with_state(self: Box<Self>, request: Request, state: S) -> RouteFuture<E>; |
68 | | } |
69 | | |
70 | | pub(crate) struct MakeErasedHandler<H, S> { |
71 | | pub(crate) handler: H, |
72 | | pub(crate) into_route: fn(H, S) -> Route, |
73 | | } |
74 | | |
75 | | impl<H, S> ErasedIntoRoute<S, Infallible> for MakeErasedHandler<H, S> |
76 | | where |
77 | | H: Clone + Send + Sync + 'static, |
78 | | S: 'static, |
79 | | { |
80 | 0 | fn clone_box(&self) -> Box<dyn ErasedIntoRoute<S, Infallible>> { |
81 | 0 | Box::new(self.clone()) |
82 | 0 | } Unexecuted instantiation: <axum::boxed::MakeErasedHandler<tonic::service::router::unimplemented, ()> as axum::boxed::ErasedIntoRoute<(), core::convert::Infallible>>::clone_box Unexecuted instantiation: <axum::boxed::MakeErasedHandler<_, _> as axum::boxed::ErasedIntoRoute<_, core::convert::Infallible>>::clone_box |
83 | | |
84 | 0 | fn into_route(self: Box<Self>, state: S) -> Route { |
85 | 0 | (self.into_route)(self.handler, state) |
86 | 0 | } Unexecuted instantiation: <axum::boxed::MakeErasedHandler<tonic::service::router::unimplemented, ()> as axum::boxed::ErasedIntoRoute<(), core::convert::Infallible>>::into_route Unexecuted instantiation: <axum::boxed::MakeErasedHandler<_, _> as axum::boxed::ErasedIntoRoute<_, core::convert::Infallible>>::into_route |
87 | | |
88 | 0 | fn call_with_state(self: Box<Self>, request: Request, state: S) -> RouteFuture<Infallible> { |
89 | 0 | self.into_route(state).call(request) |
90 | 0 | } Unexecuted instantiation: <axum::boxed::MakeErasedHandler<tonic::service::router::unimplemented, ()> as axum::boxed::ErasedIntoRoute<(), core::convert::Infallible>>::call_with_state Unexecuted instantiation: <axum::boxed::MakeErasedHandler<_, _> as axum::boxed::ErasedIntoRoute<_, core::convert::Infallible>>::call_with_state |
91 | | } |
92 | | |
93 | | impl<H, S> Clone for MakeErasedHandler<H, S> |
94 | | where |
95 | | H: Clone, |
96 | | { |
97 | 0 | fn clone(&self) -> Self { |
98 | 0 | Self { |
99 | 0 | handler: self.handler.clone(), |
100 | 0 | into_route: self.into_route, |
101 | 0 | } |
102 | 0 | } Unexecuted instantiation: <axum::boxed::MakeErasedHandler<tonic::service::router::unimplemented, ()> as core::clone::Clone>::clone Unexecuted instantiation: <axum::boxed::MakeErasedHandler<_, _> as core::clone::Clone>::clone |
103 | | } |
104 | | |
105 | | #[allow(dead_code)] |
106 | | pub(crate) struct MakeErasedRouter<S> { |
107 | | pub(crate) router: Router<S>, |
108 | | pub(crate) into_route: fn(Router<S>, S) -> Route, |
109 | | } |
110 | | |
111 | | impl<S> ErasedIntoRoute<S, Infallible> for MakeErasedRouter<S> |
112 | | where |
113 | | S: Clone + Send + Sync + 'static, |
114 | | { |
115 | 0 | fn clone_box(&self) -> Box<dyn ErasedIntoRoute<S, Infallible>> { |
116 | 0 | Box::new(self.clone()) |
117 | 0 | } |
118 | | |
119 | 0 | fn into_route(self: Box<Self>, state: S) -> Route { |
120 | 0 | (self.into_route)(self.router, state) |
121 | 0 | } |
122 | | |
123 | 0 | fn call_with_state(self: Box<Self>, request: Request, state: S) -> RouteFuture<Infallible> { |
124 | 0 | self.router.call_with_state(request, state) |
125 | 0 | } |
126 | | } |
127 | | |
128 | | impl<S> Clone for MakeErasedRouter<S> |
129 | | where |
130 | | S: Clone, |
131 | | { |
132 | 0 | fn clone(&self) -> Self { |
133 | 0 | Self { |
134 | 0 | router: self.router.clone(), |
135 | 0 | into_route: self.into_route, |
136 | 0 | } |
137 | 0 | } |
138 | | } |
139 | | |
140 | | pub(crate) struct Map<S, E, E2> { |
141 | | pub(crate) inner: Box<dyn ErasedIntoRoute<S, E>>, |
142 | | pub(crate) layer: Box<dyn LayerFn<E, E2>>, |
143 | | } |
144 | | |
145 | | impl<S, E, E2> ErasedIntoRoute<S, E2> for Map<S, E, E2> |
146 | | where |
147 | | S: 'static, |
148 | | E: 'static, |
149 | | E2: 'static, |
150 | | { |
151 | 0 | fn clone_box(&self) -> Box<dyn ErasedIntoRoute<S, E2>> { |
152 | 0 | Box::new(Self { |
153 | 0 | inner: self.inner.clone_box(), |
154 | 0 | layer: self.layer.clone_box(), |
155 | 0 | }) |
156 | 0 | } |
157 | | |
158 | 0 | fn into_route(self: Box<Self>, state: S) -> Route<E2> { |
159 | 0 | (self.layer)(self.inner.into_route(state)) |
160 | 0 | } |
161 | | |
162 | 0 | fn call_with_state(self: Box<Self>, request: Request, state: S) -> RouteFuture<E2> { |
163 | 0 | (self.layer)(self.inner.into_route(state)).call(request) |
164 | 0 | } |
165 | | } |
166 | | |
167 | | pub(crate) trait LayerFn<E, E2>: FnOnce(Route<E>) -> Route<E2> + Send + Sync { |
168 | | fn clone_box(&self) -> Box<dyn LayerFn<E, E2>>; |
169 | | } |
170 | | |
171 | | impl<F, E, E2> LayerFn<E, E2> for F |
172 | | where |
173 | | F: FnOnce(Route<E>) -> Route<E2> + Clone + Send + Sync + 'static, |
174 | | { |
175 | 0 | fn clone_box(&self) -> Box<dyn LayerFn<E, E2>> { |
176 | 0 | Box::new(self.clone()) |
177 | 0 | } |
178 | | } |