1use core::fmt::{self, Display};
4use core::future::Future;
5use core::ops::{Deref, DerefMut};
6use core::pin::Pin;
7#[cfg(feature = "std")]
8use std::error::Error;
9#[cfg(feature = "std")]
10use std::io::{BufRead, Read, Seek};
11
12use crate::iter_either::{IntoIterEither, IterEither, IterMutEither};
13use crate::Either;
14
15impl<L, R, T> AsMut<T> for Either<L, R>
16where
17 T: ?Sized,
18 L: AsMut<T>,
19 R: AsMut<T>,
20{
21 #[inline]
22 fn as_mut(&mut self) -> &mut T {
23 each!(self, .as_mut())
24 }
25}
26
27impl<L, R, T> AsRef<T> for Either<L, R>
28where
29 T: ?Sized,
30 L: AsRef<T>,
31 R: AsRef<T>,
32{
33 #[inline]
34 fn as_ref(&self) -> &T {
35 each!(self, .as_ref())
36 }
37}
38
39#[cfg(feature = "std")]
40impl<L, R> BufRead for Either<L, R>
41where
42 L: BufRead,
43 R: BufRead,
44{
45 fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
46 each!(self, .fill_buf())
47 }
48
49 fn consume(&mut self, amount: usize) {
50 each!(self, .consume(amount));
51 }
52}
53
54impl<L, R> Deref for Either<L, R>
55where
56 L: Deref<Target = R::Target>,
57 R: Deref,
58{
59 type Target = R::Target;
60
61 #[inline]
62 fn deref(&self) -> &Self::Target {
63 each!(self)
64 }
65}
66
67impl<L, R> DerefMut for Either<L, R>
68where
69 L: DerefMut<Target = R::Target>,
70 R: DerefMut,
71{
72 #[inline]
73 fn deref_mut(&mut self) -> &mut Self::Target {
74 each!(self)
75 }
76}
77
78impl<L, R> Display for Either<L, R>
79where
80 L: Display,
81 R: Display,
82{
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 each!(self, .fmt(f))
85 }
86}
87
88#[cfg(feature = "std")]
89impl<L, R> Error for Either<L, R>
90where
91 L: Error,
92 R: Error,
93{
94 fn source(&self) -> Option<&(dyn Error + 'static)> {
95 each!(self, .source())
96 }
97}
98
99impl<A, T> Extend<A> for Either<T>
100where
101 T: Extend<A>,
102{
103 fn extend<I>(&mut self, iter: I)
104 where
105 I: IntoIterator<Item = A>,
106 {
107 each!(self, .extend(iter));
108 }
109}
110
111impl<'a, L, R> From<&'a Either<L, R>> for Either<&'a L, &'a R> {
112 #[inline]
113 fn from(value: &'a Either<L, R>) -> Self {
114 value.as_ref()
115 }
116}
117
118impl<'a, L, R> From<&'a mut Either<L, R>> for Either<&'a mut L, &'a mut R> {
119 #[inline]
120 fn from(value: &'a mut Either<L, R>) -> Self {
121 value.as_mut()
122 }
123}
124
125impl<L, R> From<Result<R, L>> for Either<L, R> {
126 #[inline]
127 fn from(value: Result<R, L>) -> Self {
128 match value {
129 Ok(ok) => Self::Right(ok),
130 Err(err) => Self::Left(err),
131 }
132 }
133}
134
135impl<L, R> Future for Either<L, R>
136where
137 L: Future<Output = R::Output>,
138 R: Future,
139{
140 type Output = R::Output;
141
142 fn poll(
143 self: Pin<&mut Self>,
144 cx: &mut core::task::Context<'_>,
145 ) -> core::task::Poll<Self::Output> {
146 match self.as_pin_mut() {
147 Either::Left(left) => left.poll(cx),
148 Either::Right(right) => right.poll(cx),
149 }
150 }
151}
152
153impl<T> IntoIterator for Either<T> {
154 type Item = T;
155 type IntoIter = IntoIterEither<T>;
156
157 fn into_iter(self) -> Self::IntoIter {
158 IntoIterEither::new(self)
159 }
160}
161
162impl<'a, T> IntoIterator for &'a Either<T> {
163 type Item = &'a T;
164 type IntoIter = IterEither<'a, T>;
165
166 fn into_iter(self) -> Self::IntoIter {
167 IterEither::new(self)
168 }
169}
170
171impl<'a, T> IntoIterator for &'a mut Either<T> {
172 type Item = &'a mut T;
173 type IntoIter = IterMutEither<'a, T>;
174
175 fn into_iter(self) -> Self::IntoIter {
176 IterMutEither::new(self)
177 }
178}
179
180#[cfg(feature = "std")]
181impl<L, R> Read for Either<L, R>
182where
183 L: Read,
184 R: Read,
185{
186 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
187 each!(self, .read(buf))
188 }
189}
190
191#[cfg(feature = "std")]
192impl<L, R> Seek for Either<L, R>
193where
194 L: Seek,
195 R: Seek,
196{
197 fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
198 each!(self, .seek(pos))
199 }
200}
201
202impl<L, R> fmt::Write for Either<L, R>
203where
204 L: fmt::Write,
205 R: fmt::Write,
206{
207 fn write_str(&mut self, s: &str) -> fmt::Result {
208 each!(self, .write_str(s))
209 }
210}
211
212#[cfg(feature = "std")]
213impl<L, R> std::io::Write for Either<L, R>
214where
215 L: std::io::Write,
216 R: std::io::Write,
217{
218 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
219 each!(self, .write(buf))
220 }
221
222 fn flush(&mut self) -> std::io::Result<()> {
223 each!(self, .flush())
224 }
225}