Attribute Macro corresponding_macros::derive_corresponding
source · [−]#[derive_corresponding]Expand description
Use this macro on a module to generate MoveCorresponding implementations for all structs in this module. For all structs deriving Default also the From trait will be implemented.
Example
use corresponding::derive_corresponding;
#[derive_corresponding]
mod my_mod {
#[derive(Debug, Default)]
pub struct A {
pub a: u8,
pub b: u8,
pub c: u8,
}
#[derive(Debug, Clone)]
pub struct B {
pub a: u8,
pub b: Option<u8>,
pub d: u8,
}
}This will implement MoveCorresponding for all combinations of structs within the crate module. The generated implementations are zero cost abstractions and will look like:
impl MoveCorresponding<B> for A {
fn move_corresponding(&mut self, rhs: B) {
self.a = rhs.a;
if let Some(r) = rhs.b {
self.b = r;
}
}
}
impl MoveCorresponding<A> for B {
fn move_corresponding(&mut self, rhs: A) {
self.a = rhs.a;
self.b = Some(rhs.b);
}
}Because struct A derives Default, it will also implement From. The generated implementation looks like:
impl From<B> for A {
fn from(rhs: B) -> Self {
let mut lhs = A::default();
lhs.move_corresponding(rhs);
lhs
}
}