Closed janriemer closed 2 years ago
See https://github.com/rust-lang/futures-rs/pull/1803, https://github.com/smol-rs/async-channel/issues/2, and https://github.com/tokio-rs/tokio/issues/2742. I don't think it is reasonable to do this unless a feature like https://github.com/rust-lang/rust/issues/69329 is implemented.
Interesting, thank you for the related links. :heart: I didn't know NonZero types should not be used for APIs and is considered unidiomatic Rust.
Hey folks,
Note: after writing all of this below, I noticed std::slice::chunks also uses usize and panics when provided a value that is 0, which is really unfortunate, IMO. Of course, we won't be able to change the std lib and we should keep symmetry between
Stream
s and the std lib, but I'm still interested what you think of this.Issue
I've noticed that the methods
chunks
andready_chunks
take ausize
as an arg for the capacity. This seems to be a good arg type, because other "capacity" methods in the std lib takeusize
as well (e.g.Vec::with_capacity
) and so there is symmetry between them.However, there is a huge pitfall and it is actually not symmetric:
chunks
andready_chunks
will panic, when passed in a usize of 0 (according to docs: This method will panic ifcapacity
is zero.).The other
capacity
methods (e.g. onVec
in std) do not panic, when provided with the value 0 (which is sensible, because one can have a Vec that has no allocations).Proposal
Instead of using
usize
in the argument type, we should rather useNonZeroUsize
, because it guarantees a non-zero value. Note that internally we can still useusize
(to make it easier to interact with other methods, such aslen()
etc.) - this proposed change will only affect the API itself.Here is an exemplary playground to show the general idea:
Related
The following issues might be related and should be considered, when implementing this proposal:
2492 especially this comment
BREAKING
This is a breaking change, because the public API changes.