Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add traits for matching on and transforming the generic parameter of the builder type #22

Open
idanarye opened this issue Dec 12, 2019 · 0 comments

Comments

@idanarye
Copy link
Owner

With #21 we want to convert all the builder type's generic parameters to a single tuple of types. Here we want to add traits for it.

Say we have:

#[derive(TypedStruct)]
pub struct Foo {
    pub bar: u32,
}

Our builder type will be (omitting the phantom field):

pub struct FooBuilder<F> {
    fields: F,
}

To allow extending the builder type in a forward-compatible way, we want to add the following traits:

trait FooBuilderWithBar {
    fn get(&self) -> u32;
}

trait FooBuilderWithoutBar {
    type With: FooBuilderWithBar;
    fn set(self, bar: u32) -> Self::With;
}

These traits will operate on F, not on FooBuilder<F>, and allow extending FooBuilder like so:

impl<F: FooBuilderWithoutBar> FooBuilder<F> {
    pub fn bar_as_42(self) -> <F as FooBuilderWithoutBar>::With {
        Foo {
	    fields: FooBuilderWithoutBar::set(self, 42),
	}
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant