How do I return an iterator that has a reference to something inside a RefCell?

醉酒当歌 提交于 2019-11-28 09:40:27

问题


I'm trying to create a method that returns an iterator over the values of HashMap that is boxed inside a RefCell, but i'm having an error where Ref returned by RefCell::borrow doesn't live long enough for iterator to be returned from the method. Here's my code:

use std::rc::Rc;
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::hash_map::Values;

struct Foo {
    map: Rc<RefCell<HashMap<i32, i32>>>,
}

impl Foo {
    fn iter(&self) -> Values<i32, i32> {
        self.map.borrow().values()
    }
}

fn main() {
    let foo = Foo {
        map: Rc::new(RefCell::new(HashMap::new()))
    };

    for v in foo.iter() {
        println!("{}", v)
    }
}

Compilation error:

rustc 1.15.1 (021bd294c 2017-02-08)
error: borrowed value does not live long enough
  --> <anon>:12:9
   |
12 |         self.map.borrow().values()
   |         ^^^^^^^^^^^^^^^^^ does not live long enough
13 |     }
   |     - temporary value only lives until here
   |

How do I return a reference to something inside a RefCell without breaking encapsulation? suggests creating a guard that incapsulates Ref and provides an interface for accessing the underlying value, but what I need to do is to return an iterator object (Values<'a, K, V>) that already incapsulates a plain reference to a HashMap.

My main problem is that I have a runtime tracked reference Ref<T> while I need a plain reference to create an iterator. Ref::map exposes a plain reference for mapping, but it requires the mapper function to return another reference which is impossible here. Should I redo the entire iterator functionality to work with Ref or is there a better way?


回答1:


You cannot do this.

The ultimate problem is that std::collections::hash_map::Values holds a reference, but you don't have "just" a reference. You have the smart pointer Ref.

The easiest solution I know of is to invert the code:

impl Foo {
    fn with_iter<F, T>(&self, f: F) -> T
    where
        F: FnOnce(Values<i32, i32>) -> T,
    {
        f(self.map.borrow().values())
    }
}

fn main() {
    let foo = Foo {
        map: Rc::new(RefCell::new(HashMap::new())),
    };

    foo.with_iter(|i| {
        for v in i {
            println!("{}", v)
        }
    })
}

Here, the Values iterator no longer needs to outlive the result of borrow, so there's no additional complexity.

If you are OK with leaking your implementation, you can return the Ref:

impl Foo {
    fn iter(&self) -> Ref<'_, HashMap<i32, i32>> {
        self.map.borrow()
    }
}
for v in foo.iter().values() {
    println!("{}", v)
}

In newer versions of Rust, you can return an unnamed type that implements Deref:

use std::ops::Deref;

impl Foo {
    fn iter(&self) -> impl Deref<Target = HashMap<i32, i32>> + '_ {
        self.map.borrow()
    }
}

See also:

  • How do I return a reference to something inside a RefCell without breaking encapsulation?


来源:https://stackoverflow.com/questions/42676541/how-do-i-return-an-iterator-that-has-a-reference-to-something-inside-a-refcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!