Skip to content

Commit 8195ed6

Browse files
bIgBVcarllerche
authored andcommitted
Use rust-skeptic for doc tests (tokio-rs#296)
1 parent bafcab7 commit 8195ed6

File tree

19 files changed

+97
-20
lines changed

19 files changed

+97
-20
lines changed

ci/test.sh

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
11
#!/bin/sh
22
set -ex
33

4-
if [ ! -d tmp ]; then
5-
cargo new tmp
6-
cat >> tmp/Cargo.toml <<-EOF
7-
futures = "0.1.18"
8-
tokio = "0.1.5"
9-
EOF
10-
cargo build --manifest-path tmp/Cargo.toml
11-
fi
12-
13-
rand=$(ls tmp/target/debug/deps/librand-*.rlib | head -1)
14-
echo $rand
15-
for f in $(git ls-files | grep 'md$' | grep -v 'legacy'); do
16-
echo "$f"
17-
rustdoc --edition 2018 --test "$f" -L tmp/target/debug/deps --extern "rand=$rand"
18-
done
4+
cd doc-test && cargo test

content/docs/futures/basic.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ impl Future for HelloWorld {
4949
Ok(Async::Ready("hello world".to_string()))
5050
}
5151
}
52+
53+
# fn main() {}
5254
```
5355

5456
The `Item` and `Error` associated types define the types returned by the future
@@ -112,6 +114,8 @@ where
112114
Ok(Async::Ready(()))
113115
}
114116
}
117+
118+
# fn main() {}
115119
```
116120

117121
The `Display` takes a future that yields items that can be displayed. When it is
@@ -143,8 +147,10 @@ extern crate tokio;
143147
# }
144148
# }
145149

150+
# fn main() {
146151
let future = Display(HelloWorld);
147152
tokio::run(future);
153+
# }
148154
```
149155

150156
Running this results in "hello world" being outputted to standard out.

content/docs/futures/combinators.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ fn get_ok_data() -> Result<Vec<Data>, io::Error> {
292292

293293
Ok(dst)
294294
}
295+
296+
# fn main() {}
295297
```
296298

297299
This works because the closure passed to `and_then` is able to obtain a mutable
@@ -375,6 +377,8 @@ fn print_multi() -> impl Future<Item = (), Error = io::Error> {
375377
future::join_all(futures)
376378
.map(|_| ())
377379
}
380+
381+
# fn main() {}
378382
```
379383

380384
## Returning futures
@@ -399,11 +403,13 @@ fn with_future<T: Future<Item = String>>(f: T) {
399403
# drop(f);
400404
}
401405

406+
# fn main() {
402407
let my_future = get_message().map(|message| {
403408
format!("MESSAGE = {}", message)
404409
});
405410

406411
with_future(my_future);
412+
# }
407413
```
408414

409415
However, for returning futures, it isn't as simple. There are a few options with
@@ -428,6 +434,8 @@ fn add_10<F>(f: F) -> impl Future<Item = i32, Error = F::Error>
428434
{
429435
f.map(|i| i + 10)
430436
}
437+
438+
# fn main() {}
431439
```
432440

433441
The `add_10` function has a return type that is "something that implements

content/docs/futures/spawning.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern crate futures;
2727

2828
use futures::future::lazy;
2929

30+
# fn main() {
3031
tokio::run(lazy(|| {
3132
for i in 0..4 {
3233
tokio::spawn(lazy(move || {
@@ -37,6 +38,7 @@ tokio::run(lazy(|| {
3738

3839
Ok(())
3940
}));
41+
# }
4042
```
4143

4244
The `tokio::run` function will block until the the future passed to `run`
@@ -70,6 +72,7 @@ use futures::Future;
7072
use futures::future::lazy;
7173
use futures::sync::oneshot;
7274

