Skip to content

Commit f75df0f

Browse files
Nemo157cramertj
authored andcommitted
Add extension traits for io 0.1 -> 0.3 compat
1 parent 2d1e371 commit f75df0f

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

futures-util/src/compat/compat01as03.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use std::pin::Pin;
1111
use std::task::Waker;
1212
use futures_sink::Sink as Sink03;
1313

14+
#[cfg(feature = "io-compat")]
15+
pub use io::{AsyncRead01CompatExt, AsyncWrite01CompatExt};
16+
1417
/// Converts a futures 0.1 Future, Stream, AsyncRead, or AsyncWrite
1518
/// object to a futures 0.3-compatible version,
1619
#[derive(Debug)]
@@ -297,6 +300,32 @@ mod io {
297300
use std::io::Error;
298301
use tokio_io::{AsyncRead as AsyncRead01, AsyncWrite as AsyncWrite01};
299302

303+
/// Extension trait for tokio-io [`AsyncRead`](tokio_io::AsyncRead)
304+
pub trait AsyncRead01CompatExt: AsyncRead01 {
305+
/// Converts a tokio-io [`AsyncRead`](tokio_io::AsyncRead) into a futures-io 0.3
306+
/// [`AsyncRead`](futures_io::AsyncRead).
307+
fn compat(self) -> Compat01As03<Self>
308+
where
309+
Self: Sized,
310+
{
311+
Compat01As03::new(self)
312+
}
313+
}
314+
impl<R: AsyncRead01> AsyncRead01CompatExt for R {}
315+
316+
/// Extension trait for tokio-io [`AsyncWrite`](tokio_io::AsyncWrite)
317+
pub trait AsyncWrite01CompatExt: AsyncWrite01 {
318+
/// Converts a tokio-io [`AsyncWrite`](tokio_io::AsyncWrite) into a futures-io 0.3
319+
/// [`AsyncWrite`](futures_io::AsyncWrite).
320+
fn compat(self) -> Compat01As03<Self>
321+
where
322+
Self: Sized,
323+
{
324+
Compat01As03::new(self)
325+
}
326+
}
327+
impl<W: AsyncWrite01> AsyncWrite01CompatExt for W {}
328+
300329
impl<R: AsyncRead01> AsyncRead03 for Compat01As03<R> {
301330
unsafe fn initializer(&self) -> Initializer {
302331
// check if `prepare_uninitialized_buffer` needs zeroing

futures-util/src/compat/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ pub use self::executor::{Executor01CompatExt, Executor01Future, Executor01As03};
88
mod compat01as03;
99
pub use self::compat01as03::{Compat01As03, Compat01As03Sink, Future01CompatExt, Stream01CompatExt, Sink01CompatExt};
1010

11+
#[cfg(feature = "io-compat")]
12+
pub use self::compat01as03::{AsyncRead01CompatExt, AsyncWrite01CompatExt};
13+
1114
mod compat03as01;
1215
pub use self::compat03as01::Compat;

futures/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ pub mod compat {
9292
Stream01CompatExt,
9393
Sink01CompatExt,
9494
};
95+
96+
#[cfg(feature = "io-compat")]
97+
pub use futures_util::compat::{
98+
AsyncRead01CompatExt,
99+
AsyncWrite01CompatExt,
100+
};
95101
}
96102

97103
#[cfg(feature = "std")]

0 commit comments

Comments
 (0)