I am trying to create a generic function as below:
use std::ops::Add;
use std::ops::DivAssign;
fn normalize<T>(vec: &mut Vec<Vec<T>>, bd: u32)
where
T: DivAssign + Add<Output = T> + From<i32> + From<i16>,
{
let max_value = match bd {
16 => T::from(i16::MAX) + T::from(1_i16),
32 => T::from(i32::MAX) + T::from(1_i32),
_ => panic!("unsuported bit depth"),
};
for ch in 0..vec.len() {
vec[ch].iter_mut().for_each(|x| *x /= max_value);
}
}
When I call the function I am getting an error:
normalize::<f32>(&mut vec![vec![1_f32,2_f32,3_f32]], 16_u32);
the trait bound `f32: From<i32>` is not satisfied
the following implementations were found:
<f32 as From<i16>>
<f32 as From<i8>>
<f32 as From<u16>>
<f32 as From<u8>>rustcE0277
misc.rs(19, 38): required by a bound in `normalize`
Could someone explain me what's going on and how can I fix it?