Skip to content

WIP feat: Add basic reflection #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use std::fmt;
use std::marker::PhantomData;

use reflection;
use Predicate;

/// Predicate that combines two `Predicate`s, returning the AND of the results.
Expand Down Expand Up @@ -55,6 +56,21 @@ where
}
}

impl<M1, M2, Item> reflection::PredicateReflection for AndPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
M2: Predicate<Item>,
Item: ?Sized,
{
fn children<'a>(&'a self) -> Box<Iterator<Item = reflection::Child<'a>> + 'a> {
let params = vec![
reflection::Child::new("left", &self.a),
reflection::Child::new("right", &self.b),
];
Box::new(params.into_iter())
}
}

impl<M1, M2, Item> fmt::Display for AndPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
Expand Down Expand Up @@ -108,6 +124,21 @@ where
}
}

impl<M1, M2, Item> reflection::PredicateReflection for OrPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
M2: Predicate<Item>,
Item: ?Sized,
{
fn children<'a>(&'a self) -> Box<Iterator<Item = reflection::Child<'a>> + 'a> {
let params = vec![
reflection::Child::new("left", &self.a),
reflection::Child::new("right", &self.b),
];
Box::new(params.into_iter())
}
}

impl<M1, M2, Item> fmt::Display for OrPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
Expand Down Expand Up @@ -156,6 +187,17 @@ where
}
}

impl<M, Item> reflection::PredicateReflection for NotPredicate<M, Item>
where
M: Predicate<Item>,
Item: ?Sized,
{
fn children<'a>(&'a self) -> Box<Iterator<Item = reflection::Child<'a>> + 'a> {
let params = vec![reflection::Child::new("predicate", &self.inner)];
Box::new(params.into_iter())
}
}

impl<M, Item> fmt::Display for NotPredicate<M, Item>
where
M: Predicate<Item>,
Expand Down
14 changes: 14 additions & 0 deletions src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use std::fmt;

use reflection;
use Predicate;

/// `Predicate` that wraps another `Predicate` as a trait object, allowing
Expand Down Expand Up @@ -40,6 +41,19 @@ where
}
}

impl<Item> reflection::PredicateReflection for BoxPredicate<Item>
where
Item: ?Sized,
{
fn parameters<'a>(&'a self) -> Box<Iterator<Item = reflection::Parameter<'a>> + 'a> {
self.0.parameters()
}

fn children<'a>(&'a self) -> Box<Iterator<Item = reflection::Child<'a>> + 'a> {
self.0.children()
}
}

impl<Item> fmt::Display for BoxPredicate<Item>
where
Item: ?Sized,
Expand Down
8 changes: 8 additions & 0 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use std::fmt;
use std::marker::PhantomData;

use reflection;
use Predicate;

/// Predicate that always returns a constant (boolean) result.
Expand All @@ -28,6 +29,13 @@ impl<Item> Predicate<Item> for BooleanPredicate<Item> {
}
}

impl<Item> reflection::PredicateReflection for BooleanPredicate<Item> {
fn parameters<'a>(&'a self) -> Box<Iterator<Item = reflection::Parameter<'a>> + 'a> {
let params = vec![reflection::Parameter::new("value", &self.retval)];
Box::new(params.into_iter())
}
}

impl<Item> fmt::Display for BooleanPredicate<Item> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.retval)
Expand Down
4 changes: 2 additions & 2 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::fmt;
use reflection;

/// Trait for generically evaluating a type against a dynamically created
/// predicate function.
Expand All @@ -15,7 +15,7 @@ use std::fmt;
/// mean that the evaluated item is in some sort of pre-defined set. This is
/// different from `Ord` and `Eq` in that an `item` will almost never be the
/// same type as the implementing `Predicate` type.
pub trait Predicate<Item: ?Sized>: fmt::Display {
pub trait Predicate<Item: ?Sized>: reflection::PredicateReflection {
/// Execute this `Predicate` against `variable`, returning the resulting
/// boolean.
fn eval(&self, variable: &Item) -> bool;
Expand Down
17 changes: 12 additions & 5 deletions src/float/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::fmt;
use float_cmp::ApproxEq;
use float_cmp::Ulps;

use reflection;
use Predicate;

/// Predicate that ensures two numbers are "close" enough, understanding that rounding errors
Expand Down Expand Up @@ -86,13 +87,19 @@ impl Predicate<f64> for IsClosePredicate {
}
}

impl reflection::PredicateReflection for IsClosePredicate {
fn parameters<'a>(&'a self) -> Box<Iterator<Item = reflection::Parameter<'a>> + 'a> {
let params = vec![
reflection::Parameter::new("epsilon", &self.epsilon),
reflection::Parameter::new("ulps", &self.ulps),
];
Box::new(params.into_iter())
}
}

impl fmt::Display for IsClosePredicate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"var ~= {} +/- {} ({})",
self.target, self.epsilon, self.ulps
)
write!(f, "var ~= {}", self.target)
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use std::fmt;
use std::marker::PhantomData;

use reflection;
use Predicate;

/// Predicate that wraps a function over a reference that returns a `bool`.
Expand Down Expand Up @@ -60,6 +61,12 @@ where
}
}

impl<F, T> reflection::PredicateReflection for FnPredicate<F, T>
where
F: Fn(&T) -> bool,
{
}

impl<F, T> fmt::Display for FnPredicate<F, T>
where
F: Fn(&T) -> bool,
Expand Down
65 changes: 49 additions & 16 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::fmt;
use std::hash::Hash;
use std::iter::FromIterator;

