返回 Box<Future> 操作时的生命周期编译器错误

Lifetime compiler error on operation returning Box<Future>

我正在尝试使用 futurestokio 板条箱进行异步操作,没有遇到任何问题。现在我正在实现一个异步加载数据然后执行一些转换的存储,但是我的界面似乎有一些我无法理解的生命周期问题。

这是显示相同症状的简化测试代码;真正的函数体执行比总是返回错误更合理的动作:

extern crate futures; // v0.1 (old)

use futures::prelude::*;
use futures::future;

pub enum MyError {
    SomeError,
}

pub trait KeyValueStore {
    type ValueType;

    fn load(&self, key: String) -> Box<Future<Item = Vec<u8>, Error = MyError>>;
    fn deserialize(&self, serialized_obj: Vec<u8>) -> Result<Self::ValueType, MyError>;

    fn resolve(&self, key: String) -> Box<Future<Item = Self::ValueType, Error = MyError>>;
}

pub struct Storage<Obj> {
    _unused: std::marker::PhantomData<Obj>,
}

impl<Obj: 'static> KeyValueStore for Storage<Obj> {
    type ValueType = Obj;

    fn deserialize(&self, serialized_obj: Vec<u8>) -> Result<Self::ValueType, MyError> {
        Err(MyError::SomeError)
    }

    fn load(&self, key: String) -> Box<Future<Item = Vec<u8>, Error = MyError>> {
        Box::new(future::err(MyError::SomeError))
    }

    fn resolve(&self, key: String) -> Box<Future<Item = Self::ValueType, Error = MyError>> {
        let result = self.load(key).and_then(|bytes| self.deserialize(bytes));
        Box::new(result)
    }
}

编译器拒绝此示例代码并存在以下生命周期问题:

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/main.rs:36:9
   |
36 |         Box::new(result)
   |         ^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 34:5...
  --> src/main.rs:34:5
   |
34 | /     fn resolve(&self, key: String) -> Box<Future<Item = Self::ValueType, Error = MyError>> {
35 | |         let result = self.load(key).and_then(|bytes| self.deserialize(bytes));
36 | |         Box::new(result)
37 | |     }
   | |_____^
note: ...so that the type `futures::AndThen<std::boxed::Box<futures::Future<Error=MyError, Item=std::vec::Vec<u8>>>, std::result::Result<Obj, MyError>, [closure@src/main.rs:35:46: 35:77 self:&&Storage<Obj>]>` will meet its required lifetime bounds
  --> src/main.rs:36:9
   |
36 |         Box::new(result)
   |         ^^^^^^^^^^^^^^^^
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that expression is assignable (expected std::boxed::Box<futures::Future<Error=MyError, Item=Obj> + 'static>, found std::boxed::Box<futures::Future<Error=MyError, Item=Obj>>)
  --> src/main.rs:36:9
   |
36 |         Box::new(result)
   |         ^^^^^^^^^^^^^^^^

我以前用hypertokio-postgres写的代码看似逻辑相同,但从未产生过这样的错误。如果没有参考资料,我什至看不出生命周期哪里会出错。我的直觉说它与通用 Obj 参数有某种关系,静态生命周期限制感觉不对。

什么情况下会导致编译错误?

self.deserialize中的self需要&self参数到resolve的生命周期;结果不能装箱为 Box<Future<..>>,装箱后的 Future.

需要 'static 生命周期

您可以使用 Box<Future<..> + 'a> 覆盖盒装未来的生命周期要求(其中 'aself 参数的生命周期;您需要更改 resolve特征和实现中的签名);但是您对结果无能为力,因为通常的基于 Future 的事件循环将需要 'static 的生命周期,因为它们必须 Future 必须 运行。

相反,您可以通过将 deserialize 设为 "static method" 来解决此问题,即删除 &self 参数并通过 Self::deserialize 调用它。

将特征用作类型时的默认生命周期要求(在本例中为 'static)已在参考文献 Trait objects 中记录。