as_mut().unwrap(): Cannot infer lifetime due to conflicting requirements
Rust的一生再次使我感到困惑。 我试图返回对我拥有的装箱对象的可变引用。 这是我简化的问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | pub trait Foo { fn foo(&self); } pub struct Bar { foo: Option<Box<Foo>>, } impl Bar { pub fn foo(&mut self) -> &mut Box<Foo> { let foo_ref = self.foo.as_mut(); foo_ref.unwrap() } pub fn set_foo(&mut self, value: Box<Foo>) { self.foo = Some(value); } } |
我收到这些错误,但我不太了解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Compiling testproject v0.0.1 (file:///home/virtlink/projects/orion/testproject) src/lib.rs:15:17: 15:25 error: cannot infer an appropriate lifetime due to conflicting requirements src/lib.rs:15 foo_ref.unwrap() ^~~~~~~~ src/lib.rs:15:9: 15:25 note: first, the lifetime cannot outlive the method call at 15:8... src/lib.rs:15 foo_ref.unwrap() ^~~~~~~~~~~~~~~~ src/lib.rs:15:9: 15:16 note: ...so that method receiver is valid for the method call src/lib.rs:15 foo_ref.unwrap() ^~~~~~~ src/lib.rs:13:44: 16:6 note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the block at 13:43... src/lib.rs:13 pub fn foo(&mut self) -> &mut Box<Foo> { src/lib.rs:14 let foo_ref = self.foo.as_mut(); src/lib.rs:15 foo_ref.unwrap() src/lib.rs:16 } src/lib.rs:15:9: 15:25 note: ...so that expression is assignable (expected `&mut Box<Foo>`, found `&mut Box<Foo>`) src/lib.rs:15 foo_ref.unwrap() ^~~~~~~~~~~~~~~~ error: aborting due to previous error Could not compile `testproject`. |
我不确定该如何解决。
这是一辈子淘汰的情况,默认对象以痛苦的看不见的方式限制了回火。
错误是完全不透明的,不好。如果用类似的
1 2 3 4 | match self.foo { Some(ref mut foo) => foo, None => panic!(), } |
事情变得更加清晰:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | a.rs:13:34: 13:37 error: mismatched types: expected `&mut Box<Foo>`, found `&mut Box<Foo>` (lifetime mismatch) [E0308] a.rs:13 Some(ref mut foo) => foo, ^~~ a.rs:11:44: 16:6 note: the anonymous lifetime #1 defined on the block at 11:43... a.rs:11 pub fn foo(&mut self) -> &mut Box<Foo> { a.rs:12 match self.foo { a.rs:13 Some(ref mut foo) => foo, a.rs:14 None => panic!(), a.rs:15 } a.rs:16 } note: ...does not necessarily outlive the static lifetime error: aborting due to previous error |
现在我们知道发生的是类型为
但是,
因此,您的方法的完全无电子签名实际上是这样的:
1 | pub fn foo<'a>(&'a mut self) -> &'a mut Box<Foo + 'a>; |
现在,为什么不起作用,我不清楚。我本以为
无论如何,您真正想要的是返回
1 2 3 | pub fn foo(&mut self) -> &mut Box<Foo + 'static> { self.foo.as_mut().unwrap() } |
另一个解决方案是在定义中要求实现
您可以阅读有关RFC 599中默认对象范围的更多信息。