num-bigint
num-bigint copied to clipboard
Saturating conversion to primitives
I would like to request a way to convert a bigint into a smaller primitive, saturating at MAX_VALUE or MIN_VALUE if needed.
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 }
});
Sorry, I was mistaken when I created this issue. I'm looking for a masking conversion to u64 where you discard the high bits.