Live data from Hacker News

Zero-cost futures in Rust

aturon.github.io

11–20 of 348 posts

Re: Zero-cost futures in Rust

#12

Finally a nice async/io interface for rust, always felt that it was a big missing piece, couple of questions for peps familiar with async in other languages : 1 - Isn't the state machine approach the same as C#/.net async/await is using ? But the with the added convenience of the syntactic sugar ? 2 - no allocation : , does'nt the lambda closure need to be allocated somewhere ? 3 - I would have love some comparison (…

In Rust, closures don't need to be boxed, because they're just bytes like everything else. In this case, they can be stored by-value as a generic type in the future itself, which can be stored on the stack.

The one time that you do tend to see closures get boxed is when returning them, because you can't write out their type. In the future you'll be able to get around that by using the `impl Trait` syntax mentioned in the post (https://github.com/rust-lang/rfcs/pull/1522).

Re: Zero-cost futures in Rust

#13
post #7

Not sure if I missed this in the post, does this depend on any unstabilized features or can we use this today on 1.10.0 stable? Awesome stuff btw, love the iterator inspiration.

I think "impl Trait" is unstable.

It is, however, it's not currently actually using that syntax in the code: http://alexcrichton.com/futures-rs/futures/trait.Future.html

So, this will work on stable. It was presented that way to not get into the whole details about the whole impl trait shenanigans in general.

Re: Zero-cost futures in Rust

#14
post #7

Not sure if I missed this in the post, does this depend on any unstabilized features or can we use this today on 1.10.0 stable? Awesome stuff btw, love the iterator inspiration.

I think "impl Trait" is unstable.

Not even in nightly yet - follow https://github.com/rust-lang/rust/pull/35091 for updates (we're trying to land it this week).

Re: Zero-cost futures in Rust

#16

Is there any special handling for Futures that complete with an error? Also, how do you debug code that's hung or taking too long? It might be useful to get a list of all the jobs (incomplete Futures) that are currently running, much like running 'ps'.

Because Rust doesn't do exceptions the way other languages with futures do, there's no special handling. Just use a future representing a Result, like you would for synchronous errors.

Re: Zero-cost futures in Rust

#17

Earlier quoted context omitted.

I think "impl Trait" is unstable.

It is, however, it's not currently actually using that syntax in the code: http://alexcrichton.com/futures-rs/futures/trait.Future.html So, this will work on stable. It was presented that way to not get into the whole details about the whole impl trait shenanigans in general.

Ah, great news!

Re: Zero-cost futures in Rust

#18
post #8

Finally a nice async/io interface for rust, always felt that it was a big missing piece, couple of questions for peps familiar with async in other languages : 1 - Isn't the state machine approach the same as C#/.net async/await is using ? But the with the added convenience of the syntactic sugar ? 2 - no allocation : , does'nt the lambda closure need to be allocated somewhere ? 3 - I would have love some comparison (…

> Isn't the state machine approach the same as C#/.net async/await is using ? But the with the added convenience of the syntactic sugar ? Similar in principle, but the implementation is different. Tasks in C# are more of an OO style instead of a FP style where they turn into an enum (sum type, if you want to get theoretical). > 2 - no allocation : , does'nt the lambda closure need to be allocated somewhere ? No, not…

Yes , i only have a simple understanding of rust memory model . So what happened when/if the future escapes/outlives the current stack/context ?

Re: Zero-cost futures in Rust

#19
post #7

Not sure if I missed this in the post, does this depend on any unstabilized features or can we use this today on 1.10.0 stable? Awesome stuff btw, love the iterator inspiration.

You can indeed use this on stable Rust today! Right now 1.9.0 is the minimum supported version due to the usage of `catch_panic` in a few places.

I'd recommend a beta compiler for now though to compile some of the examples. There's a bug in the stable compiler which causes them to take up to 8x longer to compile, but beta/nightly are both speedy!

Re: Zero-cost futures in Rust

#20

Is there any special handling for Futures that complete with an error? Also, how do you debug code that's hung or taking too long? It might be useful to get a list of all the jobs (incomplete Futures) that are currently running, much like running 'ps'.

Yes -- the blog post didn't go into details about this, but Futures in general have an error type as well, and all the combinators know how to propagate errors correctly. (There's also a notion of "cancellation" for a future -- we'll get into this with later posts).

In terms of debugging, there's not infrastructure currently, but the kind of thing you're talking about should be easy to add!

Post reply on HN