[][src]Struct refinement::Refinement

pub struct Refinement<T, P>(_, _);

A Refinement type ensures all values of a particular type satisfy a Predicate.

Use as_inner/to_inner to access the underlying value or into_inner to unwrap the value.

Refinement also implements many common standard library traits if the underlying value also implements them.

Examples

use refinement::{Predicate, Refinement};

struct LessThanTen;

impl Predicate<i32> for LessThanTen {
    fn test(x: &i32) -> bool {
        *x < 10
    }
}

type LessThanTenInt = Refinement<i32, LessThanTen>;

let x = LessThanTenInt::new(5);
assert!(x.is_some());

let y = LessThanTenInt::new(11);
assert!(y.is_none());

Implementations

impl<T, P> Refinement<T, P> where
    P: Predicate<T>, 
[src]

pub fn new(x: T) -> Option<Self>[src]

Create a refined value from the underlying type T.

Returns x under the refinement type if x satisfies P, otherwise returns None.

Examples

use refinement::{Predicate, Refinement};

struct NonEmpty;

impl Predicate<String> for NonEmpty {
    fn test(x: &String) -> bool {
       !x.is_empty()
    }
}

type NonEmptyString = Refinement<String, NonEmpty>;

let s1 = NonEmptyString::new(String::from("Hello"));
assert!(s1.is_some());

let s2 = NonEmptyString::new(String::from(""));
assert!(s2.is_none());

pub fn into_inner(self) -> T[src]

Unwrap the underlying value, consuming self.

Examples

use refinement::{Predicate, Refinement};

struct ThreeDigit;

impl Predicate<String> for ThreeDigit {
    fn test(x: &String) -> bool {
       x.chars().count() == 3 && x.chars().filter(|c| c.is_ascii_digit()).count() == 3
    }
}

type ThreeDigitString = Refinement<String, ThreeDigit>;

let s = ThreeDigitString::new(String::from("123"));

assert_eq!(String::from("123"), s.unwrap().into_inner());

impl<T, P> Refinement<T, P> where
    T: Clone,
    P: Predicate<T>, 
[src]

pub fn to_inner(&self) -> T[src]

Retrieve the underlying value without consuming self.

Examples

use refinement::{Predicate, Refinement};

struct ThreeDigit;

impl Predicate<String> for ThreeDigit {
    fn test(x: &String) -> bool {
       x.chars().count() == 3 && x.chars().filter(|c| c.is_ascii_digit()).count() == 3
    }
}

type ThreeDigitString = Refinement<String, ThreeDigit>;

let s = ThreeDigitString::new(String::from("123"));

assert_eq!(String::from("123"), s.unwrap().to_inner());

impl<T, P> Refinement<T, P> where
    T: Copy,
    P: Predicate<T>, 
[src]

pub fn as_inner(&self) -> T[src]

Retrieve the underlying value for Copy types without consuming self.

Examples

use refinement::{Predicate, Refinement};

struct LessThanTen;

impl Predicate<i32> for LessThanTen {
    fn test(x: &i32) -> bool {
        *x < 10
    }
}

type LessThanTenInt = Refinement<i32, LessThanTen>;

let x = LessThanTenInt::new(5);
assert_eq!(5, x.unwrap().as_inner());

Trait Implementations

impl<T, P> Add<Refinement<T, P>> for Refinement<T, P> where
    T: Add<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the + operator.

impl<T, P> Add<T> for Refinement<T, P> where
    T: Add<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the + operator.

impl<T, P> AsRef<T> for Refinement<T, P> where
    P: Predicate<T>, 
[src]

impl<T, P> BitAnd<Refinement<T, P>> for Refinement<T, P> where
    T: BitAnd<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the & operator.

impl<T, P> BitAnd<T> for Refinement<T, P> where
    T: BitAnd<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the & operator.

impl<T, P> BitOr<Refinement<T, P>> for Refinement<T, P> where
    T: BitOr<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the | operator.

