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

WIP: Rewrite examples with async. #1228

Merged
merged 4 commits into from
Jul 9, 2019

Conversation

rubdos
Copy link
Contributor

@rubdos rubdos commented Jun 29, 2019

Motivation

We would love to get community involvement in this work. There is a lot to do. The best way to get involved is to:

The examples should be updated to idiomatic async / await syntax.

The first alpha release will happen once the examples have been updated.

Solution

I started rewriting the examples using the new #[tokio::main] and async and await keywords. I will have every example in it's own commit, such that you can cherry pick what you'd like to keep.

I'm on Gitter and IRC, so feel free to ping me there.

@rubdos rubdos force-pushed the examples-std-future branch from 170abc3 to d842e5b Compare June 29, 2019 14:55
tokio/examples/echo-udp.rs Outdated Show resolved Hide resolved
@rubdos rubdos force-pushed the examples-std-future branch from d842e5b to f2562db Compare June 29, 2019 15:18
@rubdos
Copy link
Contributor Author

rubdos commented Jun 29, 2019

I think these three examples are as far as it gets right now:

  • Anything TCP basically depends on Streams (i.e., incoming feature for tokio-tcp), which is to be decided. Without the incoming feature, you need to manually use the poll_accept method, which is quite counter productive.
  • Codecs still seem to be broken, and they also depend on Streams and Sinks.

I'll still have a look at blocking.rs and manual_runtime.rs, but especially the latter is not my personal use case, so I might have no clue :-)

Everything else seems to fall in the above categories.

@rubdos
Copy link
Contributor Author

rubdos commented Jun 29, 2019

blocking.rs depends on a threadpool's block_on_all, which is currently still commented out.

@rubdos
Copy link
Contributor Author

rubdos commented Jun 29, 2019

manual_runtime.rs depends on timer support, but that's soon there.

@carllerche
Copy link
Member

Thanks for doing this work.

re: incoming, I would rewrite the examples to not use streaming (which works better with async / await). For example:

let listener = TcpListener::bind(...)?;

loop {
    let (socket, _) = listener.accept().await?;
    // do something with the socket.
}

I'll review the rest shortly. Thanks again.

@blckngm
Copy link
Contributor

blckngm commented Jul 3, 2019

I updated the proxy example in the TCP split PR: link, but did not include it eventually. The usage of incoming can be replaced with accept. But it also needs try_join.

@rubdos
Copy link
Contributor Author

rubdos commented Jul 3, 2019

I updated the proxy example in the TCP split PR: link, but did not include it eventually.

Mind recovering that commit and filing a merge request on my merge request? I'm not sure whether I can pull that commit of GH. Commit is 0b1608c

rubdos added 4 commits July 3, 2019 16:25
Instead of implementing Future manually, we can now make use of
combinators and `async`.
@rubdos rubdos force-pushed the examples-std-future branch from 547cdbc to 015e1d3 Compare July 3, 2019 14:28
@rubdos
Copy link
Contributor Author

rubdos commented Jul 3, 2019

Hmm, I have the commit now, but there's a lot of things going on that I'm afraid to touch without permission for proxy:

  • tokio-tcp requires default = ["incoming"]
  • tokio requires futures-preview = "0.3.0-alpha.16"

EDIT: cfr commit 4487254

@rubdos
Copy link
Contributor Author

rubdos commented Jul 3, 2019

Thanks for doing this work.

re: incoming, I would rewrite the examples to not use streaming (which works better with async / await). For example:

let listener = TcpListener::bind(...)?;

loop {
    let (socket, _) = listener.accept().await?;
    // do something with the socket.
}

I'll review the rest shortly. Thanks again.

You didn't mean to rewrite the Tcp examples with fn accept(), right? Because I cannot find any accept() :-)

error[E0599]: no method named `accept` found for type `tokio_tcp::listener::TcpListener` in the current scope
  --> tokio/examples/proxy.rs:48:29
   |
48 |         let stream = socket.accept().await.unwrap()?;
   |                             ^^^^^^

@carllerche
Copy link
Member

@rubdos You are correct! I have a branch in progress, but I ended up going into a yak shave as the necessary tooling to write tests weren't in place yet. I will try to get that up today.

@blckngm
Copy link
Contributor

blckngm commented Jul 3, 2019

Hmm, I have the commit now, but there's a lot of things going on that I'm afraid to touch without permission for proxy:

* `tokio-tcp` requires `default = ["incoming"]`

* `tokio` requires `futures-preview = "0.3.0-alpha.16"`

EDIT: cfr commit 4487254

Yeah, maybe it's not time yet. It can be adopted once the incoming/accept method is settled, and try_join is implemented or allowed to be imported from futures/futures-utils.

Also, feel free to come up with something entirely different. It's by no means the only way to write this example.

@carllerche
Copy link
Member

I have posted #1242.

@rubdos
Copy link
Contributor Author

rubdos commented Jul 4, 2019

Nice! Expect TCP examples coming in tonight!

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to move forward with this as is. Follow up work can happen in new PRs. Thanks again 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants