【问题标题】:"The parameter type `C` may not live long enough", when it doesn't need to“参数类型 `C` 可能寿命不够长”,当它不需要时
【发布时间】:2018-03-15 12:04:48
【问题描述】:

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

  • Actions,图书馆用户可以自己实现,具体使用,
  • 泛型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 一样长。有什么方法可以注释吗?

我怀疑,它与 Higher-Rank Trait Bounds 有关,但我不知道如何在这里使用它们。

我也尝试摆脱 Action 作为 trait 对象,只使用普通函数引用:

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

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

但编译器错误几乎相同。

【问题讨论】:

  • 你为什么把类型参数放在 trait 上,而不是放在方法上?
  • 如果我把它放在方法上,我将无法使用Action作为特征对象。
  • 部分答案是traits are invariant 超过了他们的论点。
  • @trentcl 我已经阅读了关于方差的文本,现在对它的理解非常模糊。但是,我仍然不知道如何将这个概念应用于我的问题。有什么帮助吗?

标签: rust traits lifetime


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 2015-06-26
    • 2017-03-09
    • 1970-01-01
    相关资源
    最近更新 更多