-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmacros.rs
More file actions
102 lines (94 loc) · 3.75 KB
/
macros.rs
File metadata and controls
102 lines (94 loc) · 3.75 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
macro_rules! apply_option {
(set_if_some, $builder:expr, $option:expr, $method:ident) => {
if let Some(value) = $option.take() {
$builder = $builder.$method(value);
}
};
(set_if_some_ref, $builder:expr, $option:expr, $method:ident) => {
if let Some(value) = $option.take() {
$builder = $builder.$method(&value);
}
};
(set_if_some_inner, $builder:expr, $option:expr, $method:ident) => {
if let Some(value) = $option.take() {
$builder = $builder.$method(value.0);
}
};
(set_if_some_map, $builder:expr, $option:expr, $method:ident, $transform:expr) => {
if let Some(value) = $option.take() {
$builder = $builder.$method($transform(value));
}
};
(set_if_some_map_ref, $builder:expr, $option:expr, $method:ident, $transform:expr) => {
if let Some(value) = $option.take() {
$builder = $builder.$method($transform(&value));
}
};
(set_if_true, $builder:expr, $option:expr, $method:ident, $default:expr) => {
if $option.unwrap_or($default) {
$builder = $builder.$method();
}
};
(set_if_true_with, $builder:expr, $option:expr, $method:ident, $default:expr, $value:expr) => {
if $option.unwrap_or($default) {
$builder = $builder.$method($value);
}
};
}
macro_rules! define_ruby_enum {
($(#[$meta:meta])* $enum_type:ident, $ruby_class:expr, $ffi_type:ty, $($variant:ident),* $(,)?) => {
define_ruby_enum!($(#[$meta])* $enum_type, $ruby_class, $ffi_type, $( ($variant, $variant) ),*);
};
($(#[$meta:meta])* const, $enum_type:ident, $ruby_class:expr, $ffi_type:ty, $($variant:ident),* $(,)?) => {
define_ruby_enum!($(#[$meta])* const, $enum_type, $ruby_class, $ffi_type, $( ($variant, $variant) ),*);
};
($(#[$meta:meta])* $enum_type:ident, $ruby_class:expr, $ffi_type:ty, $(($rust_variant:ident, $ffi_variant:ident)),* $(,)?) => {
$(#[$meta])*
#[magnus::wrap(class = $ruby_class, free_immediately, size)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types)]
#[allow(clippy::upper_case_acronyms)]
pub enum $enum_type {
$($rust_variant),*
}
impl $enum_type {
pub fn into_ffi(self) -> $ffi_type {
match self {
$(<$enum_type>::$rust_variant => <$ffi_type>::$ffi_variant,)*
}
}
pub fn from_ffi(ffi: $ffi_type) -> Self {
#[allow(unreachable_patterns)]
match ffi {
$(<$ffi_type>::$ffi_variant => <$enum_type>::$rust_variant,)*
_ => unreachable!(),
}
}
}
};
($(#[$meta:meta])* const, $enum_type:ident, $ruby_class:expr, $ffi_type:ty, $(($rust_variant:ident, $ffi_variant:ident)),* $(,)?) => {
$(#[$meta])*
#[magnus::wrap(class = $ruby_class, free_immediately, size)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[allow(non_camel_case_types)]
#[allow(clippy::upper_case_acronyms)]
pub enum $enum_type {
$($rust_variant),*
}
impl $enum_type {
pub const fn into_ffi(self) -> $ffi_type {
match self {
$(<$enum_type>::$rust_variant => <$ffi_type>::$ffi_variant,)*
}
}
#[allow(dead_code)]
pub const fn from_ffi(ffi: $ffi_type) -> Self {
#[allow(unreachable_patterns)]
match ffi {
$(<$ffi_type>::$ffi_variant => <$enum_type>::$rust_variant,)*
_ => unreachable!(),
}
}
}
};
}