Skip to content

Commit 4374c9e

Browse files
authored
Merge pull request #37 from tirr-c/copyable-next
Impl Copy for Next
2 parents d2b121c + 6903c7b commit 4374c9e

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

examples/next_reuse.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#![feature(async_await)]
2+
3+
use futures::future::BoxFuture;
4+
use futures::io::AsyncReadExt;
5+
use surf::middleware::{Body, HttpClient, Middleware, Next, Request, Response};
6+
7+
struct Doubler;
8+
9+
impl<C: HttpClient> Middleware<C> for Doubler {
10+
fn handle<'a>(
11+
&'a self,
12+
req: Request,
13+
client: C,
14+
next: Next<'a, C>,
15+
) -> BoxFuture<'a, Result<Response, surf::Exception>> {
16+
if req.method().is_safe() {
17+
let mut new_req = Request::new(Body::empty());
18+
*new_req.method_mut() = req.method().clone();
19+
*new_req.uri_mut() = req.uri().clone();
20+
*new_req.version_mut() = req.version().clone();
21+
*new_req.headers_mut() = req.headers().clone();
22+
Box::pin(async move {
23+
let mut buf = Vec::new();
24+
let (res1, res2) =
25+
futures::future::join(next.run(req, client.clone()), next.run(new_req, client))
26+
.await;
27+
28+
let res = res1?;
29+
let mut body = res.into_body();
30+
body.read_to_end(&mut buf).await?;
31+
32+
let mut res = res2?;
33+
let mut body = std::mem::replace(res.body_mut(), Body::empty());
34+
body.read_to_end(&mut buf).await?;
35+
36+
*res.body_mut() = Body::from(buf);
37+
Ok(res)
38+
})
39+
} else {
40+
next.run(req, client)
41+
}
42+
}
43+
}
44+
45+
#[runtime::main]
46+
async fn main() -> Result<(), surf::Exception> {
47+
femme::start(log::LevelFilter::Info)?;
48+
let mut res = surf::get("https://httpbin.org/get")
49+
.middleware(Doubler {})
50+
.await?;
51+
dbg!(&res);
52+
let body = res.body_bytes().await?;
53+
let body = String::from_utf8_lossy(&body);
54+
println!("{}", body);
55+
Ok(())
56+
}

src/middleware/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ pub struct Next<'a, C: HttpClient> {
9696
+ Sync),
9797
}
9898

99+
impl<C: HttpClient> Clone for Next<'_, C> {
100+
fn clone(&self) -> Self {
101+
Self {
102+
next_middleware: self.next_middleware,
103+
endpoint: self.endpoint,
104+
}
105+
}
106+
}
107+
108+
impl<C: HttpClient> Copy for Next<'_, C> {}
109+
99110
impl<'a, C: HttpClient> Next<'a, C> {
100111
/// Create a new instance
101112
pub fn new(

0 commit comments

Comments
 (0)