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

Allows prevention of alloc dependency through feature and disabling of ValidationError(String) variant. #302

Merged
merged 6 commits into from
Jan 21, 2024

Conversation

red1bluelost
Copy link
Contributor

@red1bluelost red1bluelost commented Dec 22, 2023

Currently, using derive_builder in no_std still forces the user to make a global_allocator since the crate links against alloc. This PR tries to avoid that in two aways.

  • Create a new feature flag alloc that controls whether the alloc crate is used. This allows no_std user to opt into using allocation if they want it.
  • New build_fn field, validation_error: bool that controls whether the generated error includes ValidationError(String). This prevents usage of any alloc code but also limits the user from using the validate field.
    • The user can provide a custom build_fn error to avoid alloc code from being used.

This is to fix issue: #303.

@@ -231,15 +237,35 @@ impl<'a> ToTokens for Builder<'a> {
let builder_error_ident = format_ident!("{}Error", builder_ident);
let builder_error_doc = format!("Error type for {}", builder_ident);

let validation_error = self
.generate_validation_error
.then_some(quote!(
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is pretty cool, and I hadn't seen it before. However, if the discussion in #303 does lead to this being on track for a merge, we need to avoid using a method that isn't available to 1.56.0; can you change each use of it to an if block?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the code then built and ran cargo test using 1.56.0 x86_64.

@TedDriggs
Copy link
Collaborator

Apologies; I didn’t get a chance to review this today, so it’ll probably be about a week before I next get the chance to. I do intend to review it, and think we are on track towards merging this

Copy link
Collaborator

@TedDriggs TedDriggs left a comment

Choose a reason for hiding this comment

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

Did my best to review from mobile

@@ -95,6 +95,25 @@ pub struct BuildFn {
/// * If `validate` is specified, then this type must provide a conversion from the specified
/// function's error type.
error: Option<Path>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm wondering if we should make this into a type that can either be a key-value pair with a path, or a list that accepts options about the generated error:

error = "path"

error(validation = false)

The advantage would be that it's then impossible to incorrectly disable the validation variant when using a custom error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I managed to implement it so it supports the two forms from your comment.

derive_builder_core/src/macro_options/darling_opts.rs Outdated Show resolved Hide resolved
@red1bluelost
Copy link
Contributor Author

Thank you for the review. No worry about time, I'm in not in a hurry for this feature.

I'm looking into the workflow breakage. This PR has somewhat of a breaking change since alloc is an additive feature for something that was previously always enabled. For the workflow, --no-default-features before this PR is similar to --no-default-features --features alloc now.

@red1bluelost
Copy link
Contributor Author

My most recent commit addresses the GH workflows. It adds alloc feature on --no-default-features to get the same no_std with alloc behavior as before.

I also added a compile error when std and alloc are disabled but a user attempts to derive with alloc::String usage. It gives suggestion to enable the alloc feature or avoid String by using custom error or disabling validation error. This should help no std users update there code when upgrading.

@TedDriggs
Copy link
Collaborator

I just pushed a simplified version of the BuildFnError code here, but I'm not sure why it isn't showing up in the PR.

@red1bluelost
Copy link
Contributor Author

I just pushed a simplified version of the BuildFnError code here, but I'm not sure why it isn't showing up in the PR.

I think it's because this PR is on my fork while your code is a branch here. I pulled the commit and just pushed it.

Thanks for simplifying the macro code! I had a feeling that there was a nicer way to do it but this was my first time using darling_opt :)

@TedDriggs
Copy link
Collaborator

That's totally fine! I'm the author of darling, so I know how to get the most out of its capabilities 😀

@TedDriggs
Copy link
Collaborator

Please rebase this; I think that will fix the CI issue. Once we have a clean bill of health from CI, we are ready to merge.

@red1bluelost
Copy link
Contributor Author

Rebased it 👍

@TedDriggs TedDriggs merged commit 6c0afc5 into colin-kiegel:master Jan 21, 2024
15 checks passed
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