Last active
February 24, 2023 00:34
-
-
Save barrybtw/04564aabe4ec974335781ed9842f9f51 to your computer and use it in GitHub Desktop.
very clever rust developer
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
| #![allow(dead_code)] | |
| use std::fmt::{Display, Formatter}; | |
| struct Sad; | |
| struct Happy; | |
| #[derive(Debug)] | |
| enum RustLevel { | |
| Beginner, | |
| TrashDevLevel, | |
| Intermediate, | |
| DonutLevel, | |
| } | |
| impl Display for RustLevel { | |
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | |
| match self { | |
| RustLevel::Beginner => write!(f, "beginner"), | |
| RustLevel::TrashDevLevel => write!(f, "trash dev level"), | |
| RustLevel::Intermediate => write!(f, "intermediate"), | |
| RustLevel::DonutLevel => write!(f, "donut level"), | |
| } | |
| } | |
| } | |
| struct Barry<LifeState = Happy> { | |
| rust_level: RustLevel, | |
| life_state: std::marker::PhantomData<LifeState>, | |
| } | |
| impl Barry { | |
| pub fn new() -> Self { | |
| Barry { | |
| rust_level: RustLevel::Beginner, | |
| life_state: std::marker::PhantomData::<Happy>, | |
| } | |
| } | |
| } | |
| impl<LifeState> Barry<LifeState> { | |
| pub fn say_hello(&self) { | |
| println!("Hello am Barry am a {} rust developer", self.rust_level); | |
| } | |
| pub fn advance_rust_level(&mut self) { | |
| self.rust_level = match self.rust_level { | |
| RustLevel::Beginner => RustLevel::Intermediate, | |
| RustLevel::Intermediate => RustLevel::DonutLevel, | |
| RustLevel::DonutLevel => RustLevel::TrashDevLevel, | |
| RustLevel::TrashDevLevel => RustLevel::TrashDevLevel, | |
| } | |
| } | |
| } | |
| impl Barry<Sad> { | |
| pub fn cry(&self) { | |
| println!("barry cry now, no job... no trash livestrem..."); | |
| } | |
| pub fn get_job(self) -> Barry<Happy> { | |
| Barry { | |
| rust_level: self.rust_level, | |
| life_state: std::marker::PhantomData::<Happy>, | |
| } | |
| } | |
| } | |
| impl Barry<Happy> { | |
| pub fn dance(&self) { | |
| println!("barry dance now, baby shark duh duh duh duh duh duh"); | |
| } | |
| pub fn go_homeless(self) -> Barry<Sad> { | |
| Barry { | |
| rust_level: self.rust_level, | |
| life_state: std::marker::PhantomData::<Sad>, | |
| } | |
| } | |
| } | |
| fn main() { | |
| let barry = Barry::new(); | |
| barry.say_hello(); | |
| barry.dance(); | |
| let barry = barry.go_homeless(); | |
| barry.say_hello(); | |
| barry.cry(); | |
| let mut barry = barry.get_job(); | |
| barry.advance_rust_level(); | |
| barry.say_hello(); | |
| barry.dance(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment