Coverage Report

Created: 2025-02-21 07:11

/rust/registry/src/index.crates.io-6f17d22bba15001f/futures-util-0.3.31/src/future/either.rs
Line
Count
Source (jump to first uncovered line)
1
use core::pin::Pin;
2
use core::task::{Context, Poll};
3
use futures_core::future::{FusedFuture, Future};
4
use futures_core::stream::{FusedStream, Stream};
5
#[cfg(feature = "sink")]
6
use futures_sink::Sink;
7
8
/// Combines two different futures, streams, or sinks having the same associated types into a single type.
9
///
10
/// This is useful when conditionally choosing between two distinct future types:
11
///
12
/// ```rust
13
/// use futures::future::Either;
14
///
15
/// # futures::executor::block_on(async {
16
/// let cond = true;
17
///
18
/// let fut = if cond {
19
///     Either::Left(async move { 12 })
20
/// } else {
21
///     Either::Right(async move { 44 })
22
/// };
23
///
24
/// assert_eq!(fut.await, 12);
25
/// # })
26
/// ```
27
#[derive(Debug, Clone)]
28
pub enum Either<A, B> {
29
    /// First branch of the type
30
    Left(/* #[pin] */ A),
31
    /// Second branch of the type
32
    Right(/* #[pin] */ B),
33
}
34
35
impl<A, B> Either<A, B> {
36
    /// Convert `Pin<&Either<A, B>>` to `Either<Pin<&A>, Pin<&B>>`,
37
    /// pinned projections of the inner variants.
38
0
    pub fn as_pin_ref(self: Pin<&Self>) -> Either<Pin<&A>, Pin<&B>> {
39
0
        // SAFETY: We can use `new_unchecked` because the `inner` parts are
40
0
        // guaranteed to be pinned, as they come from `self` which is pinned.
41
0
        unsafe {
42
0
            match self.get_ref() {
43
0
                Self::Left(inner) => Either::Left(Pin::new_unchecked(inner)),
44
0
                Self::Right(inner) => Either::Right(Pin::new_unchecked(inner)),
45
            }
46
        }
47
0
    }
48
49
    /// Convert `Pin<&mut Either<A, B>>` to `Either<Pin<&mut A>, Pin<&mut B>>`,
50
    /// pinned projections of the inner variants.
51
0
    pub fn as_pin_mut(self: Pin<&mut Self>) -> Either<Pin<&mut A>, Pin<&mut B>> {
52
0
        // SAFETY: `get_unchecked_mut` is fine because we don't move anything.
53
0
        // We can use `new_unchecked` because the `inner` parts are guaranteed
54
0
        // to be pinned, as they come from `self` which is pinned, and we never
55
0
        // offer an unpinned `&mut A` or `&mut B` through `Pin<&mut Self>`. We
56
0
        // also don't have an implementation of `Drop`, nor manual `Unpin`.
57
0
        unsafe {
58
0
            match self.get_unchecked_mut() {
59
0
                Self::Left(inner) => Either::Left(Pin::new_unchecked(inner)),
60
0
                Self::Right(inner) => Either::Right(Pin::new_unchecked(inner)),
61
            }
62
        }
63
0
    }
Unexecuted instantiation: <futures_util::future::either::Either<surrealdb::api::method::live::Stream<surrealdb::api::value::Value>, futures_util::stream::select_all::SelectAll<surrealdb::api::method::live::Stream<surrealdb::api::value::Value>>>>::as_pin_mut
Unexecuted instantiation: <futures_util::future::either::Either<_, _>>::as_pin_mut
64
}
65
66
impl<A, B, T> Either<(T, A), (T, B)> {
67
    /// Factor out a homogeneous type from an either of pairs.
68
    ///
69
    /// Here, the homogeneous type is the first element of the pairs.
70
0
    pub fn factor_first(self) -> (T, Either<A, B>) {
71
0
        match self {
72
0
            Self::Left((x, a)) => (x, Either::Left(a)),
73
0
            Self::Right((x, b)) => (x, Either::Right(b)),
74
        }
75
0
    }
76
}
77
78
impl<A, B, T> Either<(A, T), (B, T)> {
79
    /// Factor out a homogeneous type from an either of pairs.
80
    ///
81
    /// Here, the homogeneous type is the second element of the pairs.
82
0
    pub fn factor_second(self) -> (Either<A, B>, T) {
83
0
        match self {
84
0
            Self::Left((a, x)) => (Either::Left(a), x),
85
0
            Self::Right((b, x)) => (Either::Right(b), x),
86
        }
87
0
    }
88
}
89
90
impl<T> Either<T, T> {
91
    /// Extract the value of an either over two equivalent types.
92
0
    pub fn into_inner(self) -> T {
93
0
        match self {
94
0
            Self::Left(x) | Self::Right(x) => x,
95
0
        }
96
0
    }
97
}
98
99
impl<A, B> Future for Either<A, B>
100
where
101
    A: Future,
