macro_rules! define_accessor_trait {
{
$( #[ $attr:meta ])*
$vis:vis trait $Trait:ident {
$( $accessor:ident: $type:ty, )*
}
} => { ... };
}
Expand description
Define an “accessor trait”, which describes structs that have fields of certain types
This can be useful if a large struct, living high up in the dependency graph, contains fields that lower-lever crates want to be able to use without having to copy the data about etc.
// imagine this in the lower-level module
use tor_basic_utils::define_accessor_trait;
define_accessor_trait! {
pub trait View {
lorem: String,
ipsum: usize,
}
}
fn test_view<V: View>(v: &V) {
assert_eq!(v.lorem(), "sit");
assert_eq!(v.ipsum(), &42);
}
// imagine this in the higher-level module
use derive_more::AsRef;
#[derive(AsRef)]
struct Everything {
#[as_ref] lorem: String,
#[as_ref] ipsum: usize,
dolor: Vec<()>,
}
impl View for Everything { }
let everything = Everything {
lorem: "sit".into(),
ipsum: 42,
dolor: vec![()],
};
test_view(&everything);
Generated code
pub trait View: AsRef<String> + AsRef<usize> {
fn lorem(&self) -> &String { self.as_ref() }
fn ipsum(&self) -> &usize { self.as_ref() }
}