-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathfuture.rs
More file actions
143 lines (123 loc) · 3.82 KB
/
Copy pathfuture.rs
File metadata and controls
143 lines (123 loc) · 3.82 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::{
pin::Pin,
task::{Context, Poll},
};
use http::{Request as HttpRequest, Response as HttpResponse};
use pin_project_lite::pin_project;
use tower::util::Oneshot;
use url::Url;
use super::{
Response,
aliases::{BoxedClientService, GenericClientService},
};
use crate::{
Body, Error,
client::{body, layer::redirect::RequestUri},
core::{body::Incoming, client::future::ResponseFuture as CoreResponseFuture},
error::BoxError,
into_url::IntoUrlSealed,
};
macro_rules! take_url {
($url:ident) => {
match $url.take() {
Some(url) => url,
None => {
return Poll::Ready(Err(Error::builder("URL already taken in Pending::Request")))
}
}
};
}
macro_rules! take_err {
($err:ident) => {
match $err.take() {
Some(err) => err,
None => Error::builder("Error already taken in Error"),
}
};
}
pin_project! {
#[project = PendingProj]
pub enum Pending {
BoxedRequest {
url: Option<Url>,
#[pin]
fut: Oneshot<BoxedClientService, HttpRequest<Body>>,
},
GenericRequest {
url: Option<Url>,
fut: Pin<Box<Oneshot<GenericClientService, HttpRequest<Body>>>>,
},
Error {
error: Option<Error>,
},
}
}
pin_project! {
#[project = CorePendingProj]
pub enum CorePending {
Request {
#[pin]
fut: CoreResponseFuture,
},
Error {
error: Option<Error>,
},
}
}
// ======== Pending impl ========
impl Future for Pending {
type Output = Result<Response, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let (url, res) = match self.project() {
PendingProj::BoxedRequest { url, fut } => (url, fut.poll(cx)),
PendingProj::GenericRequest { url, fut } => (url, fut.as_mut().poll(cx)),
PendingProj::Error { error } => return Poll::Ready(Err(take_err!(error))),
};
let res = match res {
Poll::Ready(Ok(res)) => res.map(body::boxed),
Poll::Ready(Err(err)) => {
let mut err = match err.downcast::<Error>() {
Ok(err) => *err,
Err(e) => Error::request(e),
};
if err.url().is_none() {
err = err.with_url(take_url!(url));
}
return Poll::Ready(Err(err));
}
Poll::Pending => return Poll::Pending,
};
if let Some(uri) = res.extensions().get::<RequestUri>() {
*url = Some(IntoUrlSealed::into_url(uri.0.to_string())?);
}
Poll::Ready(Ok(Response::new(res, take_url!(url))))
}
}
// ======== CorePending impl ========
impl Future for CorePending {
type Output = Result<HttpResponse<Incoming>, BoxError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.project() {
CorePendingProj::Request { fut } => match fut.poll(cx) {
Poll::Ready(Ok(res)) => Poll::Ready(Ok(res)),
Poll::Ready(Err(err)) => Poll::Ready(Err(err.into())),
Poll::Pending => Poll::Pending,
},
CorePendingProj::Error { error } => Poll::Ready(Err(take_err!(error).into())),
}
}
}
#[cfg(test)]
mod test {
#[test]
fn test_future_size() {
let s = std::mem::size_of::<super::Pending>();
assert!(s <= 360, "size_of::<Pending>() == {s}, too big");
}
#[tokio::test]
async fn error_has_url() {
let u = "http://does.not.exist.local/ever";
let err = crate::Client::new().get(u).send().await.unwrap_err();
assert_eq!(err.url().map(AsRef::as_ref), Some(u), "{err:?}");
}
}