Skip to content

Commit

Permalink
Document custom setter (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijackson committed Mar 14, 2022
1 parent cb8e497 commit 97e5c71
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
36 changes: 32 additions & 4 deletions derive_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
//!
//! ## Hidden Fields
//!
//! You can hide fields by skipping their setters on the builder struct.
//! You can hide fields by skipping their setters on (and presence in) the builder struct.
//!
//! - Opt-out — skip setters via `#[builder(setter(skip))]` on individual fields.
//! - Opt-in — set `#[builder(setter(skip))]` on the whole struct
Expand All @@ -165,7 +165,7 @@
//! # extern crate derive_builder;
//! #
//! #[derive(Builder)]
//! struct SetterOptOut {
//! struct HiddenField {
//! setter_present: u32,
//! #[builder(setter(skip))]
//! setter_skipped: u32,
Expand All @@ -175,8 +175,36 @@
//!
//! Alternatively, you can use the more verbose form:
//!
//! - `#[builder(setter(skip = "true"))]`
//! - `#[builder(setter(skip = "false"))]`
//! - `#[builder(setter(skip = true))]`
//! - `#[builder(setter(skip = false))]`
//!
//! ## Custom setters (skip autogenerated setters)
//!
//! Similarly to `setter(skip)`, you can say that you will provide your own setter methods.
//! This simply suppresses the generation of the setter, leaveing the field in the builder,
//! as `Option<T>`.
//!
//! ```rust
//! # #[macro_use]
//! # extern crate derive_builder;
//! #
//! #[derive(Builder)]
//! struct SetterOptOut {
//! #[builder(setter(custom))]
//! custom_setter: u32,
//! }
//! impl SetterOptOutBuilder {
//! fn custom_setter(&mut self, value: u32) {
//! self.custom_setter = Some(value);
//! }
//! }
//! # fn main() {}
//! ```
//!
//! Again, the more verbose form is accepted:
//!
//! - `#[builder(setter(custom = true))]`
//! - `#[builder(setter(custom = false))]`
//!
//! ## Setter Visibility
//!
Expand Down
2 changes: 1 addition & 1 deletion derive_builder/tests/skip-setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct SetterOptOut {
#[builder(setter(skip))]
struct SetterOptIn {
setter_skipped_by_shorthand_default: u32,
#[builder(setter(skip = "false"))]
#[builder(setter(skip = false))] // Should be still OK without quotes
setter_present_by_explicit_opt_in: u32,
#[builder(setter)]
setter_present_by_shorthand_opt_in: u32,
Expand Down

0 comments on commit 97e5c71

Please sign in to comment.