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
**self.nodeList.get(self.iter_counter){
test/test_enum.rs:59 FirstThinkImpl(first) => first as
~Base,
test_enum.rs:60 SecondThinkImpl(second)=> second as ~Base,
test_enum.rs:61 })
test_enum.rs:53:52: 63:6 note: reference must be valid for the lifetime
&'a as defined on the block at 53:51...
test_enum.rs:53 fn next(&mut self) -> Option<&'a mut ~Base> {
test_enum.rs:54 if self.iter_counter == self.nodeList.len() {
test/test_enum.rs:55 None
test_enum.rs:56 } else {
test_enum.rs:57 self.iter_counter += 1;
test_enum.rs:58 Some(&'a mut match
**self.nodeList.get(self.iter_counter){
...
test_enum.rs:56:17: 62:10 note: ...but borrowed value is only valid for
the expression at 56:16
test_enum.rs:56 } else {
test_enum.rs:57 self.iter_counter += 1;
test_enum.rs:58 Some(&'a mut match
**self.nodeList.get(self.iter_counter){
test_enum.rs:59 FirstThinkImpl(first) => first as ~Base,
test_enum.rs:60 SecondThinkImpl(second)=> second as ~Base,
test/test_enum.rs:61 })
...
test_enum.rs:59:17: 59:38 error: cannot move out of dereference of
`&`-pointer
test_enum.rs:59 FirstThinkImpl(first) => first as ~Base,
^~~~~~~~~~~~~~~~~~~~~
test_enum.rs:60:17: 60:40 error: cannot move out of dereference of
`&`-pointer
test_enum.rs:60 SecondThinkImpl(second)=> second as ~Base,
Le 05/04/2014 21:59, Rodrigo Rivas a écrit :
On Fri, Apr 4, 2014 at 10:41 PM, Philippe Delrieu
<philippe.delr...@free.fr> wrote:
Hello,
I've some problem to find a solution for something I want to do with a
vector of enum. This is an example of what I want to do:
trait Base {
fn set_something(&mut self);
}
struct FirstThink;
impl Base for FirstThink {
fn set_something(&mut self) {}
}
struct SecondThink;
impl Base for SecondThink {
fn set_something(&mut self) {}
}
enum BaseImpl {
FirstThinkImpl(~FirstThink),
SecondThinkImpl(~SecondThink),
}
fn some_process(list: &mut Vec<&mut ~Base>) {
for think in list.mut_iter() {
think.set_something();
}
}
struct Container {
nodeList: Vec<~BaseImpl>,
}
impl Container {
fn add_FirstThink(&mut self, think: ~FirstThink) {
self.nodeList.push(~FirstThinkImpl(think));
}
fn add_SecondThink(&mut self, think: ~SecondThink) {
self.nodeList.push(~SecondThinkImpl(think));
}
fn do_some_process(&mut self, fct: fn(&mut Vec<&mut ~Base>)) {
I didn't find a simple way to convert the Vec<~BaseImpl> to a &mut
Vec<&mut ~Base>
to do the call
fct(self.nodeList);
}
}
I use the enum pattern to have only one collection of object that impl Base
but sometime I have to do specific processing depending if the Base is a
FirstThink or SecondThink (not in the example). I use the match as follow
match think {
FirstThinkImpl(first) => do specific first,
SecondThinkImpl(second)=> do specific second,
});
Perhaps there is a better way to do. Any suggestions would be appreciated.
I think that would be better if the `fct` function take an
`std::iter::Iterator<&mut ~Base>` instead of a `Vec`. Naturally, you
will not be able to modify the vector, only to iterate through it. But
if `fct` is allowed to modify the vector your requirements are
impossible in the first place!
Then you can write a simple adaptor:
impl std::iter::Iterator<&mut ~Base> for Container {
// left as an exercise to the reader ;-)
}
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev