Last active
February 16, 2022 19:17
-
-
Save RalfNorthman/ef0cef6f06921192daf34c90025b6de1 to your computer and use it in GitHub Desktop.
Fun with the usage crate
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
use usage::Usage; | |
struct Author; | |
struct CanonicalAuthor; | |
struct Title; | |
type AuthorId = Usage<Author, i32>; | |
type CanonicalAuthorId = Usage<CanonicalAuthor, i32>; | |
type TitleId = Usage<Title, i32>; | |
fn main() { | |
let tolkien_id = AuthorId::from(1); | |
let heinlein_id = AuthorId::from(2); | |
let dune_id = TitleId::from(1); | |
// Can't add without dereferencing: | |
let bingo = *tolkien_id + *heinlein_id; | |
println!("{bingo:?}"); | |
// Comparable as is: | |
if tolkien_id == heinlein_id { | |
println!("What, they are the same!?"); | |
} else { | |
println!("Not the same author."); | |
} | |
// Does not compile: | |
/* | |
if tolkien_id == dune_id { | |
println!("What, same!?"); | |
} else { | |
println!("What, comparable!?"); | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment