Make the passed Yielder borrow immutable [breaking-change]

Yielder::suspend only requires an immutable reference, and allowing
mem::replace on the yielder isn't very useful.
This commit is contained in:
edef 2016-09-16 04:34:49 -04:00
parent 045ad33785
commit 19ec5ef45b
2 changed files with 6 additions and 6 deletions

View File

@ -95,7 +95,7 @@ impl<'a, Input, Output, Stack> Generator<'a, Input, Output, Stack>
/// See also the [contract](../trait.GuardedStack.html) that needs to be fulfilled by `stack`.
pub fn new<F>(stack: Stack, f: F) -> Generator<'a, Input, Output, Stack>
where Stack: stack::GuardedStack,
F: FnOnce(&mut Yielder<Input, Output>, Input) + Send + 'a {
F: FnOnce(&Yielder<Input, Output>, Input) + Send + 'a {
unsafe { Generator::unsafe_new(stack, f) }
}
@ -107,18 +107,18 @@ impl<'a, Input, Output, Stack> Generator<'a, Input, Output, Stack>
///
/// See also the [contract](../trait.Stack.html) that needs to be fulfilled by `stack`.
pub unsafe fn unsafe_new<F>(stack: Stack, f: F) -> Generator<'a, Input, Output, Stack>
where F: FnOnce(&mut Yielder<Input, Output>, Input) + Send + 'a {
where F: FnOnce(&Yielder<Input, Output>, Input) + Send + 'a {
unsafe extern "C" fn generator_wrapper<Input, Output, Stack, F>(env: usize, stack_ptr: StackPointer) -> !
where Input: Send, Output: Send, Stack: stack::Stack,
F: FnOnce(&mut Yielder<Input, Output>, Input) {
F: FnOnce(&Yielder<Input, Output>, Input) {
// Retrieve our environment from the callee and return control to it.
let f = ptr::read(env as *const F);
let (data, stack_ptr) = arch::swap(0, stack_ptr, None);
// See the second half of Yielder::suspend_bare.
let input = ptr::read(data as *const Input);
// Run the body of the generator.
let mut yielder = Yielder::new(stack_ptr);
f(&mut yielder, input);
let yielder = Yielder::new(stack_ptr);
f(&yielder, input);
// Past this point, the generator has dropped everything it has held.
loop { yielder.suspend_bare(None); }
}

View File

@ -10,7 +10,7 @@ extern crate fringe;
use fringe::{SliceStack, OwnedStack, OsStack};
use fringe::generator::{Generator, Yielder};
fn add_one_fn(yielder: &mut Yielder<i32, i32>, mut input: i32) {
fn add_one_fn(yielder: &Yielder<i32, i32>, mut input: i32) {
loop {
if input == 0 { break }
input = yielder.suspend(input + 1)