Last active
February 26, 2017 01:06
-
-
Save anonymous/5296e4100523e30cd8324cd5e73bf7fd to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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
| use std::marker::PhantomData; | |
| trait SaveState {} | |
| struct NoSave; | |
| struct Saved; | |
| struct Keyboard<S: SaveState> { | |
| save: PhantomData<S>, | |
| } | |
| impl SaveState for NoSave {} | |
| impl SaveState for Saved {} | |
| impl Keyboard<NoSave> { | |
| fn new() -> Keyboard<NoSave> { | |
| Keyboard { | |
| save: PhantomData, | |
| } | |
| } | |
| fn save(self) -> Keyboard<Saved> { | |
| Keyboard { | |
| save: PhantomData, | |
| } | |
| } | |
| } | |
| impl Keyboard<Saved> { | |
| fn restore(self) -> Keyboard<NoSave> { | |
| Keyboard { | |
| save: PhantomData, | |
| } | |
| } | |
| } | |
| fn main() { | |
| let key1 = Keyboard::new(); | |
| let key2 = key1.save(); | |
| let key3 = key2.restore(); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment