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
pub trait Cursor {
fn with_cursor(self, cursor: &str) -> Self;
fn cursor(&self) -> Option<&str>;
}
#[allow(unused_macros)]
macro_rules! impl_cursor {
($name:path) => {
impl Cursor for $name {
fn with_cursor(mut self, cursor: &str) -> $name {
self.cursor = Some(cursor.to_string());
self
}
fn cursor(&self) -> Option<&str> {
self.cursor.as_ref().map(|s| &**s)
}
}
};
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn it_can_be_derived() {
impl_cursor!(Foo);
struct Foo {
cursor: Option<String>,
}
let foo = Foo { cursor: None }.with_cursor("CURSOR");
assert_eq!(foo.cursor, Some("CURSOR".to_string()));
assert_eq!(foo.cursor(), Some("CURSOR"));
}
}