微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将迭代器返回给 RefCell

如何解决将迭代器返回给 RefCell

我无法弄清楚如何在以下代码片段中实现 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. 也许我可以让 self 被消耗掉,这样我们就不必保留对它的任何引用?
fn children_iter(self) -> impl Iterator<Item=NodeIterator>

由于 self.current 寿命不够长而失败

  1. 我还尝试克隆 Rc 以使其不引用 self 中的那个
self.current
    .clone()
    .borrow()
    ......

这失败了“创建了一个临时文件,该临时文件仍在使用中被释放”

解决方法

想通了:

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

这样你就返回一个克隆的 Vec,它已经变成了一个迭代器

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。