use reflection;
use Predicate;

/// Predicate that returns `true` if `variable` is a member of the pre-defined
Expand All @@ -29,7 +30,7 @@ pub struct InPredicate<T>
where
T: PartialEq + fmt::Debug,
{
inner: Vec<T>,
inner: reflection::DebugAdapter<Vec<T>>,
}

impl<T> InPredicate<T>
Expand Down Expand Up @@ -60,9 +61,11 @@ where
/// assert_eq!(true, predicate_fn.eval("c"));
/// ```
pub fn sort(self) -> OrdInPredicate<T> {
let mut items = self.inner;
let mut items = self.inner.debug;
items.sort();
OrdInPredicate { inner: items }
OrdInPredicate {
inner: reflection::DebugAdapter::new(items),
}
}
}

Expand All @@ -71,7 +74,7 @@ where
T: PartialEq + fmt::Debug,
{
fn eval(&self, variable: &T) -> bool {
self.inner.contains(variable)
self.inner.debug.contains(variable)
}
}

Expand All @@ -80,7 +83,17 @@ where
T: PartialEq + fmt::Debug + ?Sized,
{
fn eval(&self, variable: &T) -> bool {
self.inner.contains(&variable)
self.inner.debug.contains(&variable)
}
}

impl<T> reflection::PredicateReflection for InPredicate<T>
where
T: PartialEq + fmt::Debug,
{
fn parameters<'a>(&'a self) -> Box<Iterator<Item = reflection::Parameter<'a>> + 'a> {
let params = vec![reflection::Parameter::new("values", &self.inner)];
Box::new(params.into_iter())
}
}

Expand All @@ -89,7 +102,7 @@ where
T: PartialEq + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "var in {:?}", self.inner)
write!(f, "var in values")
}
}

Expand Down Expand Up @@ -128,7 +141,7 @@ where
I: IntoIterator<Item = T>,
{
InPredicate {
inner: Vec::from_iter(iter),
inner: reflection::DebugAdapter::new(Vec::from_iter(iter)),
}
}

Expand All @@ -146,15 +159,15 @@ pub struct OrdInPredicate<T>
where
T: Ord + fmt::Debug,
{
inner: Vec<T>,
inner: reflection::DebugAdapter<Vec<T>>,
}

impl<T> Predicate<T> for OrdInPredicate<T>
where
T: Ord + fmt::Debug,
{
fn eval(&self, variable: &T) -> bool {
self.inner.binary_search(variable).is_ok()
self.inner.debug.binary_search(variable).is_ok()
}
}

Expand All @@ -163,7 +176,17 @@ where
T: Ord + fmt::Debug + ?Sized,
{
fn eval(&self, variable: &T) -> bool {
self.inner.binary_search(&variable).is_ok()
self.inner.debug.binary_search(&variable).is_ok()
}
}

impl<T> reflection::PredicateReflection for OrdInPredicate<T>
where
T: Ord + fmt::Debug,
{
fn parameters<'a>(&'a self) -> Box<Iterator<Item = reflection::Parameter<'a>> + 'a> {
let params = vec![reflection::Parameter::new("values", &self.inner)];
Box::new(params.into_iter())
}
}

Expand All @@ -172,7 +195,7 @@ where
T: Ord + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "var in {:?}", self.inner)
write!(f, "var in values")
}
}

Expand All @@ -190,15 +213,15 @@ pub struct HashableInPredicate<T>
where
T: Hash + Eq + fmt::Debug,
{
inner: HashSet<T>,
inner: reflection::DebugAdapter<HashSet<T>>,
}

impl<T> Predicate<T> for HashableInPredicate<T>
where
T: Hash + Eq + fmt::Debug,
{
fn eval(&self, variable: &T) -> bool {
self.inner.contains(variable)
self.inner.debug.contains(variable)
}
}

Expand All @@ -207,7 +230,17 @@ where
T: Hash + Eq + fmt::Debug + ?Sized,
{
fn eval(&self, variable: &T) -> bool {
self.inner.contains(&variable)
self.inner.debug.contains(&variable)
}
}

impl<T> reflection::PredicateReflection for HashableInPredicate<T>
where
T: Hash + Eq + fmt::Debug,
{
fn parameters<'a>(&'a self) -> Box<Iterator<Item = reflection::Parameter<'a>> + 'a> {
let params = vec![reflection::Parameter::new("values", &self.inner)];
Box::new(params.into_iter())
}
}

Expand All @@ -216,7 +249,7 @@ where
T: Hash + Eq + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "var in {:?}", self.inner)
write!(f, "var in values")
}
}

Expand Down Expand Up @@ -249,6 +282,6 @@ where
I: IntoIterator<Item = T>,
{
HashableInPredicate {
inner: HashSet::from_iter(iter),
inner: reflection::DebugAdapter::new(HashSet::from_iter(iter)),
}
}
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
//! *variable == 42
//! }
//! }
//! impl predicates::reflection::PredicateReflection for IsTheAnswer {}
//! impl fmt::Display for IsTheAnswer {
//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//! write!(f, "var.is_the_answer()")
Expand Down Expand Up @@ -101,9 +102,10 @@ extern crate regex;
pub mod prelude;

mod core;
pub use core::Predicate;
pub use core::*;
mod boxed;
pub use boxed::BoxPredicate;
pub use boxed::*;
pub mod reflection;

// core predicates
pub mod constant;
Expand Down
Loading