Skip to content

Commit c949076

Browse files
Nemo157cramertj
authored andcommitted
Add doc-tests for 0.1 -> 0.3 compat methods
1 parent f75df0f commit c949076

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed

futures-util/src/compat/compat01as03.rs

+73
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ pub trait Future01CompatExt: Future01 {
4545
/// [`Future<Item = T, Error = E>`](futures::future::Future)
4646
/// into a futures 0.3
4747
/// [`Future<Output = Result<T, E>>`](futures_core::future::Future).
48+
///
49+
/// ```
50+
/// #![feature(async_await, await_macro, futures_api)]
51+
/// # futures::executor::block_on(async {
52+
/// # // TODO: These should be all using `futures::compat`, but that runs up against Cargo
53+
/// # // feature issues
54+
/// use futures_util::compat::Future01CompatExt;
55+
///
56+
/// let future = futures_01::future::ok::<u32, ()>(1);
57+
/// assert_eq!(await!(future.compat()), Ok(1));
58+
/// # });
59+
/// ```
4860
fn compat(self) -> Compat01As03<Self>
4961
where
5062
Self: Sized,
@@ -60,6 +72,19 @@ pub trait Stream01CompatExt: Stream01 {
6072
/// [`Stream<Item = T, Error = E>`](futures::stream::Stream)
6173
/// into a futures 0.3
6274
/// [`Stream<Item = Result<T, E>>`](futures_core::stream::Stream).
75+
///
76+
/// ```
77+
/// #![feature(async_await, await_macro, futures_api)]
78+
/// # futures::executor::block_on(async {
79+
/// use futures::stream::StreamExt;
80+
/// use futures_util::compat::Stream01CompatExt;
81+
///
82+
/// let stream = futures_01::stream::once::<u32, ()>(Ok(1));
83+
/// let mut stream = stream.compat();
84+
/// assert_eq!(await!(stream.next()), Some(Ok(1)));
85+
/// assert_eq!(await!(stream.next()), None);
86+
/// # });
87+
/// ```
6388
fn compat(self) -> Compat01As03<Self>
6489
where
6590
Self: Sized,
@@ -75,6 +100,22 @@ pub trait Sink01CompatExt: Sink01 {
75100
/// [`Sink<SinkItem = T, SinkError = E>`](futures::sink::Sink)
76101
/// into a futures 0.3
77102
/// [`Sink<SinkItem = T, SinkError = E>`](futures_sink::sink::Sink).
103+
///
104+
/// ```
105+
/// #![feature(async_await, await_macro, futures_api)]
106+
/// # futures::executor::block_on(async {
107+
/// use futures::{sink::SinkExt, stream::StreamExt};
108+
/// use futures_util::compat::{Stream01CompatExt, Sink01CompatExt};
109+
///
110+
/// let (tx, rx) = futures_01::unsync::mpsc::channel(1);
111+
/// let (mut tx, mut rx) = (tx.sink_compat(), rx.compat());
112+
///
113+
/// await!(tx.send(1)).unwrap();
114+
/// drop(tx);
115+
/// assert_eq!(await!(rx.next()), Some(Ok(1)));
116+
/// assert_eq!(await!(rx.next()), None);
117+
/// # });
118+
/// ```
78119
fn sink_compat(self) -> Compat01As03Sink<Self, Self::SinkItem>
79120
where
80121
Self: Sized,
@@ -304,6 +345,22 @@ mod io {
304345
pub trait AsyncRead01CompatExt: AsyncRead01 {
305346
/// Converts a tokio-io [`AsyncRead`](tokio_io::AsyncRead) into a futures-io 0.3
306347
/// [`AsyncRead`](futures_io::AsyncRead).
348+
///
349+
/// ```
350+
/// #![feature(async_await, await_macro, futures_api, impl_trait_in_bindings)]
351+
/// # futures::executor::block_on(async {
352+
/// use futures::io::AsyncReadExt;
353+
/// use futures_util::compat::AsyncRead01CompatExt;
354+
///
355+
/// let input = b"Hello World!";
356+
/// let reader: impl tokio_io::AsyncRead = std::io::Cursor::new(input);
357+
/// let mut reader: impl futures::io::AsyncRead = reader.compat();
358+
///
359+
/// let mut output = Vec::with_capacity(12);
360+
/// await!(reader.read_to_end(&mut output)).unwrap();
361+
/// assert_eq!(output, input);
362+
/// # });
363+
/// ```
307364
fn compat(self) -> Compat01As03<Self>
308365
where
309366
Self: Sized,
@@ -317,6 +374,22 @@ mod io {
317374
pub trait AsyncWrite01CompatExt: AsyncWrite01 {
318375
/// Converts a tokio-io [`AsyncWrite`](tokio_io::AsyncWrite) into a futures-io 0.3
319376
/// [`AsyncWrite`](futures_io::AsyncWrite).
377+
///
378+
/// ```
379+
/// #![feature(async_await, await_macro, futures_api, impl_trait_in_bindings)]
380+
/// # futures::executor::block_on(async {
381+
/// use futures::io::AsyncWriteExt;
382+
/// use futures_util::compat::AsyncWrite01CompatExt;
383+
///
384+
/// let input = b"Hello World!";
385+
/// let mut cursor = std::io::Cursor::new(Vec::with_capacity(12));
386+
///
387+
/// let mut writer = (&mut cursor).compat();
388+
/// await!(writer.write_all(input)).unwrap();
389+
///
390+
/// assert_eq!(cursor.into_inner(), input);
391+
/// # });
392+
/// ```
320393
fn compat(self) -> Compat01As03<Self>
321394
where
322395
Self: Sized,

futures-util/src/compat/executor.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,28 @@ pub trait Executor01CompatExt: Executor01<Executor01Future> +
2121
/// Converts a futures 0.1 [`Executor`](futures::future::Executor) into a
2222
/// futures 0.3 [`Spawn`](futures_core::task::Spawn).
2323
///
24-
/// ```ignore
24+
/// ```
2525
/// #![feature(async_await, await_macro, futures_api)]
2626
/// use futures::Future;
27+
/// use futures::task::SpawnExt;
2728
/// use futures::future::{FutureExt, TryFutureExt};
28-
/// use futures::compat::Executor01CompatExt;
29-
/// use futures::spawn;
30-
/// use tokio_threadpool::ThreadPool;
29+
/// use futures_util::compat::Executor01CompatExt;
30+
/// use tokio::executor::DefaultExecutor;
3131
///
32-
/// let pool01 = ThreadPool::new();
3332
/// # let (tx, rx) = futures::channel::oneshot::channel();
3433
///
35-
/// let future03 = async {
34+
/// let mut spawner = DefaultExecutor::current().compat();
35+
/// let future03 = async move {
3636
/// println!("Running on the pool");
37-
/// spawn!(async {
37+
/// spawner.spawn(async {
3838
/// println!("Spawned!");
3939
/// # tx.send(42).unwrap();
4040
/// }).unwrap();
4141
/// };
4242
///
4343
/// let future01 = future03.unit_error().boxed().compat();
4444
///
45-
/// pool01.spawn(future01);
46-
/// pool01.shutdown_on_idle().wait().unwrap();
45+
/// tokio::run(future01);
4746
/// # futures::executor::block_on(rx).unwrap();
4847
/// ```
4948
fn compat(self) -> Executor01As03<Self>

0 commit comments

Comments
 (0)