我发现了an interesting implementation of the visitor pattern in Rust:
pub trait Visitor<T> {
fn visit(&mut self, t: &T);
}
pub trait Visitable: Sized {
fn accept<T>(&self, t: &mut T)
where
T: Visitor<Self>,
{
t.visit(self);
}
}
由于Visitable
不是对象安全的,因此我无法存储Vec
个对象中的Visitable
个。我发现了a solution to a similar problem,是否有可能为Visitable
做类似的解决方案?
答案 0 :(得分:2)
Visitable
是一个特征,特征没有大小(不保证实现子具有相同的大小)。您需要存储一定大小的东西。将值包装在Box
中,然后按以下方式使用它:
trait VisitorTrait {
fn visit(&self, visitable: &Box<VisitableTrait>);
}
struct Visitor1 {}
struct Visitor2 {}
impl VisitorTrait for Visitor1 {
fn visit(&self, visitable: &Box<VisitableTrait>) {
println!("visited via Visitor1");
visitable.accept()
}
}
impl VisitorTrait for Visitor2 {
fn visit(&self, visitable: &Box<VisitableTrait>) {
println!("visited via Visitor2");
visitable.accept()
}
}
trait VisitableTrait {
fn accept(&self);
}
#[derive(Clone)]
struct Visitable1 {}
#[derive(Clone)]
struct Visitable2 {}
impl VisitableTrait for Visitable1 {
fn accept(&self) {
println!("accepted1.");
}
}
impl VisitableTrait for Visitable2 {
fn accept(&self) {
println!("accepted2.");
}
}
fn main() {
let visitor1 = Visitor1 {};
let visitor2 = Visitor2 {};
let visitable1 = Visitable1 {};
let visitable2 = Visitable2 {};
let mut visitors: Vec<Box<VisitorTrait>> = Vec::new();
let mut visitables: Vec<Box<VisitableTrait>> = Vec::new();
visitors.push(Box::new(visitor1));
visitors.push(Box::new(visitor2));
visitables.push(Box::new(visitable1));
visitables.push(Box::new(visitable2));
for visitable in visitables.iter() {
for visitor in visitors.iter() {
visitor.visit(visitable.clone());
}
}
}
我们为访问者和可访问者声明了两个不同的特征,并在具有两个for循环的访问者的帮助下遍历了可访问者。