Trait arithmetic_typing::visit::VisitMut

source ·
pub trait VisitMut<Prim: PrimitiveType> {
    // Provided methods
    fn visit_type_mut(&mut self, ty: &mut Type<Prim>) { ... }
    fn visit_tuple_mut(&mut self, tuple: &mut Tuple<Prim>) { ... }
    fn visit_object_mut(&mut self, object: &mut Object<Prim>) { ... }
    fn visit_dyn_constraints_mut(
        &mut self,
        constraints: &mut DynConstraints<Prim>,
    ) { ... }
    fn visit_middle_len_mut(&mut self, len: &mut TupleLen) { ... }
    fn visit_function_mut(&mut self, function: &mut Function<Prim>) { ... }
}
Expand description

Recursive traversal across the exclusive reference to a Type.

Inspired by the VisitMut trait from syn.

§Examples

use arithmetic_typing::{ast::TypeAst, arith::Num, Type};
use arithmetic_typing::visit::{self, VisitMut};

/// Replaces all primitive types with `Num`.
struct Replacer;

impl VisitMut<Num> for Replacer {
    fn visit_type_mut(&mut self, ty: &mut Type) {
        match ty {
            Type::Prim(_) => *ty = Type::NUM,
            _ => visit::visit_type_mut(self, ty),
        }
    }
}

let ty = TypeAst::try_from("(Num, Bool, (Num) -> (Bool, Num))")?;
let mut ty = Type::try_from(&ty)?;
Replacer.visit_type_mut(&mut ty);
assert_eq!(ty.to_string(), "(Num, Num, (Num) -> (Num, Num))");

Provided Methods§

source

fn visit_type_mut(&mut self, ty: &mut Type<Prim>)

Visits a generic type.

The default implementation calls one of more specific methods corresponding to the ty variant. For “simple” types (variables, params, primitive types) does nothing.

source

fn visit_tuple_mut(&mut self, tuple: &mut Tuple<Prim>)

Visits a tuple type.

The default implementation calls Self::visit_middle_len_mut() for the middle length if the tuple has a middle. Then, Self::visit_type_mut() is called for each tuple element, including the middle element if any.

source

fn visit_object_mut(&mut self, object: &mut Object<Prim>)

Visits an object type.

source

fn visit_dyn_constraints_mut(&mut self, constraints: &mut DynConstraints<Prim>)

Visits a Type::Dyn variant.

The default implementation visits the object constraint if it is present using Self::visit_object_mut().

source

fn visit_middle_len_mut(&mut self, len: &mut TupleLen)

Visits a middle length of a tuple.

The default implementation does nothing.

source

fn visit_function_mut(&mut self, function: &mut Function<Prim>)

Visits a functional type.

The default implementation calls Self::visit_tuple_mut() on arguments and then Self::visit_type_mut() on the return value.

Implementors§