Hello,
I have the following trait:
pub trait Item {
pub fn labels(&self) -> &Vec<String>;
}
But since some Item implementors need interior mutability I have come to change the trait definition:
pub trait Item {
pub fn labels(&self) -> Ref<Vec<String>>;
}
Also, I would like to be able to use my implementor such as:
fn shop() -> Box<Iterator<Item = String>> {
let i1 = ItemImplementor::new();
Box::new(i1.labels().into_iterator())
}
The two issue I'm facing are:
- I have had to change Item API because of an implementation needed a
RefCell
which leaks it'sRef
to the API. Is it possible to avoid that? - At some point I want to boxed iterator over a
Ref
but it never works.
I tried to create a wrapper struct:
pub struct VecRefWrapper<'a, T: 'a> {
pub r: Ref<'a, Vec<T>>
}
impl<'a, 'b: 'a, T: 'a> IntoIterator for &'b VecRefWrapper<'a, T> {
type IntoIter = Iter<'a, T>;
type Item = &'a T;
fn into_iter(self) -> Iter<'a, T> {
self.r.iter()
}
}
And change my Item implementation like that:
pub trait Item {
pub fn labels(&self) -> VecRefWrapper<String>;
}
But then I can't do point #2:
fn shop<'a>() -> Box<dyn Iterator<Item = &'a String> + 'a> {
let i1 = ItemImplementor::new();
Box::new(i1.labels().into_iter())
}
shop();
Which gives the following error:
cannot return value referencing temporary value
returns a value referencing data owned by the current function
use `.collect()` to allocate the iterator
Note that I would like to avoid .collect()
as much as possible. But, if there is no other way...
Any idea on how to solve those two issues?