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

Saturating conversion to primitives #177

Open
carbotaniuman opened this issue Nov 15, 2020 · 2 comments
Open

Saturating conversion to primitives #177

carbotaniuman opened this issue Nov 15, 2020 · 2 comments

Comments

@carbotaniuman
Copy link

I would like to request a way to convert a bigint into a smaller primitive, saturating at MAX_VALUE or MIN_VALUE if needed.

@cuviper
Copy link
Member

cuviper commented Nov 16, 2020

This seems like an unusual thing to want -- can you describe the use case where a saturating conversion would be helpful?

You can do this manually, if a little cumbersome, something like:

let small_int = bigint.to_i32().unwrap_or_else(|| {
    if bigint.sign() == Sign::Plus { i32::MAX } else { i32::MIN }
});

@carbotaniuman
Copy link
Author

carbotaniuman commented Nov 19, 2020

Sorry, I was mistaken when I created this issue. I'm looking for a masking conversion to u64 where you discard the high bits.

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

No branches or pull requests

2 participants