-
-
Save henninglive/3a9fd1b20a3e63a78481da087e3f895f 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