colin-kiegel / rust-derive-builder

derive builder implementation for rust structs
https://colin-kiegel.github.io/rust-derive-builder/
Apache License 2.0
1.32k stars 88 forks source link

Allow way to prevent dependency on crate `alloc`. #303

Closed red1bluelost closed 9 months ago

red1bluelost commented 10 months ago

When using the library with no_std, derive_builder's dependency on alloc forces the user to provide a global_allocator even if they don't use anything that would require allocations (i.e. *BuilderError::ValidationError(String)). It would be helpful for some no_std libraries to have a way to disable this dependency on alloc.

I made a PR as a possible solution for this. It provides a feature alloc that controls whether extern crate alloc is included during no std feature. It also provides a build_fn field validation_error that will disable the generation of *BuilderError::ValidationError(String) so that is never referenced. https://github.com/colin-kiegel/rust-derive-builder/pull/302

TedDriggs commented 10 months ago

You should be able to avoid the usage of String here by providing a custom error yourself - did that still result in generated code that used String?

red1bluelost commented 10 months ago

You should be able to avoid the usage of String here by providing a custom error yourself - did that still result in generated code that used String?

I did try custom errors and it is a good way to not import any alloc code but it does not prevent linking alloc. Since extern crate alloc still exists in 'derive_builder/src/lib.rs', it will try to link with a global_allocator which is mainly what I want to avoid.

TedDriggs commented 10 months ago

Thanks for the explanation! I am onboard with making alloc optional based on that, and will reexamine your PR with this in mind.

TedDriggs commented 9 months ago

Looks like this didn't get auto-closed by the relevant commit; the changes are in v0.13.0