I try to implement the iterator like that:
struct BaseItems<'a> {
iter : Items<'a, ~BaseImpl>
}
impl<'a> Iterator<&'a ~Base> for BaseItems<'a> {
fn next(&mut self) -> Option<&'a ~Base> {
match self.iter.next() {
Some(ref baseimpl) => {
Some(&'a match ***baseimpl{
FirstThinkImpl(ref first) => *first as ~Base,
SecondThinkImpl(ref second)=> *second as ~Base,
})
},
None => None,
}
}
}
But I have a lifetime problem. The error is : borrowed value does not
live long enough and reference must be valid for the lifetime &'a as
defined on the block
and :
cannot move out of dereference of `&`-pointer SecondThinkImpl(ref
second)=> *second as ~Base,
Another possibility:
fn next(&mut self) -> Option<&'a ~Base> {
match self.iter.next() {
Some(ref baseimpl) => {
Some(match ***baseimpl{
FirstThinkImpl(ref first) => first as &'a ~Base,
SecondThinkImpl(ref second)=> second as &'a ~Base,
})
},
None => None,
}
}
generate the error: non-scalar cast: `&~FirstThink` as `&'a
~Base<no-bounds>`
I try different possibility but I didn't find how to return a <'a>
lifetime ~Base or Base
I remove the mut to simplify the test of the different lifetime
possibilities.
Philippe
Le 07/04/2014 10:27, Rodrigo Rivas a écrit :
On Sun, Apr 6, 2014 at 7:50 PM, Philippe Delrieu
<[email protected]> wrote:
I need some more help.
The impl Iterator<&mut ~Base> for Container declaration generate the error:
error: missing lifetime specifier
So I had it but I can't manage to return the next value with the specified
life time.
The code :
impl<'a> Iterator<&'a mut ~Base> for Container {
/// Advance the iterator and return the next value. Return `None` when
the end is reached.
fn next(&mut self) -> Option<&'a mut ~Base> {
if self.iter_counter == self.nodeList.len() {
None
} else {
self.iter_counter += 1;
Some(&'a mut match **self.nodeList.get(self.iter_counter){
FirstThinkImpl(first) => first as ~Base,
SecondThinkImpl(second)=> second as ~Base,
})
}
}
}
Generate these errors :
test_enum.rs:58:18: 61:14 error: borrowed value does not live long enough
test/test_enum.rs:58 Some(&'a mut match
Oh, I think I may have misleaded you... You cannot implement the
iterator directly in Container, because the iterator must handle the
current position, while the Container just holds the values. You need
a intermediate struct that implements the Iterator traits. That's what
the `iter()` and ' move_iter()` functions do for vectors and other
standard containers. So you'll need something along the lines of this
(disclaimer: totally untested!!):
struct Container {
//....
fn iter(&'a self) -> BaseItems<'a> {
let iter = nodeList.iter();
BaseItems{ iter : iter }
}
}
struct BaseItems<'a> {
iter : Items<'a, ~Base>
}
impl<'a> Iterator<&'a mut ~Base> for BaseItems<'a> {
//....
}
BTW, why all the double pointer in all the "&mut ~Base" instead of
just "&mut Base"?
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev