我从 Rust 开始,我已经面临数据所有权的问题。
我想实现一个名为的通用结构Port<T>
有一个值向量Vec<T>
。此外,该结构有一个指向相同类型的其他结构的引用计数指针向量,Vec<Rc<Port<T>>>
:
use std::slice::Iter;
use std::rc::Rc;
pub struct Port<T> {
values: Vec<T>,
ports: Vec<Rc<Port<T>>>,
}
这个想法如下:有多种类型结构Port<T>
。您可以添加类型的值T
到给定的端口。每个端口将这些值存储在其values
属性。然而,可以使用引用计数指针将一个端口“链接”到其他端口:
impl <T> Port<T> {
pub fn new() -> Self {
Self { values: vec![], ports: vec![] }
}
pub fn add_value(&mut self, value: T) {
self.values.push(value);
}
pub fn chain_port(&mut self, port: Rc<Port<T>>) {
if !port.is_empty() {
self.ports.push(port)
}
}
pub fn is_empty(&self) -> bool {
self.values.is_empty() || self.ports.is_empty()
}
pub fn clear(&mut self) {
self.values.clear();
self.ports.clear();
}
}
到目前为止,代码已编译通过。现在,我想为端口实现一个迭代器,它返回对此端口拥有的值的引用,同时也返回对每个链接端口的迭代器生成的值的引用:
pub struct PortIterator<'a, T> {
values: Iter<'a, T>, // Iterates over values owned by Port<T>
port: Option<Box<PortIterator<'a, T>>>, // Pointer to current port iterator
ports: Vec<Rc<Port<T>>>, // Pointers to remaining chained ports
}
// Note that the iterator is created from an immutable reference to Port<T>
impl<'a, T: 'a> IntoIterator for &'a Port<T> {
type Item = &'a T; // the iterator returns references to values
type IntoIter = PortIterator<'a, T>;
fn into_iter(self) -> Self::IntoIter {
// We clone all the reference-counting pointers so the iterator owns them
let mut ports = vec![];
for port in self.ports.iter() {
ports.push(port.clone())
}
PortIterator {values: self.values.iter(), port: None, ports}
}
}
现在,让我们定义Iterator
特质为PortIterator
:
impl <'a, T: 'a> Iterator for PortIterator<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
// We first iterate over values of the original port
if let Some(v) = self.values.next() {
return Some(v)
}
// If the first iterable is done, we try to iterate over a chained port
if let Some(port) = &mut self.port {
if let Some(v) = port.next() {
return Some(v)
}
}
// if the chained port is over, we try to consume the next chained port
if let Some(port) = self.ports.get(self.next_port) {
self.next_port += 1;
self.port = Some(Box::new(port.as_ref().into_iter()));
return self.next()
}
None
}
}
现在,该程序无法编译。问题好像出在第三个if let
块,并且它与生命周期有关。编译器是这样说的:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
--> src/modeling/port.rs:69:40
|
69 | if let Some(port) = self.ports.get(self.next_port) {
| ^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 57:5...
--> src/modeling/port.rs:57:5
|
57 | fn next(&mut self) -> Option<Self::Item> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that reference does not outlive borrowed content
--> src/modeling/port.rs:69:29
|
69 | if let Some(port) = self.ports.get(self.next_port) {
| ^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 54:7...
--> src/modeling/port.rs:54:7
|
54 | impl <'a, T: 'a> Iterator for PortIterator<'a, T> {
| ^^
note: ...so that the expression is assignable
--> src/modeling/port.rs:71:25
|
71 | self.port = Some(Box::new(port.as_ref().into_iter()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected `Option<Box<PortIterator<'a, _>>>`
found `Option<Box<PortIterator<'_, _>>>`
我不知道如何处理这个问题。我一直在尝试其他选项和实现,但我一直在原地踏步。