Struct tightness::Bounded [−][src]
pub struct Bounded<T, B: Bound<Target = T>>(_, _);
Expand description
A bounded type, i.e. a thin wrapper around an inner type that guarantees a
specific invariant is always held. Generic over an inner type T
and a
Bound
that targets it.
Bounded types can be constructed directly or through the bound
macro:
// Defined directly use tightness::{Bounded, Bound}; #[derive(Debug)] pub struct LetterBound; impl tightness::Bound for LetterBound { type Target = char; fn check(target: &char) -> bool { target.is_alphabetic() } } pub type Letter = tightness::Bounded<char, LetterBound>;
// Defined via macro use tightness::{bound, Bounded}; bound!(pub Letter: char where |l| l.is_alphabetic());
Implementations
impl<T, B: Bound<Target = T>> Bounded<T, B>
[src]
impl<T, B: Bound<Target = T>> Bounded<T, B>
[src]pub fn new(t: T) -> Result<Self, ConstructionError<T>>
[src]
pub fn new(t: T) -> Result<Self, ConstructionError<T>>
[src]Fallible constructor. Will return an error if the argument t
doesn’t fulfill the conditions of the bound.
bound!(Letter: char where |c| c.is_alphabetic()); assert!(Letter::new('a').is_ok()); assert!(matches!(Letter::new('5'), Err(ConstructionError('5'))));
pub fn mutate(&mut self, f: impl FnOnce(&mut T))
[src]
pub fn mutate(&mut self, f: impl FnOnce(&mut T))
[src]Will panic if the conditions of the bound don’t hold after mutation.
bound!(Letter: char where |c| c.is_alphabetic()); let mut letter = Letter::new('a').unwrap(); letter.mutate(|l| *l = 'b'); // Panics: letter.mutate(|l| *l = '5');
pub fn mutate_or(
&mut self,
default: Self,
f: impl FnOnce(&mut T)
) -> Result<(), MutationError<T>>
[src]
pub fn mutate_or(
&mut self,
default: Self,
f: impl FnOnce(&mut T)
) -> Result<(), MutationError<T>>
[src]If the conditions of the bound don’t hold after mutation, will restore to a given value.
bound!(Letter: char where |c| c.is_alphabetic()); let mut letter = Letter::new('a').unwrap(); let mut fallback = Letter::new('b').unwrap(); letter.mutate_or(fallback, |l| *l = '5').unwrap_err(); assert_eq!(*letter, 'b');
pub fn into_mutated(
self,
f: impl FnOnce(&mut T)
) -> Result<Self, MutationError<T>>
[src]
pub fn into_mutated(
self,
f: impl FnOnce(&mut T)
) -> Result<Self, MutationError<T>>
[src]The value is dropped if the conditions of the bound don’t hold after mutation.
bound!(Letter: char where |c| c.is_alphabetic()); let mut letter = Letter::new('a').unwrap(); let letter = letter.into_mutated(|l| *l = 'b').unwrap(); let result = letter.into_mutated(|l| *l = '5'); assert!(matches!(result, Err(MutationError(Some('5')))));
pub fn into_inner(self) -> T
[src]
pub fn into_inner(self) -> T
[src]Retrieve the inner, unprotected value.
impl<T: Clone, B: Bound<Target = T>> Bounded<T, B>
[src]
impl<T: Clone, B: Bound<Target = T>> Bounded<T, B>
[src]pub fn try_mutate(
&mut self,
f: impl FnOnce(&mut T)
) -> Result<(), MutationError<T>>
[src]
pub fn try_mutate(
&mut self,
f: impl FnOnce(&mut T)
) -> Result<(), MutationError<T>>
[src]Preserves invariants after mutation, erroring out if the attempt to mutate was invalid. Requires a copy to ensure the value is recoverable.
Trait Implementations
impl<T: Ord, B: Bound<Target = T>> Ord for Bounded<T, B>
[src]
impl<T: Ord, B: Bound<Target = T>> Ord for Bounded<T, B>
[src]impl<T: PartialOrd, B: Bound<Target = T>> PartialOrd<Bounded<T, B>> for Bounded<T, B>
[src]
impl<T: PartialOrd, B: Bound<Target = T>> PartialOrd<Bounded<T, B>> for Bounded<T, B>
[src]fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]This method returns an ordering between self
and other
values if one exists. Read more
#[must_use]fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]fn lt(&self, other: &Rhs) -> bool
1.0.0[src]This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]fn le(&self, other: &Rhs) -> bool
1.0.0[src]This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl<T: Copy, B: Bound<Target = T>> Copy for Bounded<T, B>
[src]
impl<T: Eq, B: Bound<Target = T>> Eq for Bounded<T, B>
[src]
Auto Trait Implementations
impl<T, B> RefUnwindSafe for Bounded<T, B> where
B: RefUnwindSafe,
T: RefUnwindSafe,
B: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, B> Send for Bounded<T, B> where
B: Send,
T: Send,
B: Send,
T: Send,
impl<T, B> Sync for Bounded<T, B> where
B: Sync,
T: Sync,
B: Sync,
T: Sync,
impl<T, B> Unpin for Bounded<T, B> where
B: Unpin,
T: Unpin,
B: Unpin,
T: Unpin,
impl<T, B> UnwindSafe for Bounded<T, B> where
B: UnwindSafe,
T: UnwindSafe,
B: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn to_owned(&self) -> T
[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)
[src]
pub fn clone_into(&self, target: &mut T)
[src]🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more