102
    B: Future<Output = A::Output>,
103
{
104
    type Output = A::Output;
105
106
0
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
107
0
        match self.as_pin_mut() {
108
0
            Either::Left(x) => x.poll(cx),
109
0
            Either::Right(x) => x.poll(cx),
110
        }
111
0
    }
112
}
113
114
impl<A, B> FusedFuture for Either<A, B>
115
where
116
    A: FusedFuture,
117
    B: FusedFuture<Output = A::Output>,
118
{
119
0
    fn is_terminated(&self) -> bool {
120
0
        match self {
121
0
            Self::Left(x) => x.is_terminated(),
122
0
            Self::Right(x) => x.is_terminated(),
123
        }
124
0
    }
125
}
126
127
impl<A, B> Stream for Either<A, B>
128
where
129
    A: Stream,
130
    B: Stream<Item = A::Item>,
131
{
132
    type Item = A::Item;
133
134
0
    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
135
0
        match self.as_pin_mut() {
136
0
            Either::Left(x) => x.poll_next(cx),
137
0
            Either::Right(x) => x.poll_next(cx),
138
        }
139
0
    }
Unexecuted instantiation: <futures_util::future::either::Either<surrealdb::api::method::live::Stream<surrealdb::api::value::Value>, futures_util::stream::select_all::SelectAll<surrealdb::api::method::live::Stream<surrealdb::api::value::Value>>> as futures_core::stream::Stream>::poll_next
Unexecuted instantiation: <futures_util::future::either::Either<_, _> as futures_core::stream::Stream>::poll_next
140
141
0
    fn size_hint(&self) -> (usize, Option<usize>) {
142
0
        match self {
143
0
            Self::Left(x) => x.size_hint(),
144
0
            Self::Right(x) => x.size_hint(),
145
        }
146
0
    }
147
}
148
149
impl<A, B> FusedStream for Either<A, B>
150
where
151
    A: FusedStream,
152
    B: FusedStream<Item = A::Item>,
153
{
154
0
    fn is_terminated(&self) -> bool {
155
0
        match self {
156
0
            Self::Left(x) => x.is_terminated(),
157
0
            Self::Right(x) => x.is_terminated(),
158
        }
159
0
    }
160
}
161
162
#[cfg(feature = "sink")]
163
impl<A, B, Item> Sink<Item> for Either<A, B>
164
where
165
    A: Sink<Item>,
166
    B: Sink<Item, Error = A::Error>,
167
{
168
    type Error = A::Error;
169
170
0
    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
171
0
        match self.as_pin_mut() {
172
0
            Either::Left(x) => x.poll_ready(cx),
173
0
            Either::Right(x) => x.poll_ready(cx),
174
        }
175
0
    }
176
177
0
    fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error> {
178
0
        match self.as_pin_mut() {
179
0
            Either::Left(x) => x.start_send(item),
180
0
            Either::Right(x) => x.start_send(item),
181
        }
182
0
    }
183
184
0
    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
185
0
        match self.as_pin_mut() {
186
0
            Either::Left(x) => x.poll_flush(cx),
187
0
            Either::Right(x) => x.poll_flush(cx),
188
        }
189
0
    }
190
191
0
    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
192
0
        match self.as_pin_mut() {
193
0
            Either::Left(x) => x.poll_close(cx),
194
0
            Either::Right(x) => x.poll_close(cx),
195
        }
196
0
    }
