Skip to content
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

piper::Sender::send does not have a struct equivalent #6

Closed
jjl opened this issue Apr 19, 2020 · 4 comments
Closed

piper::Sender::send does not have a struct equivalent #6

jjl opened this issue Apr 19, 2020 · 4 comments

Comments

@jjl
Copy link

jjl commented Apr 19, 2020

I can't seem to figure out a way to send a message on a channel without boxing. Is there something I'm missing?

@wusyong
Copy link
Contributor

wusyong commented Apr 19, 2020

Looks like modules from piper are from async_std. You can just import them directly. Does something this work for you?

fn main() -> io::Result<()> {
    smol::run(async {
        let (s, r) = async_std::sync::channel(1);
        smol::Task::spawn( async move {s.send(2).await;} ).await;
        
        let x = r.recv().await;

        Ok(())
    })
}

@jjl
Copy link
Author

jjl commented Apr 19, 2020

I'm trying to avoid having to spawn a new task.

I've started translating the current code into a Sending object. It's a bit horrific at the moment, but let's see how it goes.

@ghost
Copy link

ghost commented Apr 19, 2020

I empathize with having to box futures to get a concrete type. This will get easier once the language supports impl Trait in more positions than it currently does.

Instead of using Task::spawn(), I suggest using .boxed() instead (exists in FutureExt) like so:

let (s, r) = piper::chan(1);
let fut: BoxFuture<'_, ()> = s.send("hello").boxed();
fut.await;

Looks like modules from piper are from async_std.

They're based on async-std's implementation of channels, but improved in many ways. For example, piper's channels can have capacity of zero. There's also try_recv(). They also have a successor of of WakerSet that is based on doubly-linked list and serves tasks in FIFO order, thus improving fairness.

@ghost
Copy link

ghost commented Apr 27, 2020

This is something that will be solved by impl Trait in more places soon (hopefully!). Closing.

@ghost ghost closed this as completed Apr 27, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants