我有一个“a”型,以及定义可以对a做的行为的各种特征。
struct A {
pub foo: String;
... etc
}
trait BigChange {
fn big_change(a: A) -> A
}
trait SmallChange {
fn small_change(a: A) -> A
}
struct AConcreteBigImplementation;
impl BigChange for AConcreateBigImplementation {
fn big_change ...
}
struct AnotherConcreteBigImplementation;
impl BigChange for AnotherConcreateBigImplementation {
fn big_change ...
}
struct AConcreteSmallImplementation;
impl SmallChange for AConcreteSmallImplementatio {
fn small_change ...
}
我试图创建一个可以引用任何行为的枚举,例如。
// Doesn't work
enum ChangeA {
BigChangeOperation(BigChange),
SmallChangeOperation(SmallChange)
}
这样我就可以轻松地传递特定的操作,并测试它们是BigChange还是SmallChanges,例如:
let oldA = A::new( ... );
match some_change {
BigChange(b) => let newA = b(oldA)
SmallChange(s) => let newA = s(oldA)
}
我有点理解,也许rust想在编译时知道一个特定的函数,所以Boxing的值可能会有所帮助,但它没有
// Better?
enum ChangeA {
BigChangeOperation(Box<BigChange>),
SmallChangeOperation(Box<SmallChange>)
}
是否可以为这样的枚举值指定一个trait,或者我缺少枚举值中传递的类型、trait、具体值之间的区别?
TIA