Created
June 20, 2017 22:34
-
-
Save dobkeratops/9c025f4e51295993617ae3f0ecca54ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// means of throwing ints into the typesys | |
pub struct TN7{} | |
/// means of throwing ints into the typesys | |
pub struct TN15{} | |
/// means of throwing ints into the typesys | |
pub struct TN16{} | |
/// means of throwing ints into the typesys | |
pub struct TN12{} | |
/// means of throwing ints into the typesys | |
pub struct TN4{} | |
/// means of throwing ints into the typesys | |
pub struct TN8{} | |
/// means of throwing ints into the typesys | |
pub struct TN5{} | |
/// means of throwing ints into the typesys | |
pub struct TN6{} | |
#[derive(Debug,Copy,Clone)] | |
pub struct FixedPt<T,Fraction>(T,Fraction); | |
/// getting a number from | |
pub trait TNum { | |
type Output; | |
fn value()->usize; | |
fn get_phantom()->Self::Output; | |
} | |
impl TNum for TN16{ fn value()->usize{16} type Output=TN16; fn get_phantom()->TN16{TN16{}} } | |
impl TNum for TN15{ fn value()->usize{15} type Output=TN15; fn get_phantom()->TN15{TN15{}} } | |
impl TNum for TN12{ fn value()->usize{12} type Output=TN12; fn get_phantom()->TN12{TN12{}} } | |
impl TNum for TN8{ fn value()->usize{8} type Output=TN8; fn get_phantom()->TN8{TN8{}} } | |
impl TNum for TN7{ fn value()->usize{7} type Output=TN7; fn get_phantom()->TN7{TN7{}} } | |
impl TNum for TN5{ fn value()->usize{5} type Output=TN5; fn get_phantom()->TN5{TN5{}} } | |
impl TNum for TN4{ fn value()->usize{4} type Output=TN4; fn get_phantom()->TN4{TN4{}} } | |
impl TNum for TN2{ fn value()->usize{2} type Output=TN2; fn get_phantom()->TN2{TN2{}} } | |
impl<T,X:TNum<Output=X>> From<f32> for FixedPt<T,X> where f32:Into<T>, T: ::std::ops::Mul<T,Output=T>, T:From<i32>{ | |
fn from(s:f32)->Self{ | |
let x:f32 = (1<< (<X as TNum>::value() ) ) as f32; | |
FixedPt::<T,X>((s * x).into() ,<X as TNum>::get_phantom()) | |
} | |
} | |
impl<T,X:TNum> From<FixedPt<T,X>> for f32 where T:Into<f32>{ | |
fn from(s: FixedPt<T,X> )->Self{ | |
s.0.into() * (1.0f32 / ((1<< <X as TNum>::value()) as f32)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment