我找到了解决方案:
// Generic system
trait Context {}
trait Action<C: Context> {
fn run(&self, context: &mut C);
}
struct ActionsContainer<A> {
actions: Vec<Box<A>>,
}
struct System<'a, A: 'a> {
actions: &'a ActionsContainer<A>,
}
impl<'a, A> System<'a, A> {
fn run<C>(&self, c: &mut C)
where
C: Context,
A: Action<C>,
{
self.actions.actions[0].run(c);
}
}
// Implementation
struct ContextImpl<'a> {
x: &'a i32,
y: i32,
}
impl<'a> Context for ContextImpl<'a> {}
struct ActionImpl {}
impl<'a> Action<ContextImpl<'a>> for ActionImpl {
fn run(&self, c: &mut ContextImpl) {
println!("Action!");
c.y = *c.x;
}
}
// usage
fn main() {
let container = ActionsContainer {
actions: vec![Box::new(ActionImpl {})],
};
{
let system = System {
actions: &container,
};
{
let x = 8;
let mut context = ContextImpl { x: &x, y: 0 };
system.run(&mut context);
assert_eq!(context.y, *context.x)
}
}
}
Playground
Rust总是假设泛型结构中提到的特性将存储在该结构中(因此我的终身问题)。如果您不打算存储特征,请不要在结构定义中提及它。相反,使用更一般的边界,并在定义适当生存期的方法上对其进行澄清。