derive-getters
is a nice crate that will create getters for you. However, it always generates getters that return a reference, even for Copy types. For example for this struct:
#[derive(Getters)]
pub struct MyCheesyStruct {
x: i64,
y: i64,
}
It will generate:
impl MyCheesyStruct {
pub fn x(&self) -> &i64 {
&self.x
}
pub fn y(&self) -> &i64 {
&self.y
}
}
This makes things more cumbersome for users than they need to be, because i64
implements Copy
. So I started to wonder if it was possible to do better? But macros operate at the token level, so they don't know anything about types/traits. Is there a way to get this functionality, short of manually annotating members with a directive to specify they should be returned without a borrow? This is the kind of thing type level metaprogramming in C++ excels at.