1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
 * Implements Default for an enum.
 * Requires the enum to have a variant named "Default"
 */
#[macro_export]
macro_rules! enum_default {
	($name: ident) => {
		impl Default for $name {
			fn default() -> Self {
				$name::Default
			}
		}
	};
}

/**
 * Implements a fmt trait for an enum.
 * The output is the enum as a number
 */
#[macro_export]
macro_rules! enum_fmt_impl {
	($name: ident, $trait: ident) => {
		impl $trait for $name {
			fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
				$trait::fmt(&(self.clone() as u8), f)
			}
		}
	};
}

/**
 * Implements the Display trait for an enum.
 * The output is the enum as a number
 */
#[macro_export]
macro_rules! enum_display {
	($name: ident) => {
		impl Display for $name {
			fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
				write!(f, "{}", self.clone() as u8)
			}
		}
	};
}

/** Implements several traits for a macro */
#[macro_export]
macro_rules! enum_impls {
	($name: ident) => {
		enum_default!($name);
		enum_display!($name);
		enum_fmt_impl!($name, Binary);
		enum_fmt_impl!($name, Octal);
		enum_fmt_impl!($name, LowerHex);
		enum_fmt_impl!($name, UpperHex);
	};
}

/** Implements several enums */
#[macro_export]
macro_rules! impl_enums {
	($($enum: ident),*) => {
		$(enum_impls!($enum);)*
	};
}

/** adds a set of functions to the trait */
#[macro_export]
macro_rules! chalk_trait_fns {
	($($name: ident),*) => {
		$(fn $name(&mut self) -> &mut Self;)*
	};
}

/** Sets up an alias for a function */
#[macro_export]
macro_rules! fn_alias {
	($alias: ident, $fn: ident) => {
		fn $alias(&mut self) -> &mut Self {self.$fn()}
	};
}