Warning
This post was published 32 days ago. The information described in this article may have changed.
The following code works:
struct MyState(Vec<usize>);
fn logic(state: &mut MyState) -> impl Iterator<Item = usize> {
state.0.iter().cloned()
}
struct Engine<State, Logic> {
state: State,
logic: Logic,
}
impl<State, Logic> Engine<State, Logic> {
fn run<'a, Iter>(&'a mut self)
where
Logic: FnMut(&'a mut State) -> Iter,
Iter: Iterator<Item = usize> + 'a,
{
for x in (self.logic)(&mut self.state) {
println!("got {x}");
}
}
}
fn main() {
let state = MyState(vec![1, 2, 3]);
let mut engine = Engine { state, logic };
engine.run();
engine.run();
}
I'd now like to change it so that Engine::run
consumes self
rather than taking &mut self
but I am struggling to figure it out. Note that:
Box
or dynamic dispatch anything.+ use<>
on the logic function return type).5 posts - 3 participants
🏷️ rust_feed