75+
# fn main() {
7376
tokio::run(lazy(|| {
7477
let (tx, rx) = oneshot::channel();
7578

@@ -84,6 +87,7 @@ tokio::run(lazy(|| {
8487
})
8588
.map_err(|e| println!("error = {:?}", e))
8689
}));
90+
# }
8791
```
8892

8993
And `mpsc` is good for sending a stream of values to another task:
@@ -96,6 +100,7 @@ use futures::{stream, Future, Stream, Sink};
96100
use futures::future::lazy;
97101
use futures::sync::mpsc;
98102

103+
# fn main() {
99104
tokio::run(lazy(|| {
100105
let (tx, rx) = mpsc::channel(1_024);
101106

@@ -112,6 +117,7 @@ tokio::run(lazy(|| {
112117
Ok(())
113118
})
114119
}));
120+
# }
115121
```
116122

117123
These two message passing primitives will also be used in the examples below to
@@ -154,6 +160,7 @@ use tokio::io;
154160
use tokio::net::TcpListener;
155161
use futures::{Future, Stream};
156162

163+
# fn main() {
157164
let addr = "127.0.0.1:0".parse().unwrap();
158165
let listener = TcpListener::bind(&addr).unwrap();
159166

@@ -179,6 +186,7 @@ tokio::run({
179186
.map_err(|e| println!("listener error = {:?}", e))
180187
});
181188
# }
189+
# }
182190
```
183191

184192
The listener task and the tasks that process each socket are completely
@@ -270,6 +278,7 @@ fn bg_task(rx: mpsc::Receiver<usize>)
270278
.map(|_| ())
271279
}
272280

281+
# fn main() {
273282
# if false {
274283
// Start the application
275284
tokio::run(lazy(|| {
@@ -310,6 +319,7 @@ tokio::run(lazy(|| {
310319
.map_err(|e| println!("listener error = {:?}", e))
311320
}));
312321
# }
322+
# }
313323
```
314324

315325
## Coordinating access to a resource
@@ -390,6 +400,7 @@ fn rtt(tx: mpsc::Sender<Message>)
390400
})
391401
}
392402

403+
# fn main() {
393404
# if false {
394405
// Start the application
395406
tokio::run(lazy(|| {
@@ -415,6 +426,7 @@ tokio::run(lazy(|| {
415426
Ok(())
416427
}));
417428
# }
429+
# }
418430
```
419431

420432
# When not to spawn tasks

content/docs/futures/streams.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ impl Stream for Fibonacci {
100100
Ok(Async::Ready(Some(curr)))
101101
}
102102
}
103+
104+
# fn main() {}
103105
```
104106

105107
To use the stream, a future must be built that consumes it. The following future
@@ -160,17 +162,19 @@ extern crate tokio;
160162
# extern crate futures;
161163
# struct Fibonacci;
162164
# impl Fibonacci { fn new() { } }
163-
# struct Display10<T> { v: T };
165+
# struct Display10<T> { v: T }
164166
# impl<T> Display10<T> {
165167
# fn new(_: T) -> futures::future::FutureResult<(), ()> {
166168
# futures::future::ok(())
167169
# }
168170
# }
169171

172+
# fn main() {
170173
let fib = Fibonacci::new();
171174
let display = Display10::new(fib);
172175

173176
tokio::run(display);
177+
# }
174178
```
175179

176180
## Getting asynchronous
@@ -245,7 +249,7 @@ extern crate tokio;
245249
# extern crate futures;
246250
# struct Fibonacci;
247251
# impl Fibonacci { fn new(dur: Duration) { } }
248-
# struct Display10<T> { v: T };
252+
# struct Display10<T> { v: T }
249253
# impl<T> Display10<T> {
250254
# fn new(_: T) -> futures::future::FutureResult<(), ()> {
251255
# futures::future::ok(())
@@ -254,10 +258,12 @@ extern crate tokio;
254258

255259
use std::time::Duration;
256260

261+
# fn main() {
257262
let fib = Fibonacci::new(Duration::from_secs(1));
258263
let display = Display10::new(fib);
259264

260265
tokio::run(display);
266+
# }
261267
```
262268

263269
# Combinators
@@ -280,6 +286,7 @@ fn fibonacci() -> impl Stream<Item = u64, Error = ()> {
280286
Some(Ok((curr, (next, new_next))))
281287
})
282288
}
289+
# fn main() {}
283290
```
284291

285292
Just like with futures, using stream combinators requires a functional style of
@@ -298,13 +305,15 @@ use futures::Stream;
298305
# stream::once(Ok(1))
299306
# }
300307

308+
# fn main() {
301309
tokio::run(
302310
fibonacci().take(10)
303311
.for_each(|num| {
304312
println!("{}", num);
305313
Ok(())
306314
})
307315
);
316+
# }
308317
```
309318

310319
The [`take`] combinator limits the fibonacci stream to 10 values. The [`for_each`]
@@ -337,6 +346,7 @@ extern crate futures;
337346

338347
use futures::{stream, Stream};
339348

349+
# fn main() {
340350
let values = vec!["one", "two", "three"];
341351

342352
tokio::run(
@@ -345,6 +355,7 @@ tokio::run(
345355
Ok(())
346356
})
347357
)
358+
# }
348359
```
349360

350361
## Adapters

content/docs/getting-started/echo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ copying operation is complete, resolving to the amount of data that was copied.
128128

129129
Let's take a look at the closure we passed to `for_each` again.
130130

131-
```rust
131+
```rust, no_run
132132
# #![deny(deprecated)]
133133
# extern crate tokio;
134134
# extern crate futures;

content/docs/getting-started/futures.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ let n = socket.read(&mut buf).unwrap();
3434

3535
// Do something with &buf[..n];
3636
# }
37+
# fn main() {}
3738
```
3839

