Skip to content

Commit 75e36c0

Browse files
yukiiiteruPureWhiteWu
authored andcommitted
feat(volo-http): use std::convert::Infallible instead of DynError
Signed-off-by: Yu Li <liyu.yukiteru@bytedance.com>
1 parent 0b44589 commit 75e36c0

File tree

6 files changed

+46
-31
lines changed

6 files changed

+46
-31
lines changed

volo-http/src/extract.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use std::convert::Infallible;
2+
13
use futures_util::Future;
24
use hyper::http::{Method, Uri};
35
use volo::net::Address;
46

5-
use crate::{response::Infallible, HttpContext, Params, State};
7+
use crate::{HttpContext, Params, State};
68

79
pub trait FromContext<S>: Sized {
810
fn from_context(

volo-http/src/handler.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{future::Future, marker::PhantomData};
1+
use std::{convert::Infallible, future::Future, marker::PhantomData};
22

33
use hyper::body::Incoming;
44
use motore::Service;
@@ -8,7 +8,7 @@ use crate::{
88
macros::{all_the_tuples, all_the_tuples_no_last_special_case},
99
request::FromRequest,
1010
response::{IntoResponse, Response},
11-
DynError, DynService, HttpContext,
11+
DynService, HttpContext,
1212
};
1313

1414
pub trait Handler<T, S>: Sized {
@@ -126,7 +126,7 @@ where
126126
cx: &mut HttpContext,
127127
req: Incoming,
128128
state: S,
129-
) -> Result<Response, DynError> {
129+
) -> Result<Response, Infallible> {
130130
self.0.into_route(state).call(cx, req).await
131131
}
132132
}
@@ -233,7 +233,7 @@ where
233233
S: Sync,
234234
{
235235
type Response = Response;
236-
type Error = DynError;
236+
type Error = Infallible;
237237

238238
async fn call<'s, 'cx>(
239239
&'s self,

volo-http/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub mod server;
99

1010
mod macros;
1111

12+
use std::convert::Infallible;
13+
1214
pub use bytes::Bytes;
1315
pub use hyper::{
1416
body::Incoming,
@@ -19,12 +21,11 @@ pub use volo::net::Address;
1921
pub use crate::{
2022
param::Params,
2123
request::{Json, Request},
22-
response::{Infallible, Response},
24+
response::Response,
2325
server::Server,
2426
};
2527

26-
pub type DynService = motore::BoxCloneService<HttpContext, Incoming, Response, DynError>;
27-
pub type DynError = Box<dyn std::error::Error + Send + Sync>;
28+
pub type DynService = motore::BoxCloneService<HttpContext, Incoming, Response, Infallible>;
2829

2930
#[derive(Debug, Default, Clone, Copy)]
3031
pub struct State<S>(pub S);

volo-http/src/response.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
convert::Infallible,
23
ops::{Deref, DerefMut},
34
pin::Pin,
45
task::{Context, Poll},
@@ -12,15 +13,16 @@ use hyper::{
1213
};
1314
use pin_project::pin_project;
1415

15-
use crate::DynError;
16-
17-
pub struct Response(pub(crate) hyper::http::Response<RespBody>);
18-
pub struct Infallible;
16+
pub struct Response(hyper::http::Response<RespBody>);
1917

2018
impl Response {
2119
pub fn builder() -> Builder {
2220
Builder::new()
2321
}
22+
23+
pub(crate) fn inner(self) -> hyper::http::Response<RespBody> {
24+
self.0
25+
}
2426
}
2527

2628
impl Deref for Response {
@@ -52,7 +54,7 @@ pub struct RespBody {
5254
impl Body for RespBody {
5355
type Data = Bytes;
5456

55-
type Error = DynError;
57+
type Error = Infallible;
5658

5759
fn poll_frame(
5860
self: Pin<&mut Self>,

volo-http/src/route.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::{collections::HashMap, convert::Infallible};
22

33
use hyper::{
44
body::Incoming,
@@ -9,7 +9,7 @@ use motore::{layer::Layer, service::Service};
99
use crate::{
1010
handler::{DynHandler, Handler},
1111
response::IntoResponse,
12-
DynError, DynService, HttpContext, Response,
12+
DynService, HttpContext, Response,
1313
};
1414

1515
// The `matchit::Router` cannot be converted to `Iterator`, so using
@@ -72,7 +72,7 @@ where
7272
pub fn layer<L>(self, l: L) -> Self
7373
where
7474
L: Layer<DynService> + Clone + Send + Sync + 'static,
75-
L::Service: Service<HttpContext, Incoming, Response = Response, Error = DynError>
75+
L::Service: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
7676
+ Clone
7777
+ Send
7878
+ Sync
@@ -120,7 +120,7 @@ where
120120
impl Service<HttpContext, Incoming> for Router<()> {
121121
type Response = Response;
122122

123-
type Error = DynError;
123+
type Error = Infallible;
124124

125125
async fn call<'s, 'cx>(
126126
&'s self,
@@ -179,7 +179,7 @@ where
179179
pub fn layer<L>(self, l: L) -> Self
180180
where
181181
L: Layer<DynService> + Clone + Send + Sync + 'static,
182-
L::Service: Service<HttpContext, Incoming, Response = Response, Error = DynError>
182+
L::Service: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
183183
+ Clone
184184
+ Send
185185
+ Sync
@@ -246,7 +246,7 @@ where
246246
cx: &'cx mut HttpContext,
247247
req: Incoming,
248248
state: S,
249-
) -> Result<Response, DynError>
249+
) -> Result<Response, Infallible>
250250
where
251251
S: 'cx,
252252
{
@@ -355,7 +355,7 @@ where
355355

356356
pub fn from_service<Srv>(srv: Srv) -> MethodEndpoint<S>
357357
where
358-
Srv: Service<HttpContext, Incoming, Response = Response, Error = DynError>
358+
Srv: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
359359
+ Clone
360360
+ Send
361361
+ Sync
@@ -411,7 +411,7 @@ where
411411

412412
pub fn from_service<Srv>(srv: Srv) -> Fallback<S>
413413
where
414-
Srv: Service<HttpContext, Incoming, Response = Response, Error = DynError>
414+
Srv: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
415415
+ Clone
416416
+ Send
417417
+ Sync
@@ -433,7 +433,7 @@ where
433433
pub(crate) fn layer<L>(self, l: L) -> Self
434434
where
435435
L: Layer<DynService> + Clone + Send + Sync + 'static,
436-
L::Service: Service<HttpContext, Incoming, Response = Response, Error = DynError>
436+
L::Service: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
437437
+ Clone
438438
+ Send
439439
+ Sync
@@ -454,7 +454,7 @@ where
454454
cx: &'cx mut HttpContext,
455455
req: Incoming,
456456
state: S,
457-
) -> Result<Response, DynError>
457+
) -> Result<Response, Infallible>
458458
where
459459
S: 'cx,
460460
{
@@ -476,7 +476,7 @@ where
476476

477477
pub fn from_service<Srv, S>(srv: Srv) -> MethodEndpoint<S>
478478
where
479-
Srv: Service<HttpContext, Incoming, Response = Response, Error = DynError>
479+
Srv: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
480480
+ Clone
481481
+ Send
482482
+ Sync
@@ -491,7 +491,7 @@ struct RouteForStatusCode(StatusCode);
491491

492492
impl Service<HttpContext, Incoming> for RouteForStatusCode {
493493
type Response = Response;
494-
type Error = DynError;
494+
type Error = Infallible;
495495

496496
async fn call<'s, 'cx>(
497497
&'s self,

volo-http/src/server.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
convert::Infallible,
23
sync::{atomic::Ordering, Arc},
34
time::Duration,
45
};
@@ -10,7 +11,11 @@ use tokio::sync::Notify;
1011
use tracing::{info, trace};
1112
use volo::net::{conn::Conn, incoming::Incoming, Address, MakeIncoming};
1213

13-
use crate::{param::Params, response::Response, DynError, HttpContext};
14+
use crate::{
15+
param::Params,
16+
response::{IntoResponse, RespBody, Response},
17+
HttpContext,
18+
};
1419

1520
pub struct Server<App> {
1621
app: Arc<App>,
@@ -26,8 +31,10 @@ impl<A> Clone for Server<A> {
2631

2732
impl<App> Server<App>
2833
where
29-
App: motore::Service<HttpContext, BodyIncoming, Response = Response> + Send + Sync + 'static,
30-
App::Error: Into<DynError>,
34+
App: motore::Service<HttpContext, BodyIncoming, Response = Response, Error = Infallible>
35+
+ Send
36+
+ Sync
37+
+ 'static,
3138
{
3239
pub fn new(app: App) -> Self {
3340
Self { app: Arc::new(app) }
@@ -157,12 +164,11 @@ async fn handle_conn<S>(
157164
conn_cnt: Arc<std::sync::atomic::AtomicUsize>,
158165
peer: Address,
159166
) where
160-
S: motore::Service<HttpContext, BodyIncoming, Response = Response>
167+
S: motore::Service<HttpContext, BodyIncoming, Response = Response, Error = Infallible>
161168
+ Clone
162169
+ Send
163170
+ Sync
164171
+ 'static,
165-
S::Error: Into<DynError>,
166172
{
167173
let notified = exit_notify.notified();
168174
tokio::pin!(notified);
@@ -186,7 +192,11 @@ async fn handle_conn<S>(
186192
inner: Vec::with_capacity(0),
187193
},
188194
};
189-
service.call(&mut cx, req).await.map(|resp| resp.0)
195+
let resp = match service.call(&mut cx, req).await {
196+
Ok(resp) => resp,
197+
Err(inf) => inf.into_response(),
198+
};
199+
Ok::<hyper::http::Response<RespBody>, Infallible>(resp.inner())
190200
}
191201
}),
192202
);

0 commit comments

Comments
 (0)