Skip to content

Commit

Permalink
Removed failing plugin. Added unpack and cast. Failing again.
Browse files Browse the repository at this point in the history
  • Loading branch information
ambaxter committed Feb 16, 2018
1 parent bf5ecf2 commit 9ee1597
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ itertools = "0.6"
string-interner = "0.6"
ordered-float = "0.5"
float-cmp = "0.2"
interpolate_idents = "0.1.8"
#interpolate_idents = "0.1.8"
parking_lot = {version = "0.4", features = ["nightly"]}
failure = "0.1"
failure_derive = "0.1"
4 changes: 0 additions & 4 deletions src/bin/sandbox.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#![feature(conservative_impl_trait)]
#![feature(plugin)]
#![plugin(interpolate_idents)]


extern crate num;
Expand Down Expand Up @@ -179,8 +177,6 @@ enum BetaJoin {
//ANY(Vec<MemoryId>)
}

expert!(Knowledge; [Aspect]);

fn main() {
use expert::builder::KnowledgeBuilder;
use expert::builder::CData;
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#![feature(plugin)]
#![plugin(interpolate_idents)]

#[macro_use]
extern crate failure_derive;

Expand Down Expand Up @@ -36,6 +33,7 @@ pub use failure::Error;

type Result<T> = std::result::Result<T, Error>;

/*
#[macro_export]
macro_rules! expert {
($base:ident; [$($input_type:ident),+]) => {
Expand Down Expand Up @@ -82,3 +80,4 @@ macro_rules! expert {
}
}
}
*/
79 changes: 69 additions & 10 deletions src/shared/cast.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
use num::{NumCast, ToPrimitive};
use num::{NumCast, Float, ToPrimitive};
use ordered_float::NotNaN;
use num::Float;
use std::ops::Deref;

pub trait ToNotNaNPrimitive : ToPrimitive {
// Since I can't implement ToPrimitive for NotNaN, we need to unpack NotNaN (and everything else)
pub trait UnpackPrimitive {
type Output: ToNNPrimitive;

fn unpack(&self) -> Self::Output;
}

macro_rules! impl_unpack_prim {
($T:ty) => {
impl UnpackPrimitive for $T {
type Output = $T;

#[inline]
fn unpack(&self) -> Self::Output {
*self
}
}
}
}

macro_rules! impl_unpack_nnprim {
($T:ty) => {
impl<T> UnpackPrimitive for $T
where T: Float + ToNNPrimitive {
type Output = T;

#[inline]
fn unpack(&self) -> Self::Output {
*self.deref()
}
}
}
}

impl_unpack_prim!(i8);
impl_unpack_prim!(i16);
impl_unpack_prim!(i32);
impl_unpack_prim!(i64);
impl_unpack_prim!(isize);
impl_unpack_prim!(u8);
impl_unpack_prim!(u16);
impl_unpack_prim!(u32);
impl_unpack_prim!(u64);
impl_unpack_prim!(usize);
impl_unpack_nnprim!(NotNaN<T>);

pub trait ToNNPrimitive: ToPrimitive {
#[inline]
fn to_nn_f32(&self) -> Option<NotNaN<f32>> {
self.to_f32().map(|f| f.into())
Expand All @@ -16,27 +62,27 @@ pub trait ToNotNaNPrimitive : ToPrimitive {


/// An interface for casting between machine scalars.
pub trait NotNaNCast: Sized + ToNotNaNPrimitive {
pub trait NNCast: Sized + ToNNPrimitive {
type Output;
/// Creates a number from another value that can be converted into
/// a primitive via the `ToPrimitive` trait.
fn from<T: ToNotNaNPrimitive>(n: T) -> Option<Self::Output>;
fn from<T: ToNNPrimitive>(n: T) -> Option<Self::Output>;
}

#[inline]
pub fn cast<T: NotNaNCast, U: NotNaNCast>(n: T) -> Option<U::Output> {
<U as NotNaNCast>::from(n)
pub fn cast<T: ToNNPrimitive, U: NNCast>(n: T) -> Option<U::Output> {
<U as NNCast>::from(n)
}

macro_rules! impl_nn_cast {
($T:ty, $O:ty, $conv:ident) => (
impl ToNotNaNPrimitive for $T {}
impl ToNNPrimitive for $T {}

impl NotNaNCast for $T {
impl NNCast for $T {
type Output = $O;
#[inline]
#[allow(deprecated)]
fn from<N: ToNotNaNPrimitive>(n: N) -> Option<Self::Output> {
fn from<N: ToNNPrimitive>(n: N) -> Option<Self::Output> {
// `$conv` could be generated using `concat_idents!`, but that
// macro seems to be broken at the moment
n.$conv()
Expand All @@ -57,3 +103,16 @@ impl_nn_cast!(i64, i64, to_i64);
impl_nn_cast!(isize, isize, to_isize);
impl_nn_cast!(f32, NotNaN<f32>, to_nn_f32);
impl_nn_cast!(f64, NotNaN<f64>, to_nn_f64);


#[cfg(test)]
mod tests {

use ::shared::cast::{NNCast, cast};

#[test]
fn test() {
assert_eq!(Some(2u8), cast(2u16));
}

}

0 comments on commit 9ee1597

Please sign in to comment.