利用Rust编写跳表时,出现了一些问题。我想要设计一个迭代器,用来访问RecCell
包裹的数据类型,但是其生命周期该如何标注😥?下面是部分关键代码。
struct SkipNode<K, V> {
entry: Option<Entry<K, V>>,
next_by_height: SkipTrack<K, V>,
}
pub struct Iter<'a, K: Ord, V>
{
ptr: Option<Rc<RefCell<SkipNode<K, V>>>>,
_marker: marker::PhantomData<&'a K>,
_marker2: marker::PhantomData<&'a V>,
}
impl<'a, K:Ord, V> Iterator for Iter<'a, K, V>
{
type Item = Ref<'a, Entry<K, V>>; //2
fn next(&mut self) -> Option<Self::Item> {
match self.ptr {
None => None,
Some(ref p) =>{
self.ptr = RefCell::borrow(p).next_by_height[0].as_ref().map(|x|Rc::clone(x));
Some(Ref::map(RefCell::borrow(p), |wrapped|{
wrapped.entry.as_ref().unwrap()
}))
}
}
}
}
error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
--> src\collections\skip_list\mod.rs:311:15
|
311 | match &self.ptr {
| ^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 310:5...
--> src\collections\skip_list\mod.rs:310:5
|
310 | fn next(&mut self) -> Option<Self::Item> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that reference does not outlive borrowed content
--> src\collections\skip_list\mod.rs:311:15
|
311 | match &self.ptr {
| ^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 306:6...
--> src\collections\skip_list\mod.rs:306:6
|
306 | impl<'a, K:Ord, V> Iterator for Iter<'a, K, V>
| ^^
note: ...so that the types are compatible
--> src\collections\skip_list\mod.rs:310:46
|
310 | fn next(&mut self) -> Option<Self::Item> {
| ______________________________________________^
311 | | match &self.ptr {
312 | | None => None,
313 | | Some(p) =>{
... |
327 | | // })
328 | | }
| |_____^
= note: expected `Iterator`
found `Iterator`
1
共 1 条评论, 1 页
评论区
写评论按源码里加上'a 通过了编译,不知道我引用的类库是不是对的。