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

Add sus::num::Bounded* types #216

Open
danakj opened this issue Feb 27, 2023 · 2 comments
Open

Add sus::num::Bounded* types #216

danakj opened this issue Feb 27, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@danakj
Copy link
Collaborator

danakj commented Feb 27, 2023

Like https://doc.rust-lang.org/std/num/index.html

They should be NeverValueField types so they don't require an extra bool in Option.

@danakj danakj added the enhancement New feature or request label Feb 27, 2023
@danakj
Copy link
Collaborator Author

danakj commented Jun 13, 2023

I think a more general thing here would be integral types with a fixed range. NonZeroI8 is just an i8 with range 1..=MAX. But it'd be nice to instead have BoundedI8<1, i8::MAX> or something. The compiler can optimize on the bounds then, as .get() could __assume() stuff about the returned value?

The NeverValueField would be the min_bound-1 or max_bound+1, assuming either one is not MIN/MAX.

Bounded type A should implicitly convert to Bounded type B if B has bounds which cover A, regardless of the actual storage type.

@danakj danakj changed the title Add sus::num::NonZero* types Add sus::num::Bounded* types Jun 13, 2023
@danakj
Copy link
Collaborator Author

danakj commented Jun 13, 2023

The compiler can optimize on the bounds then, as .get() could __assume() stuff about the returned value?

Yes, as long as the method that returns the inner value is inlined, then the caller will optimize based on the bounds in Clang and GCC (MSVC does poorly but can clearly be improved).

https://godbolt.org/z/dxTn8vxKM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant