From 317a4a9dec5dc8f9832ff668cb14c52c4749777c Mon Sep 17 00:00:00 2001 From: matt rice Date: Wed, 14 Feb 2024 05:04:33 +0000 Subject: [PATCH] Impl futures::stream::Stream for Controller/Listener (#71) --- stick/Cargo.toml | 5 +++++ stick/src/ctlr.rs | 17 +++++++++++++++++ stick/src/listener.rs | 13 +++++++++++++ 3 files changed, 35 insertions(+) diff --git a/stick/Cargo.toml b/stick/Cargo.toml index 16765ab..418edfd 100644 --- a/stick/Cargo.toml +++ b/stick/Cargo.toml @@ -36,3 +36,8 @@ default = ["sdb"] gcdb = [] # Include the stick controller database (button/axis remappings). sdb = [] +# Include futures::stream::Stream impl. +stream = ["dep:futures"] + +[dependencies] +futures = { version = "0.3.30", optional = true } diff --git a/stick/src/ctlr.rs b/stick/src/ctlr.rs index e947ed0..701522b 100644 --- a/stick/src/ctlr.rs +++ b/stick/src/ctlr.rs @@ -580,6 +580,23 @@ impl Future for Controller { } } +#[cfg(feature = "stream")] +impl futures::stream::Stream for Controller { + type Item = Event; + fn poll_next( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll> { + match self.poll(cx) { + Poll::Pending => Poll::Pending, + Poll::Ready(e) => match e { + Event::Disconnect => Poll::Ready(None), + e => Poll::Ready(Some(e)), + }, + } + } +} + pub trait Rumble { fn left(&self) -> f32; fn right(&self) -> f32; diff --git a/stick/src/listener.rs b/stick/src/listener.rs index f2ab7c1..1a2f160 100644 --- a/stick/src/listener.rs +++ b/stick/src/listener.rs @@ -36,3 +36,16 @@ impl Future for Listener { Pin::new(&mut self.get_mut().0).poll(cx) } } +#[cfg(feature = "stream")] +impl futures::stream::Stream for Listener { + type Item = crate::Controller; + fn poll_next( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll> { + match self.poll(cx) { + Poll::Ready(c) => Poll::Ready(Some(c)), + Poll::Pending => Poll::Pending, + } + } +}