"参数类型`C`可能活不够长",当它不需要时

时间:2018-03-15 12:04:48

标签: rust traits lifetime

我在Rust写了非常基本的AI系统。它的主要组成部分是:

  • Action s,可以由库用户实现,以供特定用途,
  • 通用Context,传递给所有操作,只需要在操作执行期间生效,
  • ActionsContainer,"全球"存储所有可能的操作,
  • System,选择正确的操作并运行它。有许多系统,每个代理一个。但是,它们共享相同的行为集,因此它们都引用了共同的ActionsContainer

这是一个说明我问题的最小例子。

// Generic system

trait Context {}

trait Action<C: Context> {
    fn run(&self, context: &mut C);
}

struct ActionsContainer<C: Context> {
    actions: Vec<Box<Action<C>>>,
}

struct System<'a, C: Context> {
    actions: &'a ActionsContainer<C>,
}

impl<'a, C: Context> System<'a, C> {
    fn run(&self, c: &mut 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(&context);

            assert_eq!(context.y, context.x)
        }
    }
}

playground

编译器抱怨:

error[E0309]: the parameter type `C` may not live long enough
  --> src/main.rs:14:5
   |
13 | struct System<'a, C: Context> {
   |                   -- help: consider adding an explicit lifetime bound `C: 'a`...
14 |     actions: &'a ActionsContainer<C>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: ...so that the reference type `&'a ActionsContainer<C>` does not outlive the data it points at
  --> src/main.rs:14:5
   |
14 |     actions: &'a ActionsContainer<C>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

但是,C未存储在Action中。它只需要在run执行时生存。另一方面,Action确实需要与整个System一样长寿。有没有办法注释这个?

我怀疑,它与高等级特质界有关,但我不知道如何在这里使用它们。

我还试图摆脱Action作为特征对象,只使用普通函数引用:

type Action<C> = fn(&mut C);

struct ActionsContainer<C: Context> {
    actions: Vec<&'static Action<C>>,
}

但编译器错误几乎相同。

1 个答案:

答案 0 :(得分:1)

我找到了解决方案:

// 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总是假设泛型结构中提到的特征将存储在该结构中(因此我的生命周期问题)。如果您不打算存储特征,请不要在结构定义中提及它。相反,使用更一般的边界,并在方法上阐明它们,这定义了适当的生命周期。