Menu

📰
0

Reddit - Please wait for verification

The Rust Programming Language·/u/maknobush·3 days ago
#X8Q0eCFV
Reading 0:00
15s threshold

I was thinking how most of my code usually is just: rust if let Some(x) = x {} which is fine, but i kept thinking "i could actually make some of these into compile-time checks". so i wrote a little proc-macro based crate to make this a little more fun to write. here's a simple example: `` rust use ductor::*; // this generates 3 structs: // - Door as family // - Open as state, with transition to Closed // - Closed as state, transition to Open [typestate] pub enum Door { #[transition(Closed)] Open, #[transition(Open)] Closed, } [unit(derive(Debug))] pub struct Lock; // generates an impl for you that // contains all the generics [spec(for = Open)] impl Lock { #[transit(to = Closed)] fn close(self){ self.transition(|Open| Closed) } } fn main(){ let lock = Lock::new(Closed, caps!()) .transition(|Closed| Open) .close(); // lock is now at Lock state } `` in here, we already know when the lock is opened, or closed. no need to check at runtime.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More