Skip to content

Commit

Permalink
Impl futures::stream::Stream for Controller/Listener
Browse files Browse the repository at this point in the history
  • Loading branch information
ratmice committed Feb 10, 2024
1 parent a67442f commit 23fa8e8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions stick/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
17 changes: 17 additions & 0 deletions stick/src/ctlr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Event>> {
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;
Expand Down
13 changes: 13 additions & 0 deletions stick/src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Self::Item>> {
match self.poll(cx) {
Poll::Ready(c) => Poll::Ready(Some(c)),
Poll::Pending => Poll::Pending,
}
}
}

0 comments on commit 23fa8e8

Please sign in to comment.