3940
When `socket.read` is called, either the socket has pending data in its receive

content/docs/going-deeper/chat.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Here is how the shared state is defined (the `Tx` type alias was done above):
141141
struct Shared {
142142
peers: HashMap<SocketAddr, Tx>,
143143
}
144+
# fn main() {}
144145
```
145146

146147
Then, at the very top of the `main` function, the state instance is created.
@@ -151,7 +152,9 @@ connections.
151152
# #![deny(deprecated)]
152153
# use std::sync::{Arc, Mutex};
153154
# type Shared = String;
155+
# fn main() {
154156
let state = Arc::new(Mutex::new(Shared::new()));
157+
# }
155158
```
156159

157160
Now we can handle processing incoming connections. The server task is updated to

content/docs/going-deeper/frames.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ pub struct LinesCodec {
3535
// only look at `de\n` before returning.
3636
next_index: usize,
3737
}
38+
39+
# fn main() {}
3840
```
3941

4042
The comments here explain how, since the bytes are buffered until a line is
@@ -58,7 +60,7 @@ Let's look at how `Decoder::decode` is implemented for `LinesCodec`.
5860
# use std::str;
5961
# use bytes::BytesMut;
6062
# use tokio_io::codec::*;
61-
# struct LinesCodec { next_index: usize };
63+
# struct LinesCodec { next_index: usize }
6264
# impl Decoder for LinesCodec {
6365
# type Item = String;
6466
# type Error = io::Error;
@@ -100,6 +102,7 @@ fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<String>, io::Error> {
100102
}
101103
}
102104
# }
105+
# fn main() {}
103106
```
104107

105108
The `Encoder::encode` method is called when a frame must be written to the
@@ -116,7 +119,7 @@ Let's now look at how `Encoder::encode` is implemented for `LinesCodec`.
116119
# use std::str;
117120
# use bytes::*;
118121
# use tokio_io::codec::*;
119-
# struct LinesCodec { next_index: usize };
122+
# struct LinesCodec { next_index: usize }
120123
# impl Encoder for LinesCodec {
121124
# type Item = String;
122125
# type Error = io::Error;
@@ -137,6 +140,7 @@ fn encode(&mut self, line: String, buf: &mut BytesMut) -> Result<(), io::Error>
137140
Ok(())
138141
}
139142
# }
143+
# fn main() {}
140144
```
141145

142146
It's often simpler to encode information. Here we simply reserve the space
@@ -158,6 +162,7 @@ and `AsyncWrite` traits using the `AsyncRead::framed` method.
158162
# use futures::prelude::*;
159163
# use tokio::net::TcpStream;
160164
# use tokio_codec::{Framed, LinesCodec};
165+
# fn main() {
161166
# let addr = "127.0.0.1:5000".parse().expect("invalid socket address");
162167
TcpStream::connect(&addr).and_then(|sock| {
163168
let framed_sock = Framed::new(sock, LinesCodec::new());
@@ -166,4 +171,5 @@ TcpStream::connect(&addr).and_then(|sock| {
166171
Ok(())
167172
})
168173
});
174+
# }
169175
```

content/docs/going-deeper/futures.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ And the `ResolveAndConnect` future is defined as:
248248
pub struct ResolveAndConnect {
249249
state: State,
250250
}
251+
252+
# fn main() {}
251253
```
252254

253255
Now, the implementation:

0 commit comments

Comments
 (0)