Skip to content
This repository was archived by the owner on May 11, 2023. It is now read-only.

Commit cf0ea81

Browse files
committed
zb: Make use of futures_core::ready! marco
This makes writing manual async impls easier and will hopefully be part of the [std](rust-lang/rust#81050) (maybe under a different name though).
1 parent 3a0d099 commit cf0ea81

File tree

4 files changed

+21
-30
lines changed

4 files changed

+21
-30
lines changed

zbus/src/azync/connection.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::{
2828
};
2929
use zvariant::ObjectPath;
3030

31-
use futures_core::{stream, Future};
31+
use futures_core::{ready, stream, Future};
3232
use futures_sink::Sink;
3333
use futures_util::{
3434
future::{select, Either},
@@ -857,10 +857,9 @@ impl Sink<Message> for Connection {
857857

858858
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
859859
let mut raw_out_conn = self.inner.raw_out_conn.lock().expect("poisened lock");
860-
match raw_out_conn.flush(cx) {
861-
Poll::Ready(Ok(_)) => (),
862-
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
863-
Poll::Pending => return Poll::Pending,
860+
match ready!(raw_out_conn.flush(cx)) {
861+
Ok(_) => (),
862+
Err(e) => return Poll::Ready(Err(e)),
864863
}
865864

866865
Poll::Ready(raw_out_conn.close())
@@ -876,7 +875,7 @@ impl stream::Stream for Connection {
876875
let err_fut = stream.error_receiver.next();
877876
let mut select_fut = select(msg_fut, err_fut);
878877

879-
match futures_core::ready!(Pin::new(&mut select_fut).poll(cx)) {
878+
match ready!(Pin::new(&mut select_fut).poll(cx)) {
880879
Either::Left((msg, _)) => Poll::Ready(msg.map(Ok)),
881880
Either::Right((error, _)) => Poll::Ready(error.map(Err)),
882881
}
@@ -896,13 +895,10 @@ impl<'r, 's> Future for ReceiveMessage<'r, 's> {
896895
loop {
897896
match stream.raw_conn.try_receive_message() {
898897
Err(Error::Io(e)) if e.kind() == ErrorKind::WouldBlock => {
899-
let poll = stream.raw_conn.socket().poll_readable(cx);
900-
901-
match poll {
902-
Poll::Pending => return Poll::Pending,
898+
match ready!(stream.raw_conn.socket().poll_readable(cx)) {
903899
// Guess socket became ready already so let's try it again.
904-
Poll::Ready(Ok(_)) => continue,
905-
Poll::Ready(Err(e)) => return Poll::Ready(Err(e.into())),
900+
Ok(_) => continue,
901+
Err(e) => return Poll::Ready(Err(e.into())),
906902
}
907903
}
908904
m => return Poll::Ready(m),

zbus/src/azync/handshake.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use async_io::Async;
2+
use futures_core::ready;
23

34
use std::{
45
fmt::Debug,
@@ -119,16 +120,15 @@ where
119120
}
120121
Err(Error::Io(e)) => {
121122
if e.kind() == std::io::ErrorKind::WouldBlock {
122-
let poll = match handshake.next_io_operation() {
123-
IoOperation::Read => handshake.socket().poll_readable(cx),
124-
IoOperation::Write => handshake.socket().poll_writable(cx),
123+
let res = match handshake.next_io_operation() {
124+
IoOperation::Read => ready!(handshake.socket().poll_readable(cx)),
125+
IoOperation::Write => ready!(handshake.socket().poll_writable(cx)),
125126
IoOperation::None => panic!("Invalid handshake state"),
126127
};
127-
match poll {
128-
Poll::Pending => return Poll::Pending,
128+
match res {
129129
// Guess socket became ready already so let's try it again.
130-
Poll::Ready(Ok(_)) => continue,
131-
Poll::Ready(Err(e)) => return Poll::Ready(Err(e.into())),
130+
Ok(_) => continue,
131+
Err(e) => return Poll::Ready(Err(e.into())),
132132
}
133133
} else {
134134
return Poll::Ready(Err(Error::Io(e)));

zbus/src/azync/proxy.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,8 @@ where
164164
let m = self.get_mut();
165165
let (name, stream) = (m.name, m.stream.as_mut());
166166
// there must be a way to simplify the following code..
167-
let p = stream::Stream::poll_next(stream, cx);
168-
match p {
169-
Poll::Ready(Some(item)) => {
167+
match futures_core::ready!(stream::Stream::poll_next(stream, cx)) {
168+
Some(item) => {
170169
if item.0 == name {
171170
if let Some(Ok(v)) = item.1.clone().map(T::try_from) {
172171
Poll::Ready(Some(Some(v)))
@@ -177,8 +176,7 @@ where
177176
Poll::Pending
178177
}
179178
}
180-
Poll::Ready(None) => Poll::Ready(None),
181-
Poll::Pending => Poll::Pending,
179+
None => Poll::Ready(None),
182180
}
183181
}
184182
}

zbus/src/raw/connection.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,10 @@ impl Connection<Async<Box<dyn Socket>>> {
182182
Ok(()) => return Poll::Ready(Ok(())),
183183
Err(e) => {
184184
if e.kind() == ErrorKind::WouldBlock {
185-
let poll = self.socket().poll_writable(cx);
186-
187-
match poll {
188-
Poll::Pending => return Poll::Pending,
185+
match futures_core::ready!(self.socket().poll_writable(cx)) {
189186
// Guess socket became ready already so let's try it again.
190-
Poll::Ready(Ok(_)) => continue,
191-
Poll::Ready(Err(e)) => return Poll::Ready(Err(e.into())),
187+
Ok(_) => continue,
188+
Err(e) => return Poll::Ready(Err(e.into())),
192189
}
193190
} else {
194191
return Poll::Ready(Err(crate::Error::Io(e)));

0 commit comments

Comments
 (0)