What is corosensei and how do coroutines work?
mastercorosensei provides a safe and efficient abstraction for context switching between different stacks using coroutines.
A coroutine is a function that can be paused and resumed, yielding values to the caller. It can suspend itself from any point in its call stack. This allows for bidirectional data flow: you can receive yielded values from a coroutine and pass data back into the coroutine each time it is resumed.
use corosensei::{Coroutine, CoroutineResult};
fn main() {
let mut coroutine = Coroutine::new(|yielder, input| {
// ... coroutine logic ...
let input = yielder.suspend(i);
// ...
});
loop {
match coroutine.resume(counter) {
CoroutineResult::Yield(i) => println!("got {:?}", i),
CoroutineResult::Return(()) => break,
}
}
}