Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ER] Int::RANGE constants #77397

Open
leonardo-m opened this issue Oct 1, 2020 · 4 comments
Open

[ER] Int::RANGE constants #77397

leonardo-m opened this issue Oct 1, 2020 · 4 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

leonardo-m commented Oct 1, 2020

This is a small enhancement request, that (as it usually happens) I am not sure about, but I like it.

Like u32::BITS I'd like another constant for all Rust integral numbers XX (i8, u8, i16, u16, etc):

XX::RANGE: RangeInclusive<XX> = XX::MIN ..= XX::MAX;

So you can write:

for i in u8::RANGE { ... }

That's shorter, is more DRY and avoids mistakes like (that is forgetting that a signed number doesn't start at zero, and missing the last number of the range):

for i in 0 ..= i8::MAX {}
for i in i8::MIN .. i8::MAX {}

I think this could be useful now, but it will become even more handy if/when Rust adds ranged integers as in Ada, see:

https://en.wikibooks.org/wiki/Ada_Programming/Attributes/%27Range

I think it's better not to define char::RANGE.

@jonas-schievink jonas-schievink added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Oct 1, 2020
@scottmcm
Copy link
Member

scottmcm commented Oct 2, 2020

I think it's better not to define char::RANGE.

Since we now have char: Step, can you elaborate why? It seems more plausible to iterate that than to iterate u64::RANGE (let alone u128::RANGE)...

@leonardo-m
Copy link
Author

leonardo-m commented Oct 2, 2020

If you think that iterating on chars like that could be done efficiently, then ignore my comment, I've commented out that line. (But so far in my code far I've never had to scan a range of chars like that (scanning a small subrange of chars is reasonably common)).

It seems more plausible to iterate that than to iterate u64::RANGE (let alone u128::RANGE)

I have some scans of ranges (in mathematical code), in loops where I look for the first integer value that satisfies a predicate. So scanning a usize or u64 isn't uncommon for me because I have a stopping condition. (But still using u64::RANGE isn't going to be common because most times I don't start from zero, but from 2 or some other small value).

As I hinted in the first post, scanning a full range of values could be more common once/if Rust adds a syntax to define subranges, like:

type MyIdx = usize[1 ..= 12];

At that point using MyIdx::RANGE could be even more common.

@scottmcm
Copy link
Member

scottmcm commented Oct 2, 2020

where I look for the first integer value that satisfies a predicate

Even then, though, I'd probably just use 0.. -- I'm not going to finish scanning all the u64s anyway, so if there isn't one I'm in trouble regardless.

If this is mostly dependent on subrange types, then I think this should just be a note in an item in the rfcs repo, since as I understand rust-lang/rfcs#2979 they're looking at cleaning up issues here. Or if you think this is useful enough now to pull its weight it can just be a PR. (But of course I'm not on libs, so I might be wrong.)

@leonardo-m
Copy link
Author

Or if you think this is useful enough now to pull its weight it can just be a PR.

You are a kind person. I don't know if this could be useful enough. Perhaps not. Others need to judge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants