Skip to content

Commit d226793

Browse files
oddgrdseanmonstar
authored andcommitted
fix(server): echo guide doc test errors
1 parent 35b3147 commit d226793

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

.github/workflows/check_guides.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ EOF
2626
cargo build --manifest-path "$value/Cargo.toml"
2727
fi
2828
fi
29+
2930
test_file() {
3031
echo "Testing: $f"
3132
rustdoc --edition 2018 --test $1 -L "$value/target/debug/deps"

_guides/master/server/echo.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ handle the case when someone asks for a route we don't know!
2020
Before we get started we need to add some new imports:
2121

2222
```rust
23+
# extern crate hyper;
24+
# extern crate http_body_util;
2325
use hyper::body::Frame;
2426
use hyper::{Method, StatusCode};
2527
use http_body_util::{combinators::BoxBody, BodyExt};
@@ -37,7 +39,9 @@ this we will change the type of the `Body` in our `Response` to a boxed trait ob
3739
We only care that the response body implements the [Body](https://docs.rs/http-body/1.0.0-rc1/http_body/trait.Body.html) trait, that its data is `Bytes` and its error is a `hyper::Error`.
3840

3941
```rust
40-
# use bytes::Bytes;
42+
# extern crate hyper;
43+
# extern crate http_body_util;
44+
# use hyper::body::Bytes;
4145
# use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full};
4246
# use hyper::{Method, Request, Response, StatusCode};
4347
async fn echo(
@@ -49,6 +53,7 @@ async fn echo(
4953
))),
5054
(&Method::POST, "/echo") => {
5155
// we'll be back
56+
# Ok(Response::new(req.into_body().boxed()))
5257
},
5358

5459
// Return 404 Not Found for other routes.
@@ -86,9 +91,9 @@ case will instead send back `404 Not Found`.
8691

8792
## Body Streams
8893

89-
Now let's get that echo in place. An HTTP body is a stream of
90-
`Frames`, each [Frame](https://docs.rs/http-body/1.0.0-rc1/http_body/struct.Frame.html)
91-
containing parts of the `Body` data or trailers. So rather than reading the entire `Body`
94+
Now let's get that echo in place. An HTTP body is a stream of `Frames`, each
95+
[Frame](https://docs.rs/http-body/1.0.0-rc1/http_body/struct.Frame.html) containing
96+
parts of the `Body` data or trailers. So rather than reading the entire `Body`
9297
into a buffer before sending our response, we can stream each frame as it arrives.
9398
We'll start with the simplest solution, and then make alterations exercising more complex
9499
things you can do with the `Body` streams.
@@ -97,7 +102,9 @@ First up, plain echo. Both the `Request` and the `Response` have body streams,
97102
and by default, you can easily pass the `Body` of the `Request` into a `Response`.
98103

99104
```rust
100-
# use bytes::Bytes;
105+
# extern crate hyper;
106+
# extern crate http_body_util;
107+
# use hyper::body::Bytes;
101108
# use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full};
102109
# use hyper::{Method, Request, Response, StatusCode};
103110
# async fn echo(
@@ -125,7 +132,9 @@ Next, let's add a new `/echo/uppercase` route, mapping each byte in the data `Fr
125132
of our request body to uppercase, and returning the stream in our `Response`:
126133

127134
```rust
128-
# use bytes::Bytes;
135+
# extern crate hyper;
136+
# extern crate http_body_util;
137+
# use hyper::body::Bytes;
129138
# use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full};
130139
# use hyper::body::Frame;
131140
# use hyper::{Method, Request, Response, StatusCode};
@@ -154,6 +163,11 @@ of our request body to uppercase, and returning the stream in our `Response`:
154163
# _ => unreachable!(),
155164
# }
156165
# }
166+
# fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, hyper::Error> {
167+
# Full::new(chunk.into())
168+
# .map_err(|never| match never {})
169+
# .boxed()
170+
# }
157171
# fn main() {}
158172
```
159173

@@ -176,7 +190,9 @@ We can easily turn the `Collected` body into a single `Bytes` by calling its `in
176190
method.
177191

178192
```rust
179-
# use bytes::Bytes;
193+
# extern crate hyper;
194+
# extern crate http_body_util;
195+
# use hyper::body::Bytes;
180196
# use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full};
181197
# use hyper::{Method, Request, Response, StatusCode};
182198
# async fn echo(
@@ -199,6 +215,11 @@ method.
199215
# _ => unreachable!(),
200216
# }
201217
# }
218+
# fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, hyper::Error> {
219+
# Full::new(chunk.into())
220+
# .map_err(|never| match never {})
221+
# .boxed()
222+
# }
202223
# fn main() {}
203224
```
204225

0 commit comments

Comments
 (0)