代码之家  ›  专栏  ›  技术社区  ›  jeanluc

将迭代器返回给RefCell

  •  0
  • jeanluc  · 技术社区  · 4 年前

    我不知道如何实施 children_iter 在以下代码片段中:

      use std::{cell::RefCell, rc::Rc};                                                                                                                                                                          
                                                                                                                                                                                                                 
      struct Node {                                                                                                                                                                                              
          children: Vec<Rc<RefCell<Node>>>,                                                                                                                                                                      
      }                                                                                                                                                                                                          
                                                                                                                                                                                                                 
      struct NodeIterator {                                                                                                                                                                                      
          current: Rc<RefCell<Node>>,                                                                                                                                                                            
      }                                                                                                                                                                                                          
                                                                                                                                                                                                                 
      impl NodeIterator {                                                                                                                                                                                        
          fn new(node: Rc<RefCell<Node>>) -> NodeIterator {                                                                                                                                                      
              NodeIterator { current: node }                                                                                                                                                                     
          }                                                                                                                                                                                                      
               
          /// Returns an iterator over the children of the current node wrapped in a NodeIterator                                                                                                                                                                                                  
          fn children_iter(&self) -> impl Iterator<Item = NodeIterator> {                                                                                                                                         
              self.current ‣Rc<RefCell<Node>>                                                                                                                                                                    
                  .borrow() ‣Ref<Node>                                                                                                                                                                           
                  .children ‣Vec<Rc<RefCell<Node>>>                                                                                                                                                              
                  .iter() ‣Iter<Rc<RefCell<Node>>>                                                                                                                                                               
                  .map(|n| Self::new(n.clone())) ‣&Rc<RefCell<Node>>                                                                                                                                             
          }                                                                                                                                                                                                      
      }                                                                                                                                                                                                          
                  
    

    问题是,当返回的迭代器迭代时,我不确定应该包含哪些子项。我尝试了几种不同的方法:

    1. 也许我可以将迭代器与以下对象的生命周期联系起来 self ?:
    fn children_iter<'a>(&'a self) -> impl 'a + Iterator<Item=NodeIterator>
    

    这会失败,因为它返回当前函数拥有的数据

    1. 也许我可以 自己 这样我们就不必保留任何形式的引用了吗?
    fn children_iter(self) -> impl Iterator<Item=NodeIterator>
    

    这失败了 self.current 寿命不够长

    1. 我也尝试过克隆 Rc 所以它不是指自我
    self.current
        .clone()
        .borrow()
        ......
    

    这会失败,因为“创建了一个在仍在使用时被释放的临时文件”

    1 回复  |  直到 4 年前
        1
  •  1
  •   jeanluc    4 年前

    弄清楚了:

    self.current
        .borrow()
        .children()
        .clone()
        .into_iter()
        .map(|n| Self::new(n))
    

    这样你就可以返回克隆的 Vec 它已被转化为迭代器