impl<T, P> BitOr<T> for Refinement<T, P> where
    T: BitOr<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the | operator.

impl<T, P> BitXor<Refinement<T, P>> for Refinement<T, P> where
    T: BitXor<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the ^ operator.

impl<T, P> BitXor<T> for Refinement<T, P> where
    T: BitXor<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the ^ operator.

impl<T, P> Borrow<T> for Refinement<T, P> where
    P: Predicate<T>, 
[src]

impl<T: Clone, P: Clone> Clone for Refinement<T, P>[src]

impl<T: Copy, P: Copy> Copy for Refinement<T, P>[src]

impl<T: Debug, P: Debug> Debug for Refinement<T, P>[src]

impl<T, P> Display for Refinement<T, P> where
    T: Display,
    P: Predicate<T>, 
[src]

impl<T, P> Div<Refinement<T, P>> for Refinement<T, P> where
    T: Div<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the / operator.

impl<T, P> Div<T> for Refinement<T, P> where
    T: Div<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the / operator.

impl<T: Eq, P: Eq> Eq for Refinement<T, P>[src]

impl<T: Hash, P: Hash> Hash for Refinement<T, P>[src]

impl<T, P, I> Index<I> for Refinement<T, P> where
    T: Index<I>,
    P: Predicate<T>, 
[src]

type Output = T::Output

The returned type after indexing.

impl<T, P> Mul<Refinement<T, P>> for Refinement<T, P> where
    T: Mul<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the * operator.

impl<T, P> Mul<T> for Refinement<T, P> where
    T: Mul<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the * operator.

impl<T, P> Neg for Refinement<T, P> where
    T: Neg<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the - operator.

impl<T, P> Not for Refinement<T, P> where
    T: Not<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the ! operator.

impl<T: Ord, P: Ord> Ord for Refinement<T, P>[src]

impl<T: PartialEq, P: PartialEq> PartialEq<Refinement<T, P>> for Refinement<T, P>[src]

impl<T: PartialOrd, P: PartialOrd> PartialOrd<Refinement<T, P>> for Refinement<T, P>[src]

impl<T, P, B> RangeBounds<B> for Refinement<T, P> where
    T: RangeBounds<B>,
    P: Predicate<T>, 
[src]

impl<T, P> Rem<Refinement<T, P>> for Refinement<T, P> where
    T: Rem<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the % operator.

impl<T, P> Rem<T> for Refinement<T, P> where
    T: Rem<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the % operator.

impl<T, P> Shl<Refinement<T, P>> for Refinement<T, P> where
    T: Shl<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the << operator.

impl<T, P> Shl<T> for Refinement<T, P> where
    T: Shl<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the << operator.

impl<T, P> Shr<Refinement<T, P>> for Refinement<T, P> where
    T: Shr<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the >> operator.

impl<T, P> Shr<T> for Refinement<T, P> where
    T: Shr<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the >> operator.

impl<T, P> StructuralEq for Refinement<T, P>[src]

impl<T, P> StructuralPartialEq for Refinement<T, P>[src]

impl<T, P> Sub<Refinement<T, P>> for Refinement<T, P> where
    T: Sub<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the - operator.

impl<T, P> Sub<T> for Refinement<T, P> where
    T: Sub<Output = T>,
    P: Predicate<T>, 
[src]

type Output = Option<Self>

The resulting type after applying the - operator.

Auto Trait Implementations

impl<T, P> RefUnwindSafe for Refinement<T, P> where
    P: RefUnwindSafe,
    T: RefUnwindSafe
[src]

impl<T, P> Send for Refinement<T, P> where
    P: Send,
    T: Send
[src]

impl<T, P> Sync for Refinement<T, P> where
    P: Sync,
    T: Sync
[src]

impl<T, P> Unpin for Refinement<T, P> where
    P: Unpin,
    T: Unpin
[src]

impl<T, P> UnwindSafe for Refinement<T, P> where
    P: UnwindSafe,
    T: UnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.