[][src]Derive Macro getters_by_type::GettersByType

#[derive(GettersByType)]

The GettersByType macro automatically generates an impl for the given struct, implementing a getter method for each different type contained within the struct.

The generated methods start with the prefix get_fields_ and end with a transcription of the type they refer.

Example:

use getters_by_type::GettersByType;
#[derive(GettersByType)]
struct Foo {
    a: String,
    b: String,
}

// Would generete:

impl Foo {
    fn get_fields_string(&self) -> [&String; 2] {
        [&self.a, &self.b]
    }
}

As you notice, the chars of all the types (String in this case) go to the method signature in lowercase form.

It works the same with generic, reference and other types, with the following exceptions:

  1. Characters < > ( ) [ ] , ; always get converted to _.
  2. Return type arrow -> and reference character & get ignored completely.
  3. Pointer types *const and *mut get converted o ptr_const_ and ptr_mut_ respectively.

Also, reference types and non-reference types will be covered by the same method, as the methods are always returning references in the first place.

Example for fn, generic and reference types (more examples for other types later):

use getters_by_type::GettersByType;
#[derive(GettersByType)]
struct Foo {
    a: Result<i32, i32>,
    b: Result<i32, Result<i32, i32>>,
    c: fn(usize) -> f32,
    d: &'static bool,
    e: bool,
}

// Would generate:

impl Foo {
    fn get_fields_result_i32_i32_(&self) -> [&Result<i32, i32>; 1] {
        [&self.a]
    }
    fn get_fields_result_i32_result_i32_i32__(&self) -> [&Result<i32, Result<i32, i32>>; 1] {
        [&self.b]
    }
    fn get_fields_fn_usize___f32(&self) -> [&fn(usize) -> f32; 1] {
        [&self.c]
    }
    fn get_fields_bool(&self) -> [&bool; 2] {
        [&self.d, &self.e]
    }
}

Other examples of types to method signature conversions here:

use getters_by_type::GettersByType;
#[derive(GettersByType)]
struct Foo<'a> {
    a: &'a str,
    b: Box<Fn(i32) -> f32>,
    c: &'a Vec<&'a Option<&'a i32>>,
    d: Result<i32, Result<i32, Result<i32, &'static str>>>,
    e: Option<Option<Option<Option<Option<fn(usize)>>>>>,
    f: (i32, i32),
    g: [i32; 2],
    h: &'a [&'a Option<&'a i32>],
    i: *const i32,
    j: *mut i32,
    k: Box<dyn Bar>,
}
trait Bar {}
impl Bar for i32 {}
let vector = vec!();
let number_1 = 1;
let mut number_2 = 2;
let o = Foo {
    a: "",
    b: Box::new(|_| 0.0),
    c: &vector,
    d: Ok(0),
    e: None,
    f: (0, 0),
    g: [0, 0],
    h: vector.as_slice(),
    i: &number_1,
    j: &mut number_2,
    k: Box::new(0),
};
// from type: &'a str
o.get_fields_str();
// from type: Box<Fn(i32) -> f32>
o.get_fields_box_fn_i32_f32_();
// from type: &'a Vec<&'a Option<&'a i32>>
o.get_fields_vec_option_i32__();
// from type: Result<i32, Result<i32, Result<i32, &'static str>>>
o.get_fields_result_i32_result_i32_result_i32_str___();
// from type: Option<Option<Option<Option<Option<fn(usize)>>>>>
o.get_fields_option_option_option_option_option_fn_usize______();
// from type: (i32, i32)
o.get_fields__i32_i32_();
// from type: [i32; 2]
o.get_fields__i32_2_();
// from type: &'a [&'a Option<&'a i32>]
o.get_fields__option_i32__();
// from type: *const i32
o.get_fields_ptr_const_i32();
// from type: *mut i32
o.get_fields_ptr_mut_i32();
// from type: Box<dyn Bar>
o.get_fields_box_dyn_bar_();

Method visibility is inherited directly from the struct visibility, so if the struct is public, all the methods generated by GettersByType will be public too. There is no fine-grained control for fields visibility.

There are still some types not implemented. Those are the following:

  • TraitObject is partially implemented, Box<dyn Trait> works, but &dyn Trait doesn't.
  • Never
  • ImplTrait
  • Group
  • Infer
  • Macro
  • Verbatim

Hopefully, they will get implemented in next releases.