>You can ignore all of the Box stuff; the reasoning behind that isn't terribly important right now. What is the reason to box the futures? futures::finished and futures::failed both return FutureResult.
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...)
Getting Started with Tokio
11–20 of 22 posts
Re: Getting Started with Tokio
#12Re: Getting Started with Tokio
#13Earlier quoted context omitted.
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...)
In general, yes. But here I don't see why it's not possible to use FutureResult instead of BoxFuture.
Re: Getting Started with Tokio
#14>You can ignore all of the Box stuff; the reasoning behind that isn't terribly important right now. What is the reason to box the futures? futures::finished and futures::failed both return FutureResult.
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...)
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"
Re: Getting Started with Tokio
#15Earlier quoted context omitted.
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...)
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;…
The name of said type can get real hairy real fast, as well.
Re: Getting Started with Tokio
#16>You can ignore all of the Box stuff; the reasoning behind that isn't terribly important right now. What is the reason to box the futures? futures::finished and futures::failed both return FutureResult.
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...)
Can someone point me to more info on this? Possibly akin to existential types in Haskell?
Re: Getting Started with Tokio
#17Earlier quoted context omitted.
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...)
> use the nightly-only "-> impl Trait" Can someone point me to more info on this? Possibly akin to existential types in Haskell?
Re: Getting Started with Tokio
#18Earlier quoted context omitted.
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;…
Just because a type can be named doesn't mean that returning the named type properly expresses your intent. The name of said type can get real hairy real fast, as well.
Re: Getting Started with Tokio
#19Earlier quoted context omitted.
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...)
> use the nightly-only "-> impl Trait" Can someone point me to more info on this? Possibly akin to existential types in Haskell?
Re: Getting Started with Tokio
#20Earlier quoted context omitted.
Just because a type can be named doesn't mean that returning the named type properly expresses your intent. The name of said type can get real hairy real fast, as well.
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.