Struct Refinement

Source
pub struct Refinement<T, P>(/* private fields */);
Expand description

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§

Source§

impl<T, P> Refinement<T, P>
where P: Predicate<T>,

Source

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

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());
Source

pub fn into_inner(self) -> T

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());
Source§

impl<T, P> Refinement<T, P>
where T: Clone, P: Predicate<T>,

Source

pub fn to_inner(&self) -> T

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());
Source§

impl<T, P> Refinement<T, P>
where T: Copy, P: Predicate<T>,

Source

pub fn as_inner(&self) -> T

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§

Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: T) -> Self::Output

Performs the + operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

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

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: T) -> Self::Output

Performs the & operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: T) -> Self::Output

Performs the | operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: T) -> Self::Output

Performs the ^ operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T: Clone, P> Clone for Refinement<T, P>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, P> Debug for Refinement<T, P>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, P> Deref for Refinement<T, P>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T, P> DerefMut for Refinement<T, P>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: T) -> Self::Output

Performs the / operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: Hash, P: Hash> Hash for Refinement<T, P>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

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

Source§

type Output = <T as Index<I>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: T) -> Self::Output

Performs the * operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl<T: Ord, P: Ord> Ord for Refinement<T, P>

Source§

fn cmp(&self, other: &Refinement<T, P>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq, P: PartialEq> PartialEq for Refinement<T, P>

Source§

fn eq(&self, other: &Refinement<T, P>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd, P: PartialOrd> PartialOrd for Refinement<T, P>

Source§

fn partial_cmp(&self, other: &Refinement<T, P>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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

Source§

fn start_bound(&self) -> Bound<&B>

Start index bound. Read more
Source§

fn end_bound(&self) -> Bound<&B>

End index bound. Read more
1.35.0 · Source§

fn contains<U>(&self, item: &U) -> bool
where T: PartialOrd<U>, U: PartialOrd<T> + ?Sized,

Returns true if item is contained in the range. Read more
Source§

fn is_empty(&self) -> bool
where T: PartialOrd,

🔬This is a nightly-only experimental API. (range_bounds_is_empty)
Returns true if the range contains no items. One-sided ranges (RangeFrom, etc) always return false. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: T) -> Self::Output

Performs the % operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Self) -> Self::Output

Performs the % operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: T) -> Self::Output

Performs the << operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: Self) -> Self::Output

Performs the << operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: T) -> Self::Output

Performs the >> operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: Self) -> Self::Output

Performs the >> operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: T) -> Self::Output

Performs the - operation. Read more
Source§

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

Source§

type Output = Option<Refinement<T, P>>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Copy, P> Copy for Refinement<T, P>

Source§

impl<T: Eq, P: Eq> Eq for Refinement<T, P>

Source§

impl<T, P> StructuralPartialEq for Refinement<T, P>

Auto Trait Implementations§

§

impl<T, P> Freeze for Refinement<T, P>
where T: Freeze,

§

impl<T, P> RefUnwindSafe for Refinement<T, P>

§

impl<T, P> Send for Refinement<T, P>
where T: Send, P: Send,

§

impl<T, P> Sync for Refinement<T, P>
where T: Sync, P: Sync,

§

impl<T, P> Unpin for Refinement<T, P>
where T: Unpin, P: Unpin,

§

impl<T, P> UnwindSafe for Refinement<T, P>
where T: UnwindSafe, P: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.