1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::{error::Error as StdError, fmt::Display};
use futures_util::future::BoxFuture;
use tower::{util::BoxCloneService, Service, ServiceExt};
use crate::{box_error, request::BoxRequest, response::BoxResponse, BoxError};
use super::transport::TransportError;
#[derive(Clone)]
pub struct BoxedTransport {
inner: BoxCloneService<BoxRequest, BoxResponse, TransportError<BoxedTransportError>>,
}
impl BoxedTransport {
pub fn new<Svc, SvcErr>(svc: Svc) -> Self
where
Svc: Service<BoxRequest, Response = BoxResponse, Error = TransportError<SvcErr>>
+ Send
+ Clone
+ 'static,
Svc::Future: Send,
SvcErr: StdError + Sync + Send + 'static,
{
let svc = svc.map_err(|err| match err {
TransportError::GenericClient(err) => TransportError::GenericClient(err),
TransportError::Transport(err) => {
TransportError::Transport(BoxedTransportError::new(box_error(err)))
}
});
Self {
inner: BoxCloneService::new(svc),
}
}
}
impl Service<BoxRequest> for BoxedTransport {
type Response = BoxResponse;
type Error = TransportError<BoxedTransportError>;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
fn poll_ready(
&mut self,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
Service::poll_ready(&mut self.inner, cx)
}
fn call(&mut self, req: BoxRequest) -> Self::Future {
Service::call(&mut self.inner, req)
}
}
#[derive(Debug)]
pub struct BoxedTransportError {
inner: BoxError,
}
impl BoxedTransportError {
pub(super) fn new(err: BoxError) -> Self {
Self { inner: err }
}
pub fn into_box_error(self) -> BoxError {
self.inner
}
}
impl Display for BoxedTransportError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.inner, f)
}
}
impl StdError for BoxedTransportError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(self.inner.as_ref())
}
}