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

Introduce base builder type-alias #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

l4l
Copy link

@l4l l4l commented Jul 24, 2021

It's quite handy to have an alias rather than manually recreating the internal type.

fn make_builder() -> FooBuilder<((), (), (), ())> { .. }

// vs

fn make_builder() -> FooBuilder { .. }

@idanarye
Copy link
Owner

  1. Why would you ever need to specifically address the base builder type? I can imagine wanting to write a function that returns the builder with some fields filled in, or accept a builder with some fields filled in, or a struct that stores the builder with some fields filled in - but these all are not the base builder. The base builder itself is a fancy unit type.
  2. If you still want to do it - wouldn't it be simpler to use Default Generic Type Parameters?.

@l4l
Copy link
Author

l4l commented Jul 25, 2021

  1. I'd like to expose the type in my API, in particular builder for http client. Apparently the other, more generic option would be adding a trait, but that perhaps quite a big change.
  2. That sounds better, try to look at this.

@@ -126,6 +126,13 @@ impl<'a> StructInfo<'a> {
quote!(#[doc(hidden)])
};

let mut b_decl_generics = b_generics.clone();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required? b_generics is only used when declaring the builder type (and you want the default there) and when splitting it for the impl block (where defaults should not be looked at anyway). So why clone it instead of modifying the original?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's redundant, indeed.

@@ -126,6 +126,13 @@ impl<'a> StructInfo<'a> {
quote!(#[doc(hidden)])
};

let mut b_decl_generics = b_generics.clone();
for param in &mut b_decl_generics.params {
if let syn::GenericParam::Type(ref mut param) = param {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this match all type parameters? The one you want to add a default to is always the first one, so you can just modify that.

Or even better - just add the default when creating b_generics.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would match all parameters. I guess that barely make sense.

Though unfortunately rust doesn't allow to specify defaults for non-trailing generics and the TypedBuilderFields is the first one. I'm not sure whether it's fine to set it as the last one, perhaps it might break some code.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm half minded to just go with your original design as a temporary solution. Once const generics support strings I plan on doing a great refactor that'll break anything (other than regular by-the-book usage) anyway, and once that come I may remove it in favor of something else.

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

Successfully merging this pull request may close these issues.

None yet

2 participants