Working on an evaluation tree structure, I have a trait that I'm using to let the tree struct use dyn trait bounds on a vec of branch objects:
trait Eval { fn eval(&self, vl: &VarList) -> bool; } Essentially a trait declaring that the struct can evaluate to a boolean given a VarList, another structure with a get(key)->value method. The branching is done with a structure that holds a vec of structs that implement Eval:
enum Operator { And, Or, } struct BBranch { operator: Operator, branches: Vec<Box<dyn Eval>>, } The setup works when I explicitly declare the &VarList type in the function signature for eval(). What I thought I would do as a test is try to make Eval generic, something like:
trait Eval<T> { fn eval(&self, vl: T) -> bool; } Aiming for "This type evaluates to a boolean against some type T", with each struct implementing it for one or multiple T's. My issue is chasing my tail on trying to implement a concrete type for that generic. Where I landed last on trying to implement Eval<&VarList> for BBranch looks like this:
struct BBranch<T> { operator: Operator, branches: Vec<Box<dyn Eval<T>>>, } impl<T> Eval<&VarList> for BBranch<T> { fn eval(&self, vl: &VarList) -> bool { let check = match self.operator { Operator::And => false, Operator::Or => true, }; for item in &self.branches { if item.eval(vl) == check { return check; } } return !check; } } With the current compiler error that for if item.eval(vl) == check { it expects type T and found type &VarList. The current state is the result of chasing compiler suggestion on adding the generic declarations such as the <T> in struct BBranch<T> {.
Part of it is probably a lack of understanding of the role of each of the type declarations in impl<T> Eval<&VarList> for BBranch<T>.
3 posts - 3 participants



