libs-team
libs-team copied to clipboard
Implement Step for NonZeroUxx
Proposal
Problem statement
Ranges of non-zero unsigned integers does not contain zero, and should be step-able.
Motivation, use-cases
I want to iterate over a Range<NonZeroUsize>
.
Solution sketches
It might be tricky to implement backward_checked
properly since we cannot guarantee that the input usize
is less than start
.
It is also inconsistent that NonZeroIxx are not implemented.
Links and related work
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
If all you want to do is iterate, why can't you use the underlying type? Though in general I don't see a reason to not implement step, so this seems like a good idea.
In terms of the ACP, it should be mentioned that ranges need to implement step for them to be iterable, hence why this ACP is necessary.
If all you want to do is iterate, why can't you use the underlying type?
That's my alternative solution now.
https://github.com/SOF3/dynec/blob/bade2b3083f9348b740dd83e34af4478dadee353/src/entity/raw.rs#L68-L73
The range is used as a parameter to a BTreeSet<NonZeroU32>
, which has a range
associated function that accepts a Range
. As for why it doesn't just contain BTreeSet<u32>
... I just want the types to be as explicit as possible.
I posted about this in the Internals Forum.
There I state another (although a bit esoteric) use case: when solving Sudokus letting 0
mean an empty cell is quite natural, you can make it explicit using Option<NonZeroU*>, but when you want to iterate over the values, using the underlying type is painful, as stated before.
In my use case, I was developing an ECS library where entity IDs are always nonzero (to optimize Option<Entity>
memory usage), but I frequently have the need to iterate over a range of allocated entities (for SIMD purposes).
I was bitten by this today. I need the NonZeroUxx value inside the iteration.
Is there a reason not to move forward on this or does it just need someone to actually do the work?
libs-api discusses change proposals during meetings, there's a backlog and people open new ones almost as fast as they get processed so it just takes a while.
In principle the ACP process is optional and one can make a PR instead, but then one may be faced with the same questions about motivation, alternatives, etc. especially when the reviewer is unsure if the addition makes sense.