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"));
}
}