Skip to content

Commit

Permalink
feat: Move Future impl to forwarding macro
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunley <dev@notgull.net>
  • Loading branch information
notgull committed Feb 3, 2024
1 parent e0fefc2 commit 6fc00c0
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ pub trait Listener<T>: Future<Output = T> + __sealed::Sealed {
fn same_event(&self, other: &Self) -> bool;
}

/// Implement the `Listener` trait using the underlying `InnerListener`.
macro_rules! forward_impl_to_listener {
($gen:ident => $ty:ty) => {
impl<$gen> crate::Listener<$gen> for $ty {
Expand Down Expand Up @@ -869,6 +870,15 @@ macro_rules! forward_impl_to_listener {
core::ptr::eq::<Inner<$gen>>(&*self.listener().event, &*other.listener().event)
}
}

impl<$gen> Future for $ty {
type Output = $gen;

#[inline]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<$gen> {
self.listener_mut().poll_internal(cx)
}
}
};
}

Expand Down Expand Up @@ -922,14 +932,6 @@ impl<T> EventListener<T> {

forward_impl_to_listener! { T => EventListener<T> }

impl<T> Future for EventListener<T> {
type Output = T;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.listener.as_mut().poll_internal(cx)
}
}

/// Create a stack-based event listener for an [`Event`].
#[macro_export]
macro_rules! listener {
Expand Down Expand Up @@ -1376,13 +1378,4 @@ pub mod __private {
}

forward_impl_to_listener! { T => StackListener<'_, '_, T> }

impl<T> Future for StackListener<'_, '_, T> {
type Output = T;

#[inline]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.listener_mut().poll_internal(cx)
}
}
}

0 comments on commit 6fc00c0

Please sign in to comment.