Last active
December 24, 2015 21:11
-
-
Save erickt/3bec54c33b6e76f6e6a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(plugin)] | |
#![plugin(stateful)] | |
#![allow(unused_variables)] | |
#[state_machine] | |
fn yield_(mut i: usize, then: usize, else_: usize) -> usize { | |
loop { | |
if i < 10 { | |
i += 1; | |
return i; | |
} else { | |
break; | |
}; | |
}; | |
} | |
fn main() { | |
for value in yield_(0, 0, 1).take(20) { | |
println!("yield_: {:?}", value); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(prelude_import)] | |
#![no_std] | |
#![feature(plugin)] | |
#![plugin(stateful)] | |
#![allow(unused_variables)] | |
#[prelude_import] | |
use std::prelude::v1::*; | |
#[macro_use] | |
extern crate std as std; | |
fn yield_(mut i: usize, | |
then: usize, | |
else_: usize) | |
-> ::std::boxed::Box<::std::iter::Iterator<Item = usize>> { | |
struct Wrapper<S, F> { | |
state: S, | |
next: F, | |
} | |
impl<S, T, F> Wrapper<S, F> where F: Fn(S) -> (Option<T>, S) | |
{ | |
fn new(initial_state: S, next: F) -> Self { | |
Wrapper { | |
state: initial_state, | |
next: next, | |
} | |
} | |
} | |
impl<S, T, F> Iterator for Wrapper<S, F> | |
where S: Default, | |
F: Fn(S) -> (Option<T>, S) | |
{ | |
type | |
Item | |
= | |
T; | |
fn next(&mut self) -> Option<Self::Item> { | |
let old_state = ::std::mem::replace(&mut self.state, S::default()); | |
let (value, next_state) = (self.next)(old_state); | |
self.state = next_state; | |
value | |
} | |
} | |
enum State<T0, | |
T1, | |
T2, | |
T3, | |
T4, | |
T5, | |
T6, | |
T7, | |
T8, | |
T9, | |
T10, | |
T11, | |
T12, | |
T13, | |
T14, | |
T15, | |
T16, | |
T17, | |
T18, | |
T19, | |
T20, | |
T21, | |
T22, | |
T23, | |
T24, | |
T25, | |
T26> | |
{ | |
State0Entry(T0, T1, T2), | |
State2LoopEntry(T3, T4, T5), | |
State3LoopExit(T6, T7, T8), | |
State4Then(T9, T10, T11), | |
State5Else(T12, T13, T14), | |
State6EndIf(T15, T16, T17), | |
State7Yield(T18, T19, T20), | |
State8BlockExit(T21, T22, T23), | |
State9BlockExit(T24, T25, T26), | |
State1Exit, | |
} | |
impl <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, | |
T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26> Default | |
for | |
State<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, | |
T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26> { | |
fn default() -> Self { State::State1Exit } | |
} | |
Box::new(Wrapper::new(State::State0Entry(i, then, else_), |mut state| { | |
loop { | |
match state { | |
State::State0Entry(mut i, then, else_) => { | |
state = State::State2LoopEntry(i, then, else_); | |
continue; | |
} | |
State::State2LoopEntry(mut i, then, else_) => { | |
if i < 10 { | |
state = State::State4Then(i, then, else_); | |
continue; | |
} else { | |
state = State::State5Else(i, then, else_); | |
continue; | |
} | |
} | |
State::State3LoopExit(mut i, then, else_) => { | |
state = State::State9BlockExit(i, then, else_); | |
continue; | |
} | |
State::State4Then(mut i, then, else_) => { | |
i += 1; | |
return (::std::option::Option::Some(else_), | |
State::State7Yield(i, then, else_)); | |
} | |
State::State5Else(mut i, then, else_) => { | |
state = State::State3LoopExit(i, then, else_); | |
continue; | |
state = State::State8BlockExit(i, then, else_); | |
continue; | |
} | |
State::State6EndIf(mut i, then, else_) => { | |
state = State::State2LoopEntry(i, then, else_); | |
continue; | |
} | |
State::State7Yield(mut i, then, else_) => { | |
state = State::State6EndIf(i, then, else_); | |
continue; | |
} | |
State::State8BlockExit(mut i, then, else_) => { | |
state = State::State6EndIf(i, then, else_); | |
continue; | |
} | |
State::State9BlockExit(mut i, then, else_) => { | |
state = State::State1Exit; | |
continue; | |
} | |
State::State1Exit => { | |
return (::std::option::Option::None, State::State1Exit); | |
} | |
} | |
} | |
})) | |
} | |
fn main() { | |
for value in yield_(0, 0, 1).take(20) { | |
::std::io::_print(::std::fmt::Arguments::new_v1({ | |
static __STATIC_FMTSTR: | |
&'static [&'static str] | |
= | |
&["yield_: ", | |
"\n"]; | |
__STATIC_FMTSTR | |
}, | |
&match (&value,) { | |
(__arg0,) => | |
[::std::fmt::ArgumentV1::new(__arg0, | |
::std::fmt::Debug::fmt)], | |
})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment