`Rc` 的层次结构与 Iterator 一起使用的生命周期是多少?
What lifetime to use with Iterator for a hierarchy of `Rc`s?
我有一个代表分层数据结构的 Rust 结构,每个结构都包含一个创建分层结构的 Vec<Rc<Self>>
。我想创建一个迭代器,从给定节点开始,迭代 Rc
s 到层次结构的每个成员。 Node
结构有一个生命周期参数 'a
因为整个层次结构引用了整个层次结构所基于的文本文档中的一些 &'a str
(包含在 NodeDetail<'a>
中)。
pub struct Node<'a> {
pub detail: NodeDetail<'a>,
pub children: Vec<Rc<Self>>,
}
impl<'a> Node<'a> {
// Returns an iterator over `Rc`s to all the children
pub fn iter(&self) -> Box<(dyn Iterator<Item=Rc<Self>> + 'static)> {
let mut ans:Box<(dyn Iterator<Item=Rc<Self>> + 'static)> = Box::new(std::iter::empty());
for c in self.children.iter() {
ans = Box::new(ans.chain(std::iter::once(c.clone())));
ans = Box::new(ans.chain(c.iter()));
}
ans
}
}
通过这次尝试,我收到了这个错误:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> src/model/updm/impl_common.rs:23:40
|
23 | ans = Box::new(ans.chain(c.iter()));
| ^^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the impl at 5:6...
--> src/model/updm/impl_common.rs:5:6
|
5 | impl<'a> super::UpdmCommon<'a> {
| ^^
note: ...so that the types are compatible
--> src/model/updm/impl_common.rs:23:40
|
23 | ans = Box::new(ans.chain(c.iter()));
| ^^^^
= note: expected `&UpdmCommon<'_>`
found `&UpdmCommon<'a>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the expression is assignable
--> src/model/updm/impl_common.rs:23:19
|
23 | ans = Box::new(ans.chain(c.iter()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected `Box<(dyn Iterator<Item = Rc<UpdmCommon<'a>>> + 'static)>`
found `Box<dyn Iterator<Item = Rc<UpdmCommon<'a>>>>`
我认为 Node
结构的通用生命周期与特定于迭代器的生命周期混淆了。我正在克隆 Rc
,因此迭代器的 Item
已拥有并具有 'static
生命周期。我认为迭代器本身也应该被拥有,这将使它成为 'static
。我首先尝试了 Box<dyn Iterator<Item=Rc<Self>>>
,然后添加了 'static
注释来尝试修复我遇到的第一个错误。有人知道如何解决这个问题吗?
我明白了。寻求帮助后,我会立即尽最大努力排除故障。有点像我在按下发送键后如何进行最好的电子邮件校对。
事实证明,我需要 Box<(dyn Iterator<Item=Rc<Self>> + 'a)>
,现在我想到它是有道理的,因为迭代器不能比整个层次结构所依赖的原始 &'a str
寿命更长。
您需要将您从 iter()
返回的值的生命周期绑定到 &self
,并且您需要将盒装迭代器转换为特征对象。
use std::rc::Rc;
pub struct NodeDetail<'a>(&'a str);
pub struct Node<'a> {
pub detail: NodeDetail<'a>,
pub children: Vec<Rc<Self>>,
}
impl<'a> Node<'a> {
// Returns an iterator over `Rc`s to all the children
pub fn iter(&'a self) -> Box<(dyn Iterator<Item=Rc<Self>> + 'a)> {
let mut ans = Box::new(std::iter::empty()) as Box<dyn Iterator<Item=Rc<Self>>>;
for c in self.children.iter() {
ans = Box::new(ans.chain(std::iter::once(c.clone())));
ans = Box::new(ans.chain(c.iter()));
}
ans
}
}
我有一个代表分层数据结构的 Rust 结构,每个结构都包含一个创建分层结构的 Vec<Rc<Self>>
。我想创建一个迭代器,从给定节点开始,迭代 Rc
s 到层次结构的每个成员。 Node
结构有一个生命周期参数 'a
因为整个层次结构引用了整个层次结构所基于的文本文档中的一些 &'a str
(包含在 NodeDetail<'a>
中)。
pub struct Node<'a> {
pub detail: NodeDetail<'a>,
pub children: Vec<Rc<Self>>,
}
impl<'a> Node<'a> {
// Returns an iterator over `Rc`s to all the children
pub fn iter(&self) -> Box<(dyn Iterator<Item=Rc<Self>> + 'static)> {
let mut ans:Box<(dyn Iterator<Item=Rc<Self>> + 'static)> = Box::new(std::iter::empty());
for c in self.children.iter() {
ans = Box::new(ans.chain(std::iter::once(c.clone())));
ans = Box::new(ans.chain(c.iter()));
}
ans
}
}
通过这次尝试,我收到了这个错误:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> src/model/updm/impl_common.rs:23:40
|
23 | ans = Box::new(ans.chain(c.iter()));
| ^^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the impl at 5:6...
--> src/model/updm/impl_common.rs:5:6
|
5 | impl<'a> super::UpdmCommon<'a> {
| ^^
note: ...so that the types are compatible
--> src/model/updm/impl_common.rs:23:40
|
23 | ans = Box::new(ans.chain(c.iter()));
| ^^^^
= note: expected `&UpdmCommon<'_>`
found `&UpdmCommon<'a>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the expression is assignable
--> src/model/updm/impl_common.rs:23:19
|
23 | ans = Box::new(ans.chain(c.iter()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected `Box<(dyn Iterator<Item = Rc<UpdmCommon<'a>>> + 'static)>`
found `Box<dyn Iterator<Item = Rc<UpdmCommon<'a>>>>`
我认为 Node
结构的通用生命周期与特定于迭代器的生命周期混淆了。我正在克隆 Rc
,因此迭代器的 Item
已拥有并具有 'static
生命周期。我认为迭代器本身也应该被拥有,这将使它成为 'static
。我首先尝试了 Box<dyn Iterator<Item=Rc<Self>>>
,然后添加了 'static
注释来尝试修复我遇到的第一个错误。有人知道如何解决这个问题吗?
我明白了。寻求帮助后,我会立即尽最大努力排除故障。有点像我在按下发送键后如何进行最好的电子邮件校对。
事实证明,我需要 Box<(dyn Iterator<Item=Rc<Self>> + 'a)>
,现在我想到它是有道理的,因为迭代器不能比整个层次结构所依赖的原始 &'a str
寿命更长。
您需要将您从 iter()
返回的值的生命周期绑定到 &self
,并且您需要将盒装迭代器转换为特征对象。
use std::rc::Rc;
pub struct NodeDetail<'a>(&'a str);
pub struct Node<'a> {
pub detail: NodeDetail<'a>,
pub children: Vec<Rc<Self>>,
}
impl<'a> Node<'a> {
// Returns an iterator over `Rc`s to all the children
pub fn iter(&'a self) -> Box<(dyn Iterator<Item=Rc<Self>> + 'a)> {
let mut ans = Box::new(std::iter::empty()) as Box<dyn Iterator<Item=Rc<Self>>>;
for c in self.children.iter() {
ans = Box::new(ans.chain(std::iter::once(c.clone())));
ans = Box::new(ans.chain(c.iter()));
}
ans
}
}