-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explicit access to initial and final values of coroutine #68
Conversation
the |
There are some. Let's choose the better way: First approach: Use Usage: let mut coro1 = Coroutine::spawn(move |me| {
let initial = me.get_first();
me.yield_with(1);
me.set_last(2);
}); Pros:
Cons:
Second approach: Rename methods to something more meaningful ( Usage + Pros + Cons are the same as in first. Third approach: Use only one cross value. Usage: let mut coro1 = Coroutine::spawn(move |me| {
let initial = me.get_cross();
me.yield_with(1);
me.set_cross(2);
}); Pros:
Cons:
Fourth approach: Change signature of closure from Usage: let mut coro1 = Coroutine::spawn(move |me, initial| {
me.yield_with(1);
2
}); Pros:
Cons:
Fifth approach: Wrap spawn with additional closure for Usage: let mut coro1 = Coroutine::spawn(move |me| {
me.yield_with(1);
});
let mut coro2 = Coroutine::spawn_explicit(move |me, initial| {
me.yield_with(1);
2
});
// In fact `spawn` wraps it like:
fn spawn(f:F) {
spawn_explicit(|me, _| {
f(me);
usize:MAX
})
} Pros:
Cons:
I like 4th and 5th. What about you? |
For the 1st, 2nd, 3rd approaches, Hmm, I personally prefer the 4th approach, but it forces user to return an |
I think we have to follow 4th. Reasons:
My experience: when I was stumbled upon a segmentation fault, I spend time to find the cause, and explore library's code, but when somebody user who forced to use explicit values will take a bug he'll say: "Ok, I return bad value (pointer) in closure. It's simple to fix.". Is there no caveats I'll implement it? |
Sure. |
Done! ) |
PR #67 is great, but it has one imperfection: impossible to specify return value when coroutine ends (it return
usize::MAX
by default. I use library dinamically and take segfaults, becauseresume
expects valid value anytime. We can solve it if closure will return value explicitly, but it makes API quite ugly and forces users to rewrite more code.Another ides I tried:
usize::MAX
value, but I have to return additional data.yield
on tail, coroutine will contains invalidis_finished
status.I've made this PR to solve mentioned problems in better way. This PR takes profits:
resume
call (byget_first
method ofCoroutine
)resume
call (byset_last
method ofCoroutine
)FnOnce(&mut Coroutine)
to prevent user's code changingIt's more user-friendly, because API not changed and nobody have to change closures, but anyone can control first and last value explicitly.
@zonyitoo It needs your resoulition.