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]

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]

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]

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]

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 get(&self) -> &T[src]

Access the inner value through an immutable reference.

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

Retrieve the inner, unprotected value.

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]

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, B: Bound<Target = T>> AsRef<T> for Bounded<T, B>[src]

fn as_ref(&self) -> &T[src]

Performs the conversion.

impl<T, B: Bound<Target = T>> Borrow<T> for Bounded<T, B>[src]

fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T: Clone, B: Bound<Target = T>> Clone for Bounded<T, B>[src]

fn clone(&self) -> Self[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: Debug, B: Debug + Bound<Target = T>> Debug for Bounded<T, B>[src]

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

Formats the value using the given formatter. Read more

impl<T, B: Bound<Target = T>> Deref for Bounded<T, B>[src]

type Target = T

The resulting type after dereferencing.

fn deref(&self) -> &Self::Target[src]

Dereferences the value.

impl<T: Hash, B: Bound<Target = T>> Hash for Bounded<T, B>[src]

fn hash<H: Hasher>(&self, state: &mut H)[src]

Feeds this value into the given Hasher. Read more

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

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

impl<T: Index<U>, U, B: Bound<Target = T>> Index<U> for Bounded<T, B>[src]

type Output = T::Output

The returned type after indexing.

fn index(&self, index: U) -> &Self::Output[src]

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

impl<T: Ord, B: Bound<Target = T>> Ord for Bounded<T, B>[src]

fn cmp(&self, other: &Self) -> Ordering[src]

This method returns an Ordering between self and other. Read more

#[must_use]
fn max(self, other: Self) -> Self
1.21.0[src]

Compares and returns the maximum of two values. Read more

#[must_use]
fn min(self, other: Self) -> Self
1.21.0[src]

Compares and returns the minimum of two values. Read more

#[must_use]
fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]

Restrict a value to a certain interval. Read more

impl<T: PartialEq, B: Bound<Target = T>> PartialEq<Bounded<T, B>> for Bounded<T, B>[src]

fn eq(&self, other: &Self) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

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]

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]

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]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater 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

impl<T, B> Send for Bounded<T, B> where
    B: Send,
    T: Send

impl<T, B> Sync for Bounded<T, B> where
    B: Sync,
    T: Sync

impl<T, B> Unpin for Bounded<T, B> where
    B: Unpin,
    T: Unpin

impl<T, B> UnwindSafe for Bounded<T, B> where
    B: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

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

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

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

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

Immutably borrows from an owned value. Read more

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

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

Mutably borrows from an owned value. Read more

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

pub fn from(t: T) -> T[src]

Performs the conversion.

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

pub fn into(self) -> U[src]

Performs the conversion.

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

type Owned = T

The resulting type after obtaining ownership.

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]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

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.

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

Performs the conversion.

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.

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

Performs the conversion.