Skip to content

Commit

Permalink
improve other future-like impls for Either
Browse files Browse the repository at this point in the history
  • Loading branch information
GlenDC committed Nov 7, 2024
1 parent 8285021 commit db28d4c
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions rama-core/src/combinators/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ macro_rules! define_either {
}


impl<$($param),+, Output> $id<$($param),+>
where
$($param: std::future::Future<Output = Output>),+
{
impl<$($param),+> $id<$($param),+> {
/// Convert `Pin<&mut Either<A, B>>` to `Either<Pin<&mut A>, Pin<&mut B>>`,
/// pinned projections of the inner variants.
fn as_pin_mut(self: Pin<&mut Self>) -> $id<$(Pin<&mut $param>),+> {
Expand Down Expand Up @@ -155,69 +152,69 @@ macro_rules! impl_async_read_write_either {
($id:ident, $($param:ident),+ $(,)?) => {
impl<$($param),+> AsyncRead for $id<$($param),+>
where
$($param: AsyncRead + Unpin),+,
$($param: AsyncRead),+,
{
fn poll_read(
mut self: Pin<&mut Self>,
self: Pin<&mut Self>,
cx: &mut TaskContext<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<IoResult<()>> {
match &mut *self {
match self.as_pin_mut() {
$(
$id::$param(reader) => Pin::new(reader).poll_read(cx, buf),
$id::$param(reader) => reader.poll_read(cx, buf),
)+
}
}
}

impl<$($param),+> AsyncWrite for $id<$($param),+>
where
$($param: AsyncWrite + Unpin),+,
$($param: AsyncWrite),+,
{
fn poll_write(
mut self: Pin<&mut Self>,
self: Pin<&mut Self>,
cx: &mut TaskContext<'_>,
buf: &[u8],
) -> Poll<Result<usize, IoError>> {
match &mut *self {
match self.as_pin_mut() {
$(
$id::$param(writer) => Pin::new(writer).poll_write(cx, buf),
$id::$param(writer) => writer.poll_write(cx, buf),
)+
}
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<Result<(), IoError>> {
match &mut *self {
fn poll_flush(self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<Result<(), IoError>> {
match self.as_pin_mut() {
$(
$id::$param(writer) => Pin::new(writer).poll_flush(cx),
$id::$param(writer) => writer.poll_flush(cx),
)+
}
}

fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<Result<(), IoError>> {
match &mut *self {
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<Result<(), IoError>> {
match self.as_pin_mut() {
$(
$id::$param(writer) => Pin::new(writer).poll_shutdown(cx),
$id::$param(writer) => writer.poll_shutdown(cx),
)+
}
}

fn poll_write_vectored(
mut self: Pin<&mut Self>,
self: Pin<&mut Self>,
cx: &mut TaskContext<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<Result<usize, IoError>> {
match &mut *self {
match self.as_pin_mut() {
$(
$id::$param(writer) => Pin::new(writer).poll_write_vectored(cx, bufs),
$id::$param(writer) => writer.poll_write_vectored(cx, bufs),
)+
}
}

fn is_write_vectored(&self) -> bool {
match self {
$(
$id::$param(reader) => reader.is_write_vectored(),
$id::$param(writer) => writer.is_write_vectored(),
)+
}
}
Expand Down

0 comments on commit db28d4c

Please sign in to comment.