Riven/riven/src/consts/macros.rs

98 lines
2.5 KiB
Rust
Raw Normal View History

2021-07-01 01:50:08 +00:00
#![macro_use]
2021-07-01 02:35:08 +00:00
macro_rules! string_enum_str {
( $name:ident ) => {
impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
match self {
Self::UNKNOWN(string) => &*string,
known => known.into(),
}
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
self.as_ref().fmt(f)
}
}
}
}
2021-07-01 01:50:08 +00:00
macro_rules! arr {
(
$( #[$attr:meta] )*
$v:vis $id:ident $name:ident: [$ty:ty; _] = $value:expr
) => {
$( #[$attr] )*
$v $id $name: [$ty; $value.len()] = $value;
}
}
macro_rules! newtype_enum {
{
$( #[$attr:meta] )*
2021-07-01 02:35:08 +00:00
$v:vis newtype_enum $name:ident($repr:ty) {
2021-07-01 01:50:08 +00:00
$(
$( #[$var_attr:meta] )*
$var_name:ident = $var_val:expr,
)*
}
} => {
$( #[$attr] )*
#[derive(Copy, Clone)]
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord)]
2021-07-01 01:50:08 +00:00
#[repr(transparent)]
$v struct $name($v $repr);
impl $name {
$(
$( #[$var_attr] )*
2021-07-01 02:35:08 +00:00
$v const $var_name: Self = Self($var_val);
2021-07-01 01:50:08 +00:00
)*
}
impl $name {
arr!{
#[doc = "Array containing all known variants."]
2021-07-01 01:50:08 +00:00
pub const ALL_KNOWN: [Self; _] = [
$( Self::$var_name, )*
]
}
#[doc = "If this is one of the known variants."]
$v const fn is_known(self) -> bool {
2021-07-01 01:50:08 +00:00
match self {
$(
Self::$var_name => true,
)*
_ => false,
}
}
}
impl std::convert::From<$repr> for $name {
fn from(value: $repr ) -> Self {
Self(value)
}
}
impl std::convert::From<$name> for $repr {
fn from(value: $name ) -> Self {
value.0
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl std::fmt::Debug for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}({}{})", stringify!($name), self.0, if self.is_known() { "" } else { "?" })
}
}
}
}