I want to be able to halve a generic integer type and have my function return that integer type.
use std::ops::*;
pub fn half_nonnegative<I: Shr<i32>>(n: I) -> <I as std::ops::Shr<i32>>::Output {
n >> 1
}
pub fn half_nonnegative_input_type_same_as_output<I: Shr<i32>>(n: I) -> I {
n >> 1
}
The first function halves my integer, but potentially returns a different type. The second function doesn't compile. How do I constrain the second function to have the same output type as the input type?
Should I be halving in some other way? Dividing by one() + one()?