Skip to content

Provide ability to create getters that return A<&T> instead of &A<T> #78

Open
@alopatindev

Description

@alopatindev

Thanks for this awesome crate!

#[derive(Default, getset::Getters)]
struct A {
    #[get = "pub"]
    b: Option<B>,
}

#[derive(Default)]
struct B;

fn main() {
    let a = A::default();
    let b: Option<&B> = a.b();
}
error[E0308]: mismatched types
  --> src/main.rs:12:25
   |
12 |     let b: Option<&B> = a.b();
   |            ----------   ^^^^^
   |            |            |
   |            |            expected enum `Option`, found `&Option<B>`
   |            |            help: you can convert from `&Option<T>` to `Option<&T>` using `.as_ref()`: `a.b().as_ref()`
   |            expected due to this
   |
   = note:   expected enum `Option<&B>`
           found reference `&Option<B>`

Getters that return &Option<T> (just like &Result<T>, etc.) are not really useful, as they require getter user to pollute code with .as_ref() everywhere. As alternative, having manual trivial getters like

impl A {
    pub fn b(&self) -> Option<&B> {
        self.b.as_ref()
    }
}

is just another kind of boilerplate code pollution.

Can we do anything with this (and not some breaking change at the same time of course)?

I guess solution would likely require working with each such type separately (perhaps adding OptionGetters and #[get_option = "pub"], etc.), but still this would make this crate more coherent for users.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions