libs-team icon indicating copy to clipboard operation
libs-team copied to clipboard

Implement Step for NonZeroUxx

Open SOF3 opened this issue 1 year ago • 7 comments

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.

SOF3 avatar Oct 30 '22 09:10 SOF3

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.

SUPERCILEX avatar Dec 05 '22 07:12 SUPERCILEX

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.

SOF3 avatar Apr 18 '23 10:04 SOF3

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.

jalil-salame avatar Apr 24 '23 13:04 jalil-salame

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).

SOF3 avatar Apr 25 '23 02:04 SOF3

I was bitten by this today. I need the NonZeroUxx value inside the iteration.

marshrayms avatar Jun 26 '23 23:06 marshrayms

Is there a reason not to move forward on this or does it just need someone to actually do the work?

khuey avatar Dec 24 '23 22:12 khuey

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.

the8472 avatar Dec 24 '23 22:12 the8472