futures_util_either/
impl_futures_io.rs

1use core::{
2    pin::Pin,
3    task::{Context, Poll},
4};
5
6use futures_io::{
7    AsyncBufRead, AsyncRead, AsyncSeek, AsyncWrite, IoSlice, IoSliceMut, Result, SeekFrom,
8};
9
10use super::Either;
11
12//
13// https://github.com/rust-lang/futures-rs/blob/0.3.21/futures-util/src/future/either.rs#L191-L296
14//
15impl<A, B> AsyncRead for Either<A, B>
16where
17    A: AsyncRead,
18    B: AsyncRead,
19{
20    fn poll_read(
21        self: Pin<&mut Self>,
22        cx: &mut Context<'_>,
23        buf: &mut [u8],
24    ) -> Poll<Result<usize>> {
25        match self.project() {
26            Either::Left(x) => x.poll_read(cx, buf),
27            Either::Right(x) => x.poll_read(cx, buf),
28        }
29    }
30
31    fn poll_read_vectored(
32        self: Pin<&mut Self>,
33        cx: &mut Context<'_>,
34        bufs: &mut [IoSliceMut<'_>],
35    ) -> Poll<Result<usize>> {
36        match self.project() {
37            Either::Left(x) => x.poll_read_vectored(cx, bufs),
38            Either::Right(x) => x.poll_read_vectored(cx, bufs),
39        }
40    }
41}
42
43impl<A, B> AsyncWrite for Either<A, B>
44where
45    A: AsyncWrite,
46    B: AsyncWrite,
47{
48    fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
49        match self.project() {
50            Either::Left(x) => x.poll_write(cx, buf),
51            Either::Right(x) => x.poll_write(cx, buf),
52        }
53    }
54
55    fn poll_write_vectored(
56        self: Pin<&mut Self>,
57        cx: &mut Context<'_>,
58        bufs: &[IoSlice<'_>],
59    ) -> Poll<Result<usize>> {
60        match self.project() {
61            Either::Left(x) => x.poll_write_vectored(cx, bufs),
62            Either::Right(x) => x.poll_write_vectored(cx, bufs),
63        }
64    }
65
66    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
67        match self.project() {
68            Either::Left(x) => x.poll_flush(cx),
69            Either::Right(x) => x.poll_flush(cx),
70        }
71    }
72
73    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
74        match self.project() {
75            Either::Left(x) => x.poll_close(cx),
76            Either::Right(x) => x.poll_close(cx),
77        }
78    }
79}
80
81impl<A, B> AsyncSeek for Either<A, B>
82where
83    A: AsyncSeek,
84    B: AsyncSeek,
85{
86    fn poll_seek(self: Pin<&mut Self>, cx: &mut Context<'_>, pos: SeekFrom) -> Poll<Result<u64>> {
87        match self.project() {
88            Either::Left(x) => x.poll_seek(cx, pos),
89            Either::Right(x) => x.poll_seek(cx, pos),
90        }
91    }
92}
93
94impl<A, B> AsyncBufRead for Either<A, B>
95where
96    A: AsyncBufRead,
97    B: AsyncBufRead,
98{
99    fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
100        match self.project() {
101            Either::Left(x) => x.poll_fill_buf(cx),
102            Either::Right(x) => x.poll_fill_buf(cx),
103        }
104    }
105
106    fn consume(self: Pin<&mut Self>, amt: usize) {
107        match self.project() {
108            Either::Left(x) => x.consume(amt),
109            Either::Right(x) => x.consume(amt),
110        }
111    }
112}