- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个结构 A
具有盒装特征( Foo
),另一个结构 BoxedA
它有一个 Rc<RefCell<A>>
在里面。我正在尝试在 BoxedA
上创建一个方法返回对盒装特征的引用,但在映射 Ref<A>
时一直遇到生命周期问题至 Ref<Foo>
.
这是我的代码:
use std::rc::Rc;
use std::cell::{RefCell, Ref};
trait Foo {
}
struct A {
a: Box<Foo>
}
impl A {
fn new(a: Box<Foo>) -> A {
A { a: a }
}
fn a(&self) -> &Foo {
&*self.a
}
}
struct BoxedA {
a: Rc<RefCell<A>>
}
impl BoxedA {
fn new(a: Box<Foo>) -> BoxedA {
BoxedA {
a: Rc::new(RefCell::new(A::new(a)))
}
}
fn a(&self) -> Ref<Foo> {
Ref::map(self.a.borrow(), |a| a.a())
}
}
impl Foo for i32 {
}
fn main() {
let a = BoxedA::new(Box::new(3));
let a_ref = a.a();
}
Rust Playground 链接:https://play.rust-lang.org/?gist=d0348ad9b06a152770f3877864b01531&version=stable&backtrace=0
我得到以下编译错误:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> <anon>:34:41
|
34 | Ref::map(self.a.borrow(), |a| a.a())
| ^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 34:38...
--> <anon>:34:39
|
34 | Ref::map(self.a.borrow(), |a| a.a())
| ^^^^^
note: ...so that reference does not outlive borrowed content
--> <anon>:34:39
|
34 | Ref::map(self.a.borrow(), |a| a.a())
| ^
note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the body at 33:28...
--> <anon>:33:29
|
33 | fn a(&self) -> Ref<Foo> {
| _____________________________^ starting here...
34 | | Ref::map(self.a.borrow(), |a| a.a())
35 | | }
| |_____^ ...ending here
note: ...so that the declared lifetime parameter bounds are satisfied
--> <anon>:34:9
|
34 | Ref::map(self.a.borrow(), |a| a.a())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
奇怪的是,如果我替换所有 Foo
代码编译与i32
.
最佳答案
问题出在这个签名上:
fn a(&self) -> &Foo { ... }
根据生命周期省略规则,这扩展为:
fn a<'b>(&'b self) -> &'b (Foo + 'b) { ... }
哇,那是什么+ 'b
位?
Trait 对象有一个生命周期限制,指定对象中包含的引用的最短生命周期。如果该类型不包含任何引用,则该生命周期绑定(bind)将为 'static
.
如果您在包含引用的类型上实现特征,例如一对引用:
impl<'a, 'b> Foo for (&'a u32, &'b u32) {}
那么你有一个 &Foo
类型的变量这恰好是对 (&'a u32, &'b u32)
的引用(要清楚,这是对一对引用的引用),关于这两个 &u32
的生命周期信息在哪里?引用去?这就是 trait 对象上生命周期的用武之地。 &Foo
的完整扩展类型看起来像&'c (Foo + 'd)
, 其中'd
是'a
中最短的一个和 'b
(或者可能更短,多亏了协方差)。
还有许多其他地方您没有明确指定生命周期限制。所有期望函数返回类型的地方都默认为'static
终生束缚。例如,Box<Foo>
就是这样。在struct A
: 它实际上被解释为 Box<Foo + 'static>
.
您的问题的简单解决方案是指定 A::a
返回带有 'static
的特征对象生命周期限制:
fn a(&self) -> &(Foo + 'static) { ... }
这很有意义,因为我们返回一个指向 Box<Foo + 'static>
内部的指针!
BoxedA::a
最终可能会给您带来类似的问题,因此您可能也想在解决这个问题的同时解决这个问题:
fn a(&self) -> Ref<Foo + 'static> { ... }
既然您已经了解了这些生命周期边界,您可能想考虑将 A
设为是否有意义和 BoxedA
在该生命周期范围内通用,而不是强制执行 'static
.如果您想最大化通用性,您的代码将如下所示:
struct A<'a> {
a: Box<Foo + 'a>
}
impl<'a> A<'a> {
fn new(a: Box<Foo + 'a>) -> A<'a> {
A { a: a }
}
fn a(&self) -> &(Foo + 'a) {
&*self.a
}
}
struct BoxedA<'a> {
a: Rc<RefCell<A<'a>>>
}
impl<'a> BoxedA<'a> {
fn new(a: Box<Foo + 'a>) -> BoxedA<'a> {
BoxedA {
a: Rc::new(RefCell::new(A::new(a)))
}
}
fn a(&self) -> Ref<Foo + 'a> {
Ref::map(self.a.borrow(), |a| a.a())
}
}
是否需要这种级别的通用性由您决定。
关于rust - 将 Ref::map 与特征对象一起使用时无法推断生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42541298/
设置 我希望能够定义一个特征,使得任何实现该特征的结构不仅必须实现函数,而且还必须为某些常量指定值。所以也许是这样的: trait MyTrait { const MY_CONST: u8;
在我的 Web 应用程序中,授权用户至少有 4 个“方面”:http session 相关数据、持久数据、facebook 数据、运行时业务数据。 我决定使用案例类组合而不是特征至少有两个原因: 性状
我正在尝试使用以下代码从类中获取完整数据成员的列表: import std.stdio; import std.traits; class D { static string[] integr
我正在尝试实现 From对于我的一种类型。它应该消耗任意长度的行(仅在运行时已知)并从行中获取数据。编译器提示 &[&str; 2]不是 &[&str] ,即它不能将固定大小的切片转换为任意长度的切片
有人可以请你这么好心,并指出一种提取拟合树中使用的列/特征的方法,使用如下代码: library(dplyr) library(caret) library(rpart) df % dplyr
假设我定义了一个 Group所有组操作的特征。是否可以创建一个包装器AGroup超过 Group无需手动派生所有操作? 基本上,我想要这个: #[derive (Copy, Debug, Clone,
最近浏览了Markus Stocker的博客他很好地解释了如何在使用 observation 时表示传感器观察结果。 SSN 的模块本体论。我完全理解他的解释,但我发现有一件事多余地代表了一个的两个特
我有以下情况/代码; trait Model { def myField: String } case class MyModel(myField: String) extends Model
我想让一个案例类扩展一个特征 以下是我的要求: 我需要为 child 使用案例类。这是一个硬性要求,因为 scopt ( https://github.com/scopt/scopt ) parent
最近浏览了Markus Stocker的博客他很好地解释了如何在使用 observation 时表示传感器观察结果。 SSN 的模块本体论。我完全理解他的解释,但我发现有一件事多余地代表了一个的两个特
我有以下情况/代码; trait Model { def myField: String } case class MyModel(myField: String) extends Model
不确定标题是否完全有意义,对此感到抱歉。我是机器学习新手,正在使用 Scikit 和决策树。 这就是我想做的;我想获取所有输入并包含一个独特的功能,即客户端 ID。现在,客户端 ID 是唯一的,无法以
我想读取具有 Eigen 的 MNIST 数据集,每个文件都由一个矩阵表示。我希望在运行时确定矩阵大小,因为训练集和测试集的大小不同。 Map> MNIST_dataset((uchar*)*_dat
在 MATLAB 中,我可以选择一个分散的子矩阵,例如: A = [1 ,2 ,3;4,5,6;7,8,9] A([1,3],[1,3]) = [1,3;7,9] 有没有用 Eigen 做到这一点的聪
我在执行 Into 时遇到问题Rust 中通用结构的特征。下面是我正在尝试做的简化版本: struct Wrapper { value: T } impl Into for Wrapper {
我有这段 matlab 代码,我想用 Eigen 编写: [V_K,D_K] = eig(K); d_k = diag(D_K); ind_k = find(d_k > 1e-8); d_k(ind_
我正在使用 Eigen C++ 矩阵库,我想获取对矩阵列的引用。文档说要使用 matrix_object.col(index),但这似乎返回了一个表示列的对象,而不是简单地引用原始矩阵对象中的列。我担
在乘以很多旋转矩阵之后,由于舍入问题(去正交化),最终结果可能不再是有效的旋转矩阵 重新正交化的一种方法是遵循以下步骤: 将旋转矩阵转换为轴角表示法 ( link ) 将轴角转换回旋转矩阵 ( lin
定义可由命名空间中的多个类使用的常量的最佳方法是什么?我试图避免太多的继承,所以扩展基类不是一个理想的解决方案,我正在努力寻找一个使用特征的好的解决方案。这在 PHP 5.4 中是可行的还是应该采用不
定义可由命名空间中的多个类使用的常量的最佳方法是什么?我试图避免太多的继承,所以扩展基类不是一个理想的解决方案,我正在努力寻找一个使用特征的好的解决方案。这在 PHP 5.4 中是可行的还是应该采用不
我是一名优秀的程序员,十分优秀!