futures_util_either/
impl_std_io.rs

1use std::io::{BufRead, Read, Result, Seek, SeekFrom, Write};
2
3use super::Either;
4
5//
6// Ref https://github.com/bluss/either/blob/1.6.1/src/lib.rs#L843
7impl<A, B> Read for Either<A, B>
8where
9    A: Read,
10    B: Read,
11{
12    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
13        match self {
14            Self::Left(x) => x.read(buf),
15            Self::Right(x) => x.read(buf),
16        }
17    }
18
19    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
20        match self {
21            Self::Left(x) => x.read_to_end(buf),
22            Self::Right(x) => x.read_to_end(buf),
23        }
24    }
25}
26
27// Ref https://github.com/bluss/either/blob/1.6.1/src/lib.rs#L877
28impl<A, B> Write for Either<A, B>
29where
30    A: Write,
31    B: Write,
32{
33    fn write(&mut self, buf: &[u8]) -> Result<usize> {
34        match self {
35            Self::Left(x) => x.write(buf),
36            Self::Right(x) => x.write(buf),
37        }
38    }
39    fn flush(&mut self) -> Result<()> {
40        match self {
41            Self::Left(x) => x.flush(),
42            Self::Right(x) => x.flush(),
43        }
44    }
45}
46
47impl<A, B> Seek for Either<A, B>
48where
49    A: Seek,
50    B: Seek,
51{
52    fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
53        match self {
54            Self::Left(x) => x.seek(pos),
55            Self::Right(x) => x.seek(pos),
56        }
57    }
58}
59
60// Ref https://github.com/bluss/either/blob/1.6.1/src/lib.rs#L859
61impl<A, B> BufRead for Either<A, B>
62where
63    A: BufRead,
64    B: BufRead,
65{
66    fn fill_buf(&mut self) -> Result<&[u8]> {
67        match self {
68            Self::Left(x) => x.fill_buf(),
69            Self::Right(x) => x.fill_buf(),
70        }
71    }
72    fn consume(&mut self, amt: usize) {
73        match self {
74            Self::Left(x) => x.consume(amt),
75            Self::Right(x) => x.consume(amt),
76        }
77    }
78}