pub struct StructConstraint<Prim, C, F> { /* private fields */ }
Expand description

Helper to define structural Constraints, i.e., constraints recursively checking the provided type.

The following logic is used to check whether a type satisfies the constraint:

  • Primitive types satisfy the constraint iff the predicate provided in Self::new() returns true.
  • Type::Any always satisfies the constraint.
  • Type::Dyn types satisfy the constraint iff the Constraint wrapped by this helper is present among DynConstraints. Thus, if the wrapped constraint is not object-safe, it will not be satisfied by any Dyn type.
  • Functional types never satisfy the constraint.
  • A compound type (i.e., a tuple) satisfies the constraint iff all its items satisfy the constraint.
  • If Self::deny_dyn_slices() is set, tuple types need to have static length.

Examples

Defining a constraint type using StructConstraint:

/// Constraint for hashable types.
#[derive(Clone, Copy)]
struct Hashed;

impl fmt::Display for Hashed {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("Hash")
    }
}

impl<Prim: PrimitiveType> Constraint<Prim> for Hashed {
    fn visitor<'r>(
        &self,
        substitutions: &'r mut Substitutions<Prim>,
        errors: OpErrors<'r, Prim>,
    ) -> Box<dyn Visit<Prim> + 'r> {
        // We can hash everything except for functions (and thus,
        // types containing functions).
        StructConstraint::new(*self, |_| true)
            .visitor(substitutions, errors)
    }

    fn clone_boxed(&self) -> Box<dyn Constraint<Prim>> {
        Box::new(*self)
    }
}

Implementations§

source§

impl<Prim, C, F> StructConstraint<Prim, C, F>where Prim: PrimitiveType, C: Constraint<Prim> + Clone, F: Fn(&Prim) -> bool + 'static,

source

pub fn new(constraint: C, predicate: F) -> Self

Creates a new helper. predicate determines whether a particular primitive type should satisfy the constraint.

source

pub fn deny_dyn_slices(self) -> Self

Marks that dynamically sized slices should fail the constraint check.

source

pub fn visitor<'r>( self, substitutions: &'r mut Substitutions<Prim>, errors: OpErrors<'r, Prim> ) -> Box<dyn Visit<Prim> + 'r>

Returns a Visitor that can be used for Constraint::visitor() implementations.

Trait Implementations§

source§

impl<Prim: Debug, C: Debug, F: Debug> Debug for StructConstraint<Prim, C, F>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Prim, C, F> RefUnwindSafe for StructConstraint<Prim, C, F>where C: RefUnwindSafe, F: RefUnwindSafe, Prim: RefUnwindSafe,

§

impl<Prim, C, F> Send for StructConstraint<Prim, C, F>where C: Send, F: Send, Prim: Send,

§

impl<Prim, C, F> Sync for StructConstraint<Prim, C, F>where C: Sync, F: Sync, Prim: Sync,

§

impl<Prim, C, F> Unpin for StructConstraint<Prim, C, F>where C: Unpin, F: Unpin, Prim: Unpin,

§

impl<Prim, C, F> UnwindSafe for StructConstraint<Prim, C, F>where C: UnwindSafe, F: UnwindSafe, Prim: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.