jbaublitz/getset

Getters/setters for specific traits

ssokolen opened this issue · 1 comments

Is there any current way to automatically implement getter/setter functions for specific traits? For example, I would like to have a Private trait that relies on having a private() getter:

use getset::{CopyGetters, Getters, MutGetters, Setters};

pub trait Private {
    fn private(&self) -> &i32;
}

#[derive(Getters, Setters, MutGetters, CopyGetters, Default)]
pub struct Foo {
    #[getset(get, set, get_mut)]
    private: i32,
}

impl Private for Foo {};

fn main() {
    let mut foo = Foo::default();
    foo.set_private(1);
    (*foo.private_mut()) += 1;
    assert_eq!(*foo.private(), 2);
}

The above obviously fails unless I do something like:

impl Private for Foo {
    fn private(&self) -> &i32 {
        self.private() 
    }
}

The question is whether there is a way to specify in getset that private() should be implemented for the trait Public and avoid the extra boilerplate. And if not, is this something that could be implemented in the future?

There is not, I don't think that's in scope for this crate, sorry.