< 返回版块

坚果修补匠 发表于 2021-02-20 10:51

利用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`

评论区

写评论
w 2021-03-10 14:35

按源码里加上'a 通过了编译,不知道我引用的类库是不是对的。

use std::collections::btree_map::Entry;

struct SkipNode<'a, K: 'a, V: 'a> {
    entry: Option<Entry<'a, K, V>>,
    // next_by_height: SkipTrack<K, V>,
}

pub struct Iter<'a, K: Ord, V>
{
    ptr: Option<Rc<RefCell<SkipNode<'a, 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<'a, K, V>>; //2

    fn next(&mut self) -> Option<Self::Item> {
        match self.ptr {
            None => None,
            Some(ref p) => {

                Option::None
                // 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()
                // }))
            }
        }
    }
}

1 共 1 条评论, 1 页