Skip to content

Instantly share code, notes, and snippets.

@victor-iyi
Created August 23, 2022 18:12
Show Gist options
  • Save victor-iyi/7400fc7744b3b5807b4b32074e1c7ab2 to your computer and use it in GitHub Desktop.
Save victor-iyi/7400fc7744b3b5807b4b32074e1c7ab2 to your computer and use it in GitHub Desktop.
Handy use of Rust's declarative macro
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