tui-realm icon indicating copy to clipboard operation
tui-realm copied to clipboard

[Feature Request] - Change ports to make use of `Iterator` and `Stream`

Open hasezoey opened this issue 4 months ago • 0 comments

Description

Currently Poll and PollAsync make use of custom function signatures, but from usage and expectations they would be more in-line with using Iterators for Poll and (futures_util::)Stream with PollAsync

Changes

We could either:

  • completely remove those 2 traits and make them a alias to Iterator<Item = Result<Event, Error>> and Stream<Item = Result<Event, Error>>, or
  • keep the traits, but make them a default impl and require Iterator / Stream to be implemented, similar to how rodio handles Source and Samples

Implementation

Either:

pub type Poll<UserEvent: Eq + PartialEq + Clone + PartialOrd + 'static> = Iterator<Item = ListenerResult<Event<UserEvent>>> + Send;

pub type PollAsync<UserEvent: Eq + PartialEq + Clone + PartialOrd + 'static> = futures_util::Stream<Item = ListenerResult<Event<UserEvent>>> + Send;

or

pub trait Poll<UserEvent>: Iterator<Item = ListenerResult<Event<UserEvent>>> + Send
where
    UserEvent: Eq + PartialEq + Clone + PartialOrd + 'static,
{
    fn poll(&mut self) -> ListenerResult<Option<Event<UserEvent>>> {
        self.next().transpose()
    }
}

pub trait PollAsync<UserEvent>: futures_util::Stream<Item = ListenerResult<Event<UserEvent>>> + Send
where
    UserEvent: Eq + PartialEq + Clone + PartialOrd + Send + 'static,
{
    async fn poll(&mut self) -> ListenerResult<Option<Event<UserEvent>>> {
        self.next().await.transpose()
    }
}

(we could also avoid the transposes by changing our signatures, which i would actually recommend)

PS: if the Poll and PollAsync types are not expected to actually get any extra functions, i would actually recommend going with the type aliases.

hasezoey avatar Jun 11 '25 13:06 hasezoey