Is there a proc macro crate that makes it easier to define "setters" that change type?

I want to do this, but shorter:

pub struct System<Foo, Bar> {
    foo: Foo,
    bar: Bar,
    baz: u32,
}

impl<Foo, Bar> System<Foo, Bar> {
    pub fn with_foo<Value>(self, foo: Value) -> System<Value, Bar> {
        let System { foo: _, bar, baz } = self;
        System { foo, bar, baz }
    }

    pub fn with_bar<Value>(self, bar: Value) -> System<Foo, Value> {
        let System { foo, bar: _, baz } = self;
        System { foo, bar, baz }
    }

    pub fn with_baz(self, baz: u32) -> System<Foo, Bar> {
        let System { foo, bar, baz: _ } = self;
        System { foo, bar, baz }
    }
}

impl System<(), ()> {
    pub fn new() -> Self {
        System { foo: (), bar: (), baz: 0 }
    }
}

Existing builder crates would define a new SystemBuilder type, which is not what I want. And the getset crate doesn't seem to be able to change type.

1 Like

Have not looked deeply into it, but derive_builder claims to have generic struct support.

I'm not aware of any crate that can do this (from a quick look, derive_builder creates a "builder" type, so it's no good).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.