pub trait ConvertFrom<T: ?Sized> {
fn convert_from(&mut self, other: T);
}
impl<T> ConvertFrom<T> for T {
fn convert_from(&mut self, value: T) {
*self = value
}
}
pub trait ConvertTo<T> {
fn convert_to(&self, other: &mut T);
}
impl<A, B> ConvertTo<B> for A
where
for<'a> B: ConvertFrom<&'a A>,
{
fn convert_to(&self, other: &mut B) {
other.convert_from(self)
}
}
impl ConvertFrom<i32> for f32 {
fn convert_from(&mut self, other: i32) {
const MUL: f32 = 1.0 / (0x80000000i64 as f32);
*self = other as f32 * MUL
}
}
impl ConvertFrom<f32> for i32 {
fn convert_from(&mut self, other: f32) {
const MUL: f32 = 0x80000000i64 as f32;
*self = (other * MUL) as i32
}
}
impl ConvertFrom<i16> for f32 {
fn convert_from(&mut self, other: i16) {
const MUL: f32 = 1.0 / i16::MAX as f32;
*self = other as f32 * MUL
}
}
impl ConvertFrom<f32> for i16 {
fn convert_from(&mut self, other: f32) {
const MUL: f32 = i16::MAX as f32;
*self = (other * MUL) as i16
}
}