On Sun, Apr 6, 2014 at 7:50 PM, Philippe Delrieu
<philippe.delr...@free.fr> 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"?

-- 
Rodrigo
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to