【发布时间】:2023-03-06 15:09:01
【问题描述】:
我正在寻找一种创建特征对象集合的方法。 但是我想接受实现给定特征的对象,或者包装和取消引用特征的对象。
trait TheTrait {
// fn foo() -> ();
}
// "Direct" implementation
struct Implements {}
impl TheTrait for Implements {}
// "Proxy" implementation
struct DerefsTo {
implements: Implements,
}
impl std::ops::Deref for DerefsTo {
type Target = dyn TheTrait;
fn deref(&self) -> &Self::Target {
return &self.implements;
}
}
fn main() -> () {
let x1: Box<dyn TheTrait> = Box::new(Implements {}); // This is fine
let x2: Box<dyn TheTrait> = Box::new(DerefsTo {implements: Implements {}}); // Trait TheTrait not implemented
let x3: Box<dyn TheTrait> = Box::new(x1); // Trait TheTrait not implemented
// Put x1, x2, x3 to collection, call foo
}
有没有办法做到这一点,可能不接触Implements 类型?
是否有任何通用方法通过公开实现特征的字段来实现特征,例如“包装器”类型?
【问题讨论】:
标签: rust