Created
August 23, 2022 18:12
-
-
Save victor-iyi/7400fc7744b3b5807b4b32074e1c7ab2 to your computer and use it in GitHub Desktop.
Handy use of Rust's declarative macro
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
trait MaxValue { | |
fn max_value() -> Self; | |
} | |
macro_rules! max_impl { | |
($t:ty) => { | |
impl crate::MaxValue for $t { | |
fn max_value() -> Self { | |
<$t>::max_value() | |
} | |
} | |
}; | |
} | |
// unsigned integers | |
max_value!(u8); | |
max_value!(u16); | |
max_value!(u32); | |
max_value!(u64); | |
max_value!(usize); | |
// signed integers | |
max_value!(i8); | |
max_value!(i16); | |
max_value!(i32); | |
max_value!(i64); | |
max_value!(isize); | |
// floats | |
max_value!(f32); | |
max_value!(f64); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment