8000 Significantly improve futures usability by austinjones · Pull Request #54 · austinjones/postage-rs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Significantly improve futures usability #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ parking_lot = "0.12"
futures-test = "0.3"
tokio = { version = "1.0", features = ["rt", "rt-multi-thread", "macros", "time", "sync"] }
async-std = { version = "1.9", features = ["attributes"] }
futures = { version = "0.3", default-features = false }
criterion = "0.3"

[[bench]]
Expand Down Expand Up @@ -71,3 +72,4 @@ harness = false
[[bench]]
name = "async_std_channel"
harness = false

37 changes: 37 additions & 0 deletions examples/futures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use futures::{SinkExt, StreamExt};
use postage::{mpsc, sink::SendError};

// ignore this.. build issue hack
macro_rules! tokio_main_if {
($feature:expr => async fn main() $body:block) => {
#[tokio::main]
async fn main() {
#[cfg(feature = $feature)]
inner().await;
}

#[cfg(feature = $feature)]
async fn inner() {
$body
}
};
}

tokio_main_if! ("futures-traits" => async fn main() {
let (mut tx, mut rx) = mpsc::channel(2);

tx.send(0usize).await.ok();
tx.send(1usize).await.ok();

println!("Sender says {:?}", rx.next().await);
println!("Sender says {:?}", rx.next().await);

// Let's deal with some errors.
tx.send(0usize).await.ok();
tx.send(1usize).await.ok();

// If the channel is closed, no value is returned.
// Unfortunately this is due to the design of futures::Sink
drop(rx);
assert_eq!(Err(SendError(0usize)), tx.send(0usize).await);
});
10 changes: 7 additions & 3 deletions src/channels/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ mod impl_futures {
use std::task::Poll;

impl<T> futures::sink::Sink<T> for super::Sender<T> {
type Error = SendError<()>;
type Error = SendError<T>;

fn poll_ready(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
loop {
if self.shared.is_closed() {
return Poll::Ready(Err(SendError(())));
return Poll::Ready(Ok(()));
}

let queue = &self.shared.extension().queue;
Expand All @@ -121,12 +121,16 @@ mod impl_futures {
}

fn start_send(self: std::pin::Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
if self.shared.is_closed() {
return Err(SendError(item));
}

let result = self
.shared
.extension()
.queue
.push(item)
.map_err(|_t| SendError(()));
.map_err(|item| SendError(item));

if result.is_ok() {
self.shared.notify_receivers();
Expand Down
10 changes: 7 additions & 3 deletions src/channels/mpsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ mod impl_futures {
use std::task::Poll;

impl<T> futures::sink::Sink<T> for super::Sender<T> {
type Error = SendError<()>;
type Error = SendError<T>;

fn poll_ready(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
loop {
if self.shared.is_closed() {
return Poll::Ready(Err(SendError(())));
return Poll::Ready(Ok(()));
}

let queue = &self.shared.extension().queue;
Expand All @@ -118,12 +118,16 @@ mod impl_futures {
}

fn start_send(self: std::pin::Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
if self.shared.is_closed() {
return Err(SendError(item));
}

let result = self
.shared
.extension()
.queue
.push(item)
.map_err(|_t| SendError(()));
.map_err(|item| SendError(item));

if result.is_ok() {
self.shared.notify_receivers();
Expand Down
8 changes: 2 additions & 6 deletions src/channels/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,18 @@ mod impl_futures {
use crate::sink::SendError;

impl<T> futures::sink::Sink<T> for super::Sender<T> {
type Error = SendError<()>;
type Error = SendError<T>;

fn poll_ready(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
if self.shared.is_closed() {
return Poll::Ready(Err(SendError(())));
}

Poll::Ready(Ok(()))
}

fn start_send(self: std::pin::Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
if self.shared.is_closed() {
return Err(SendError(()));
return Err(SendError(item));
}

self.shared.extension().push(item);
Expand Down
4 changes: 2 additions & 2 deletions src/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ mod sink_tests {

drop(rx);
assert_eq!(
Poll::Ready(Err(SendError(()))),
Poll::Ready(Ok(())),
Pin::new(&mut tx).poll_ready(&mut std_cx)
);
assert_eq!(Err(SendError(())), Pin::new(&mut tx).start_send($val));
assert_eq!(Err(SendError($val)), Pin::new(&mut tx).start_send($val));
};
}

Expand Down
0