2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-20 11:21:14 +00:00
pict-rs/src/either.rs

68 lines
1.5 KiB
Rust
Raw Permalink Normal View History

2023-08-23 16:59:42 +00:00
use futures_core::Stream;
2021-10-14 00:06:53 +00:00
use std::{
pin::Pin,
task::{Context, Poll},
};
use tokio::io::{AsyncRead, ReadBuf};
pin_project_lite::pin_project! {
#[project = EitherProj]
#[project_replace = EitherProjReplace]
pub(crate) enum Either<Left, Right> {
Left {
#[pin]
left: Left,
},
Right {
#[pin]
right: Right,
},
}
}
impl<L, R> Either<L, R> {
pub(crate) fn left(left: L) -> Self {
Either::Left { left }
}
pub(crate) fn right(right: R) -> Self {
Either::Right { right }
}
2021-10-14 00:06:53 +00:00
}
impl<Left, Right> AsyncRead for Either<Left, Right>
where
Left: AsyncRead,
Right: AsyncRead,
2021-10-14 00:06:53 +00:00
{
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
let this = self.as_mut().project();
match this {
EitherProj::Left { left } => left.poll_read(cx, buf),
EitherProj::Right { right } => right.poll_read(cx, buf),
2021-10-14 00:06:53 +00:00
}
}
}
impl<Left, Right> Stream for Either<Left, Right>
where
Left: Stream<Item = <Right as Stream>::Item>,
Right: Stream,
2021-10-14 00:06:53 +00:00
{
type Item = <Left as Stream>::Item;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.as_mut().project();
match this {
EitherProj::Left { left } => left.poll_next(cx),
EitherProj::Right { right } => right.poll_next(cx),
2021-10-14 00:06:53 +00:00
}
}
}