Struct arithmetic_typing::arith::StructConstraint
source · pub struct StructConstraint<Prim, C, F> { /* private fields */ }
Expand description
Helper to define structural Constraint
s, 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()
returnstrue
. Type::Any
always satisfies the constraint.Type::Dyn
types satisfy the constraint iff theConstraint
wrapped by this helper is present amongDynConstraints
. Thus, if the wrapped constraint is not object-safe, it will not be satisfied by anyDyn
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>
impl<Prim, C, F> StructConstraint<Prim, C, F>
sourcepub fn new(constraint: C, predicate: F) -> Self
pub fn new(constraint: C, predicate: F) -> Self
Creates a new helper. predicate
determines whether a particular primitive type
should satisfy the constraint
.
sourcepub fn deny_dyn_slices(self) -> Self
pub fn deny_dyn_slices(self) -> Self
Marks that dynamically sized slices should fail the constraint check.
sourcepub fn visitor<'r>(
self,
substitutions: &'r mut Substitutions<Prim>,
errors: OpErrors<'r, Prim>,
) -> Box<dyn Visit<Prim> + 'r>
pub fn visitor<'r>( self, substitutions: &'r mut Substitutions<Prim>, errors: OpErrors<'r, Prim>, ) -> Box<dyn Visit<Prim> + 'r>
Returns a Visit
or that can be used for Constraint::visitor()
implementations.
Trait Implementations§
Auto Trait Implementations§
impl<Prim, C, F> Freeze for StructConstraint<Prim, C, F>
impl<Prim, C, F> RefUnwindSafe for StructConstraint<Prim, C, F>
impl<Prim, C, F> Send for StructConstraint<Prim, C, F>
impl<Prim, C, F> Sync for StructConstraint<Prim, C, F>
impl<Prim, C, F> Unpin for StructConstraint<Prim, C, F>
impl<Prim, C, F> UnwindSafe for StructConstraint<Prim, C, F>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more