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
pub trait Limit {
fn with_limit(self, limit: u32) -> Self;
fn limit(&self) -> Option<u32>;
}
macro_rules! impl_limit {
($name:path) => {
impl Limit for $name {
fn with_limit(mut self, limit: u32) -> $name {
self.limit = Some(limit);
self
}
fn limit(&self) -> Option<u32> {
self.limit
}
}
};
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn it_can_be_derived() {
struct Foo {
limit: Option<u32>,
}
impl_limit!(Foo);
let foo = Foo { limit: None };
let foo = foo.with_limit(7);
assert_eq!(foo.limit(), Some(7));
}
}