197
}
198
199
#[cfg(feature = "io")]
200
#[cfg(feature = "std")]
201
mod if_std {
202
    use super::*;
203
204
    use futures_io::{
205
        AsyncBufRead, AsyncRead, AsyncSeek, AsyncWrite, IoSlice, IoSliceMut, Result, SeekFrom,
206
    };
207
208
    impl<A, B> AsyncRead for Either<A, B>
209
    where
210
        A: AsyncRead,
211
        B: AsyncRead,
212
    {
213
0
        fn poll_read(
214
0
            self: Pin<&mut Self>,
215
0
            cx: &mut Context<'_>,
216
0
            buf: &mut [u8],
217
0
        ) -> Poll<Result<usize>> {
218
0
            match self.as_pin_mut() {
219
0
                Either::Left(x) => x.poll_read(cx, buf),
220
0
                Either::Right(x) => x.poll_read(cx, buf),
221
            }
222
0
        }
223
224
0
        fn poll_read_vectored(
225
0
            self: Pin<&mut Self>,
226
0
            cx: &mut Context<'_>,
227
0
            bufs: &mut [IoSliceMut<'_>],
228
0
        ) -> Poll<Result<usize>> {
229
0
            match self.as_pin_mut() {
230
0
                Either::Left(x) => x.poll_read_vectored(cx, bufs),
231
0
                Either::Right(x) => x.poll_read_vectored(cx, bufs),
232
            }
233
0
        }
234
    }
235
236
    impl<A, B> AsyncWrite for Either<A, B>
237
    where
238
        A: AsyncWrite,
239
        B: AsyncWrite,
240
    {
241
0
        fn poll_write(
242
0
            self: Pin<&mut Self>,
243
0
            cx: &mut Context<'_>,
244
0
            buf: &[u8],
245
0
        ) -> Poll<Result<usize>> {
246
0
            match self.as_pin_mut() {
247
0
                Either::Left(x) => x.poll_write(cx, buf),
248
0
                Either::Right(x) => x.poll_write(cx, buf),
249
            }
250
0
        }
251
252
0
        fn poll_write_vectored(
253
0
            self: Pin<&mut Self>,
254
0
            cx: &mut Context<'_>,
255
0
            bufs: &[IoSlice<'_>],
256
0
        ) -> Poll<Result<usize>> {
257
0
            match self.as_pin_mut() {
258
0
                Either::Left(x) => x.poll_write_vectored(cx, bufs),
259
0
                Either::Right(x) => x.poll_write_vectored(cx, bufs),
260
            }
261
0
        }
262
263
0
        fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
264
0
            match self.as_pin_mut() {
265
0
                Either::Left(x) => x.poll_flush(cx),
266
0
                Either::Right(x) => x.poll_flush(cx),
267
            }
268
0
        }
269
270
0
        fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
271
0
            match self.as_pin_mut() {
272
0
                Either::Left(x) => x.poll_close(cx),
273
0
                Either::Right(x) => x.poll_close(cx),
274
            }
275
0
        }
276
    }
277
278
    impl<A, B> AsyncSeek for Either<A, B>
279
    where
280
        A: AsyncSeek,
281
        B: AsyncSeek,
282
    {
283
0
        fn poll_seek(
284
0
            self: Pin<&mut Self>,
285
0
            cx: &mut Context<'_>,
286
0
            pos: SeekFrom,
287
0
        ) -> Poll<Result<u64>> {
288
0
            match self.as_pin_mut() {
289
0
                Either::Left(x) => x.poll_seek(cx, pos),
290
0
                Either::Right(x) => x.poll_seek(cx, pos),
291
            }
292
0
        }
293
    }
294
295
    impl<A, B> AsyncBufRead for Either<A, B>
296
    where
297
        A: AsyncBufRead,
298
        B: AsyncBufRead,
299
    {
300
0
        fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
301
0
            match self.as_pin_mut() {
302
0
                Either::Left(x) => x.poll_fill_buf(cx),
303
0
                Either::Right(x) => x.poll_fill_buf(cx),
304
            }
305
0
        }
306
307
0
        fn consume(self: Pin<&mut Self>, amt: usize) {
308
0
            match self.as_pin_mut() {
309
0
                Either::Left(x) => x.consume(amt),
310
0
                Either::Right(x) => x.consume(amt),
311
            }
312
0
        }
313
    }
314
}