[][src]Crate arraygen

This crate provides Arraygen derive macro for structs, which generates methods returning arrays filled with the selected struct fields.

Complete example:

use arraygen::Arraygen;
 
#[derive(Arraygen, Debug)]
#[gen_array(fn get_names: &mut String)]
struct Person {
    #[in_array(get_names)]
    first_name: String,
    #[in_array(get_names)]
    last_name: String,
}
 
let mut person = Person {
    first_name: "Ada".into(),
    last_name: "Lovelace".into()
};
 
for name in person.get_names().iter_mut() {
    **name = name.to_lowercase();
}
 
assert_eq!(
    format!("{:?}", person),
    "Person { first_name: \"ada\", last_name: \"lovelace\" }"
);

As you can see above, the attribute gen_array generates a new method returning an array of the given type. And the attribute in_array selects those struct fields to be used by that method.

What Arraygen does under the hood is simply generating the following impl:

struct Person {
    first_name: String,
    last_name: String,
}
 
impl Person {
    #[inline(always)]
    fn get_names(&mut self) -> [&mut String; 2] {
        [&mut self.first_name, &mut self.last_name]
    }
}

Derive Macros

Arraygen