Struct refinement::Refinement
source · [−]pub struct Refinement<T, P>(_, _);
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
sourceimpl<T, P> Refinement<T, P> where
P: Predicate<T>,
impl<T, P> Refinement<T, P> where
P: Predicate<T>,
sourcepub fn new(x: T) -> Option<Self>
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());
sourcepub fn into_inner(self) -> T
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());
sourceimpl<T, P> Refinement<T, P> where
T: Clone,
P: Predicate<T>,
impl<T, P> Refinement<T, P> where
T: Clone,
P: Predicate<T>,
sourcepub fn to_inner(&self) -> T
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());
sourceimpl<T, P> Refinement<T, P> where
T: Copy,
P: Predicate<T>,
impl<T, P> Refinement<T, P> where
T: Copy,
P: Predicate<T>,
sourcepub fn as_inner(&self) -> T
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
sourceimpl<T, P> Add<Refinement<T, P>> for Refinement<T, P> where
T: Add<Output = T>,
P: Predicate<T>,
impl<T, P> Add<Refinement<T, P>> for Refinement<T, P> where
T: Add<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Add<T> for Refinement<T, P> where
T: Add<Output = T>,
P: Predicate<T>,
impl<T, P> Add<T> for Refinement<T, P> where
T: Add<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> AsRef<T> for Refinement<T, P> where
P: Predicate<T>,
impl<T, P> AsRef<T> for Refinement<T, P> where
P: Predicate<T>,
sourceimpl<T, P> BitAnd<Refinement<T, P>> for Refinement<T, P> where
T: BitAnd<Output = T>,
P: Predicate<T>,
impl<T, P> BitAnd<Refinement<T, P>> for Refinement<T, P> where
T: BitAnd<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> BitAnd<T> for Refinement<T, P> where
T: BitAnd<Output = T>,
P: Predicate<T>,
impl<T, P> BitAnd<T> for Refinement<T, P> where
T: BitAnd<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> BitOr<Refinement<T, P>> for Refinement<T, P> where
T: BitOr<Output = T>,
P: Predicate<T>,
impl<T, P> BitOr<Refinement<T, P>> for Refinement<T, P> where
T: BitOr<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> BitOr<T> for Refinement<T, P> where
T: BitOr<Output = T>,
P: Predicate<T>,
impl<T, P> BitOr<T> for Refinement<T, P> where
T: BitOr<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> BitXor<Refinement<T, P>> for Refinement<T, P> where
T: BitXor<Output = T>,
P: Predicate<T>,
impl<T, P> BitXor<Refinement<T, P>> for Refinement<T, P> where
T: BitXor<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> BitXor<T> for Refinement<T, P> where
T: BitXor<Output = T>,
P: Predicate<T>,
impl<T, P> BitXor<T> for Refinement<T, P> where
T: BitXor<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Borrow<T> for Refinement<T, P> where
P: Predicate<T>,
impl<T, P> Borrow<T> for Refinement<T, P> where
P: Predicate<T>,
sourceimpl<T: Clone, P> Clone for Refinement<T, P>
impl<T: Clone, P> Clone for Refinement<T, P>
sourceimpl<T, P> Debug for Refinement<T, P> where
T: Debug,
impl<T, P> Debug for Refinement<T, P> where
T: Debug,
sourceimpl<T, P> Deref for Refinement<T, P>
impl<T, P> Deref for Refinement<T, P>
sourceimpl<T, P> DerefMut for Refinement<T, P>
impl<T, P> DerefMut for Refinement<T, P>
sourceimpl<T, P> Display for Refinement<T, P> where
T: Display,
P: Predicate<T>,
impl<T, P> Display for Refinement<T, P> where
T: Display,
P: Predicate<T>,
sourceimpl<T, P> Div<Refinement<T, P>> for Refinement<T, P> where
T: Div<Output = T>,
P: Predicate<T>,
impl<T, P> Div<Refinement<T, P>> for Refinement<T, P> where
T: Div<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Div<T> for Refinement<T, P> where
T: Div<Output = T>,
P: Predicate<T>,
impl<T, P> Div<T> for Refinement<T, P> where
T: Div<Output = T>,
P: Predicate<T>,
sourceimpl<T: Hash, P: Hash> Hash for Refinement<T, P>
impl<T: Hash, P: Hash> Hash for Refinement<T, P>
sourceimpl<T, P, I> Index<I> for Refinement<T, P> where
T: Index<I>,
P: Predicate<T>,
impl<T, P, I> Index<I> for Refinement<T, P> where
T: Index<I>,
P: Predicate<T>,
sourceimpl<T, P> Mul<Refinement<T, P>> for Refinement<T, P> where
T: Mul<Output = T>,
P: Predicate<T>,
impl<T, P> Mul<Refinement<T, P>> for Refinement<T, P> where
T: Mul<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Mul<T> for Refinement<T, P> where
T: Mul<Output = T>,
P: Predicate<T>,
impl<T, P> Mul<T> for Refinement<T, P> where
T: Mul<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Neg for Refinement<T, P> where
T: Neg<Output = T>,
P: Predicate<T>,
impl<T, P> Neg for Refinement<T, P> where
T: Neg<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Not for Refinement<T, P> where
T: Not<Output = T>,
P: Predicate<T>,
impl<T, P> Not for Refinement<T, P> where
T: Not<Output = T>,
P: Predicate<T>,
sourceimpl<T: Ord, P: Ord> Ord for Refinement<T, P>
impl<T: Ord, P: Ord> Ord for Refinement<T, P>
sourcefn cmp(&self, other: &Refinement<T, P>) -> Ordering
fn cmp(&self, other: &Refinement<T, P>) -> Ordering
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl<T: PartialEq, P: PartialEq> PartialEq<Refinement<T, P>> for Refinement<T, P>
impl<T: PartialEq, P: PartialEq> PartialEq<Refinement<T, P>> for Refinement<T, P>
sourcefn eq(&self, other: &Refinement<T, P>) -> bool
fn eq(&self, other: &Refinement<T, P>) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourceimpl<T: PartialOrd, P: PartialOrd> PartialOrd<Refinement<T, P>> for Refinement<T, P>
impl<T: PartialOrd, P: PartialOrd> PartialOrd<Refinement<T, P>> for Refinement<T, P>
sourcefn partial_cmp(&self, other: &Refinement<T, P>) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<T, P, B> RangeBounds<B> for Refinement<T, P> where
T: RangeBounds<B>,
P: Predicate<T>,
impl<T, P, B> RangeBounds<B> for Refinement<T, P> where
T: RangeBounds<B>,
P: Predicate<T>,
sourceimpl<T, P> Rem<Refinement<T, P>> for Refinement<T, P> where
T: Rem<Output = T>,
P: Predicate<T>,
impl<T, P> Rem<Refinement<T, P>> for Refinement<T, P> where
T: Rem<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Rem<T> for Refinement<T, P> where
T: Rem<Output = T>,
P: Predicate<T>,
impl<T, P> Rem<T> for Refinement<T, P> where
T: Rem<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Shl<Refinement<T, P>> for Refinement<T, P> where
T: Shl<Output = T>,
P: Predicate<T>,
impl<T, P> Shl<Refinement<T, P>> for Refinement<T, P> where
T: Shl<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Shl<T> for Refinement<T, P> where
T: Shl<Output = T>,
P: Predicate<T>,
impl<T, P> Shl<T> for Refinement<T, P> where
T: Shl<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Shr<Refinement<T, P>> for Refinement<T, P> where
T: Shr<Output = T>,
P: Predicate<T>,
impl<T, P> Shr<Refinement<T, P>> for Refinement<T, P> where
T: Shr<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Shr<T> for Refinement<T, P> where
T: Shr<Output = T>,
P: Predicate<T>,
impl<T, P> Shr<T> for Refinement<T, P> where
T: Shr<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Sub<Refinement<T, P>> for Refinement<T, P> where
T: Sub<Output = T>,
P: Predicate<T>,
impl<T, P> Sub<Refinement<T, P>> for Refinement<T, P> where
T: Sub<Output = T>,
P: Predicate<T>,
sourceimpl<T, P> Sub<T> for Refinement<T, P> where
T: Sub<Output = T>,
P: Predicate<T>,
impl<T, P> Sub<T> for Refinement<T, P> where
T: Sub<Output = T>,
P: Predicate<T>,
impl<T: Copy, P> Copy for Refinement<T, P>
impl<T: Eq, P: Eq> Eq for Refinement<T, P>
impl<T, P> StructuralEq for Refinement<T, P>
impl<T, P> StructuralPartialEq for Refinement<T, P>
Auto Trait Implementations
impl<T, P> RefUnwindSafe for Refinement<T, P> where
P: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, P> Send for Refinement<T, P> where
P: Send,
T: Send,
impl<T, P> Sync for Refinement<T, P> where
P: Sync,
T: Sync,
impl<T, P> Unpin for Refinement<T, P> where
P: Unpin,
T: Unpin,
impl<T, P> UnwindSafe for Refinement<T, P> where
P: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more