Skip to content

Instantly share code, notes, and snippets.

@themaxhero
Created August 31, 2019 15:31
Show Gist options
  • Save themaxhero/380ed9184f9ebeb1b746f0638ba497d6 to your computer and use it in GitHub Desktop.
Save themaxhero/380ed9184f9ebeb1b746f0638ba497d6 to your computer and use it in GitHub Desktop.
Crazy Stuff
use std::convert::{ Into, From };
enum Integer { I8(i8), I16(i16), I32(i32), U8(u8), U16(u16), U32(u32) }
enum Float { F32(f32), F64(f64) }
enum Number { F(Float), I(Integer) }
impl Into<f32> for Integer {
fn into(self) -> f32 {
match self {
Integer::I8(n) => return n as f32,
Integer::I16(n) => return n as f32,
Integer::I32(n) => return n as f32,
Integer::U8(n) => return n as f32,
Integer::U16(n) => return n as f32,
Integer::U32(n) => return n as f32,
}
}
}
impl Into<f32> for Float {
fn into(self) -> f32 {
match self {
Float::F32(n) => return n,
Float::F64(n) => return n as f32
}
}
}
impl Into<f32> for Number {
fn into(self) -> f32 {
match self {
Number::F(n) => return n.into(),
Number::I(n) => return n.into()
}
}
}
fn sum<T>(a: T, b: T) -> f32 where T: Into<f32> {
return a.into() + b.into();
}
fn multiply<T>(a: T, b: T) -> f32 where T: Into<f32> {
return a.into() * b.into();
}
fn subtract<T>(a: T, b: T) -> f32 where T: Into<f32> {
return a.into() - b.into();
}
fn divide<T>(a: T, b: T) -> f32 where T: Into<f32> {
return a.into() / b.into();
}
fn pow<T>(base: T, expo: T) -> f32 where T: Into<f32> {
return base.into().powf(expo.into());
}
fn sqrt(value: Number) -> f32 {
return pow(value, Number::F(Float::F32(0.5)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment