Created
February 13, 2014 05:54
-
-
Save bvssvni/8970459 to your computer and use it in GitHub Desktop.
This file contains 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
/// Phantom type for step 1. | |
pub enum Step1 {} | |
/// Phantom type for step 2. | |
pub enum Step2 {} | |
/// Contains data we set step by step. | |
pub struct Data<'a> { | |
/// 'a' is set in the first step. | |
a: Option<int>, | |
/// 'b' is set in the second step. | |
b: Option<&'a str> | |
} | |
/// Wraps data for using a safe state machine using phantom type. | |
pub struct State<'a, Step> { | |
data: &'a mut Data<'a> | |
} | |
impl<'a> Data<'a> { | |
/// Create data used for initializing the steps. | |
pub fn new() -> Data<'a> { | |
Data { | |
a: None, | |
b: None | |
} | |
} | |
/// Execute step 1. | |
pub fn a(&'a mut self, a: int) -> State<'a, Step1> { | |
self.a = Some(a); | |
State { data: self } | |
} | |
} | |
impl<'a> State<'a, Step1> { | |
/// Execute step 2, but only if step 1 is made first. | |
pub fn b(self, b: &'a str) -> State<'a, Step2> { | |
self.data.b = Some(b); | |
State { data: self.data } | |
} | |
/// Go back to initial data. | |
/// Returns a mutable borrowed pointer to be able to redo steps. | |
pub fn _0(self) -> &'a mut Data<'a> { | |
self.data.a = None; | |
self.data | |
} | |
} | |
impl<'a> State<'a, Step2> { | |
/// Print values. | |
pub fn print(&self) { | |
println!("{} {}", self.data.a.unwrap(), self.data.b.unwrap()); | |
} | |
/// Go back to step 1. | |
pub fn _1(self) -> State<'a, Step1> { | |
self.data.b = None; | |
State { data: self.data } | |
} | |
} | |
fn main() { | |
let mut data = Data::new(); | |
let step1 = data.a(42); | |
let step2 = step1.b("hello!"); | |
// Prints "42 hello!". | |
step2.print(); | |
// Go back one step. | |
let step1 = step2._1(); | |
let step2 = step1.b("try again!"); | |
// Prints "42 try again!". | |
step2.print(); | |
// Go back to the beginning. | |
let data = step2._1()._0(); | |
// Prints "values None None". | |
println!("values {} {}", data.a, data.b); | |
// Short version. | |
data.a(32).b("hello again!").print(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The advantage using phantom types is intersecting the same name space for functions, such that there can only be one 'a' functions for all the steps. If you don't want this you can use newtypes or struct for each step.
If you want the state machine to be required, you can declare the members of 'Data' and 'State' with 'priv' to prevent code outside the module to dereference the members directly.
Also, you can't dereference a member of Data while it is borrowed as mutable by a state. The safety of Rust ensures that you can work around a state machine if you like to, but you can't use a state machine and the workaround at the same time.