Re:Getting Started with Tokio
by silluk on Saturday December 24, 2016 @03:09PM
(#13251411)
I've been following futures since the announcement blog post came out (and had been using rust for several months before that) and I've never found a group of crates that intermix so well together. Simple stuff like tokio-tls implementing the Io trait from tokio-core so it can easily be used in bind_transport to secure the connection makes working with tokio and futures so nice. Thanks so much to the people working on this!
Re:Getting Started with Tokio
by mrcsparker on Saturday December 24, 2016 @03:07PM
(#13251404)
Thanks for writing this. I often find it difficult to wrap my head around some of these crates if I'm unfamiliar with the problem space. Often times if you haven't tried building a server before you won't know why you would use a crate like this. Having a high level overview of what you might use it for and a simple walked through example is really helpful.
Re:Getting Started with Tokio
by pimeys on Saturday December 24, 2016 @03:28PM
(#13251492)
I'm mostly doing IO in these services and now I scale them up with threads and processes. I'm not using CPU so much so I could live with a small amount of threads doing async IO, because I don't need to process the messages in sync anyways. I have plenty of RAM to spare and could live with bigger memory usage.
Re:Getting Started with Tokio
by steveklabnik on Saturday December 24, 2016 @03:52PM
(#13251575)
In general, Future is a trait, which means if you want to return a Future, you have to either create a trait object, or use the nightly-only "-> impl Trait". Boxing is the most straightforward way of creating a trait object.
(I haven't dug into the specifics of this specific example; that's what I assumed when I read that line. Maybe the author also knew this rule of thumb and did it unnecessarily...)
Re:Getting Started with Tokio
by Manishearth on Saturday December 24, 2016 @08:46PM
(#13252449)
I'm not really sure if that advice is accurate. For both iterators and futures, in most cases you will be returning a concrete type which can be named. You only really need to box it if you have a closure involved (and if you have a complicated adapter chain it becomes cleaner to box/impl trait it)
E.g. in this case there's no specific compulsion to use a trait object.
I wouldn't consider this to be a rule of thumb; it's more like "if you have trouble naming the return type try using Box<Trait>"
Re:Getting Started with Tokio
by Manishearth on Sunday December 25, 2016 @01:03AM
(#13253167)
Right, but in most cases returning a named type is fine. Usually with futures/iterators I've seen that you end up returning a new custom type that implements the trait, and often it's okay to just name it.
Re:Getting Started with Tokio
by Rusky on Sunday December 25, 2016 @12:05AM
(#13253019)
It's not really existential types in the general sense. A function returning `impl SomeTrait` can still only return values of a single type, it's just that the compiler infers that type from the function body and then restricts the caller to accessing that value via the methods in `SomeTrait`.
Re:Getting Started with Tokio
by thinkpad20 on Sunday December 25, 2016 @06:20PM
(#13255960)
Yeah that's what I was thinking; e.g. in Haskell you can use existential types to refer to "some type which implements this type class", and then an object of that type will have the functions in that type class available to it and no others (more or less). Cool that rust has this as well, I'm excited to learn about it.
Re:Getting Started with Tokio
by Rusky on Sunday December 25, 2016 @09:42PM
(#13256634)
Rust has something else more general that's much closer to existentials- you can use trait objects to erase a value's type and restrict methods on it to those in the trait.
The difference between trait objects and `impl Trait` is that trait objects can hold any value that implements a trait, since they're implemented via dynamic dispatch.
Re:Getting Started with Tokio
by lukes386 on Saturday December 24, 2016 @05:31PM
(#13251895)
Yep, you're right, a plain FutureResult works fine in this case. I was aware of the general rule about boxing futures, but didn't realize it was unnecessary in this case. Thanks for pointing it out! I'll update the post.
Thank you for the whole team.
What is the reason to box the futures? futures::finished and futures::failed both return FutureResult.
(I haven't dug into the specifics of this specific example; that's what I assumed when I read that line. Maybe the author also knew this rule of thumb and did it unnecessarily...)
E.g. in this case there's no specific compulsion to use a trait object.
I wouldn't consider this to be a rule of thumb; it's more like "if you have trouble naming the return type try using Box<Trait>"
The name of said type can get real hairy real fast, as well.
Can someone point me to more info on this? Possibly akin to existential types in Haskell?
The difference between trait objects and `impl Trait` is that trait objects can hold any value that implements a trait, since they're implemented via dynamic dispatch.