Closed
Description
Equal to https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.size_hint. Don't think this needs to be marked async
. This would've been useful to calculate the right pre-alloc size for calling collect
into a vec in #125. Thanks!
Activity
taiki-e commentedon Aug 31, 2019
Could you explain the value of
size_hint
that cannot be implemented manually?yoshuawuyts commentedon Aug 31, 2019
@taiki-e I feel its mostly API consistency?
Though with iter size hint it's useful when pre-allocating structures. For example when using
collect
for vec it would've been useful to get an estimate of how much to allocate. Right now we start with an empty vector and allocate as we go, which is not nearly as efficient as it could be.taiki-e commentedon Aug 31, 2019
The problem is that
size_hint
always returns the default value becauseasync_std::stream::Stream
cannot be implemented manually. So, currently, callingsize_hint
for optimization is a needless operation.taiki-e commentedon Aug 31, 2019
IMO, we should wait for the upstream to add size_hint, instead of adding such a “useless API”.
yoshuawuyts commentedon Sep 1, 2019
@taiki-e ah yeah, fair point.
Though I don't disagree, I do see value in implementing all APIs sooner rather than later though. This way we can have the external API that folks can call, which can be incorporated in app logic — and later when we fix the impl nobody needs to rewrite their code.
Or well, that's the theory.
yoshuawuyts commentedon Sep 3, 2019
Yay, rust-lang/futures-rs#1853 landed!
yoshuawuyts commentedon Sep 18, 2019
We're now blocked on
futures-preview@0.3.0-alpha.19
landing to implement this.yoshuawuyts commentedon Sep 27, 2019
Sweet, this has landed! This means we can now rely on
size_hint
for optimizing allocations inFromStream
impls!cc/ @sunjay