Currently, I can do something like:
let mut c: Vec<Box<FnMut() -> uint + 'static>> = vec![];
c.push(box |&:| 1u);
and then by using the gated manual Fn traits:
struct Counter {
count: uint,
}
impl FnMut() -> uint for Counter {
extern "rust-call" fn call_mut(&mut self, (): ()) -> uint {
let tmp = self.count;
self.count += 1;
tmp
}
}
I am then able to do
c.push(box Counter { count: 5 });
To get something with explicit state.
I’ve tried to construct something that behaves similar to the Counter
example, but uses closure notation rather than implementing the call
directly. I haven’t been able to figure out a way of doing this. It
seems to always either want to capture the environment by reference,
which gives it a non-'static lifetime, or won’t capture it at all.
Is this something that is desired? I guess it feels natural to me,
coming from functional languages, to be able to construct these types
of closures. Or is the intent just to do the manual impl FnMut...
to do this kind of thing?