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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// devela::code::util::methods
//
//! Defines [`methods_as_fns`].
//
// NOTE:
// keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern`
//
// - IMPROVE: support generics, other use cases.
// - TODO: make corresponding fns_as_methods.
/// Defines standalone functions that call associated methods.
///
/// Supports various function qualifiers (const, async, unsafe) and attributes.
///
/** # Examples
```
# use devela::methods_as_fns;
struct MyType;
impl MyType {
pub const fn add(a: i32, b: i32) -> i32 { a + b }
pub async fn fetch(url: &str) -> &str { "fetched" }
pub const unsafe fn dangerous() {}
}
methods_as_fns! {
pub MyType =>
#[inline]
+const add(a: i32, b: i32) -> i32,
#[must_use]
+async fetch(url: &str) -> &str,
+const +unsafe dangerous()
}
assert_eq!(add(5, 2), 7);
// syntax to name the functions differently than the method (can't be mixed):
methods_as_fns! {
pub(crate) MyType =>
add|addition(a: i32, b: i32) -> i32,
}
assert_eq!(addition(5, 2), 7);
```
**/
// we are employing a trick, e.g. `$(+const$($_c:lifetime)?)?` for the optional fn modifiers,
// where the never expected lifetime allows to refer to the non-identifier `+const` later on;
// and the same for the async and unsafe fn modifiers.
}
// calls a safe method
$*
$fn_vis $? $?
fn $method $?
)*
};
=> ;
}
pub use _methods_as_fns as methods_as_fns;
// TODO
/*
#[cfg(test)]
#[rustfmt::skip]
mod tests {
#![allow(dead_code)]
use crate::methods_as_fns;
struct TestMethods;
#[allow(dead_code)]
impl TestMethods {
pub const fn add(a: i32, b: i32) -> i32 { a + b }
pub fn sub(a: i32, b: i32) -> i32 { a - b }
pub fn trim(name: &str) -> &str { name.trim() }
pub async fn fetch(_url: &str) -> &str { "fetched" }
#[cfg(all(unsafe··, not(feature = "safe_code")))]
pub const unsafe fn noop() {}
}
// Test same-name pattern with attributes
methods_as_fns! {
pub TestMethods =>
#[inline] +const add(a: i32, b: i32) -> i32,
#[cold] #[allow(unused)] sub(a: i32, b: i32) -> i32,
#[must_use] +async fetch(url: &str) -> &str,
#[must_use] trim(name: &str) -> &str,
}
#[cfg(all(unsafe··, not(feature = "safe_code")))]
methods_as_fns! { pub TestMethods => +const +unsafe noop() }
// Test different-name pattern with attributes
methods_as_fns! {
pub(crate) TestMethods =>
#[inline(always)] +const add|add_numbers(a: i32, b: i32) -> i32,
#[cold] sub|subtract(a: i32, b: i32) -> i32,
+async fetch|fetch_data(url: &str) -> &str,
#[must_use] trim|do_trim(name: &str) -> &str,
}
#[cfg(all(unsafe··, not(feature = "safe_code")))]
methods_as_fns! { pub TestMethods => +const +unsafe noop|do_nothing() }
#[test]
fn test_all_variants() {
// Test same-name functions
crate::const_assert!(eq add(2, 3), 5);
assert_eq!(sub(5, 2), 3);
assert_eq!(trim(" world "), "world");
#[cfg(all(unsafe··, not(feature = "safe_code")))]
unsafe { noop(); }
// Test different-name functions
crate::const_assert!(eq add_numbers(2, 3), 5);
assert_eq!(subtract(5, 2), 3);
assert_eq!(do_trim(" test "), "test");
#[cfg(all(unsafe··, not(feature = "safe_code")))]
unsafe { do_nothing(); }
}
#[test]
#[cfg(feature = "dep_tokio")]
fn test_async_functions_with_tokio() {
use ::tokio::runtime::Builder;
assert_eq![
Builder::new_multi_thread().enable_all().build().unwrap().block_on(fetch("url")),
"fetched",
];
}
#[test]
#[cfg(feature = "std")]
#[cfg(not(feature = "dep_portable_atomic_util"))]
fn test_async_functions_with_ext_future() {
use crate::FutureExt;
assert_eq!(fetch("test.com").block_on(), "fetched");
assert_eq!(fetch_data("api.com").block_on(), "fetched");
}
}
*/