@@ -45,6 +45,18 @@ pub trait Future01CompatExt: Future01 {
45
45
/// [`Future<Item = T, Error = E>`](futures::future::Future)
46
46
/// into a futures 0.3
47
47
/// [`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
+ /// ```
48
60
fn compat ( self ) -> Compat01As03 < Self >
49
61
where
50
62
Self : Sized ,
@@ -60,6 +72,19 @@ pub trait Stream01CompatExt: Stream01 {
60
72
/// [`Stream<Item = T, Error = E>`](futures::stream::Stream)
61
73
/// into a futures 0.3
62
74
/// [`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
+ /// ```
63
88
fn compat ( self ) -> Compat01As03 < Self >
64
89
where
65
90
Self : Sized ,
@@ -75,6 +100,22 @@ pub trait Sink01CompatExt: Sink01 {
75
100
/// [`Sink<SinkItem = T, SinkError = E>`](futures::sink::Sink)
76
101
/// into a futures 0.3
77
102
/// [`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
+ /// ```
78
119
fn sink_compat ( self ) -> Compat01As03Sink < Self , Self :: SinkItem >
79
120
where
80
121
Self : Sized ,
@@ -304,6 +345,22 @@ mod io {
304
345
pub trait AsyncRead01CompatExt : AsyncRead01 {
305
346
/// Converts a tokio-io [`AsyncRead`](tokio_io::AsyncRead) into a futures-io 0.3
306
347
/// [`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
+ /// ```
307
364
fn compat ( self ) -> Compat01As03 < Self >
308
365
where
309
366
Self : Sized ,
@@ -317,6 +374,22 @@ mod io {
317
374
pub trait AsyncWrite01CompatExt : AsyncWrite01 {
318
375
/// Converts a tokio-io [`AsyncWrite`](tokio_io::AsyncWrite) into a futures-io 0.3
319
376
/// [`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
+ /// ```
320
393
fn compat ( self ) -> Compat01As03 < Self >
321
394
where
322
395
Self : Sized ,
0 commit comments