| 1 | /// Controls the signature of a setter method, |
| 2 | /// more specifically how `self` is passed and returned. |
| 3 | /// |
| 4 | /// It can also be generalized to methods with different parameter sets and |
| 5 | /// return types, e.g. the `build()` method. |
| 6 | #[derive (PartialEq, Eq, Debug, Clone, Copy, FromMeta)] |
| 7 | pub enum BuilderPattern { |
| 8 | /// E.g. `fn bar(self, bar: Bar) -> Self`. |
| 9 | Owned, |
| 10 | /// E.g. `fn bar(&mut self, bar: Bar) -> &mut Self`. |
| 11 | Mutable, |
| 12 | /// E.g. `fn bar(&self, bar: Bar) -> Self`. |
| 13 | /// |
| 14 | /// Note: |
| 15 | /// - Needs to `clone` in order to return an _updated_ instance of `Self`. |
| 16 | /// - There is a great chance that the Rust compiler (LLVM) will |
| 17 | /// optimize chained `clone` calls away in release mode. |
| 18 | /// Therefore this turns out not to be as bad as it sounds. |
| 19 | Immutable, |
| 20 | } |
| 21 | |
| 22 | impl BuilderPattern { |
| 23 | /// Returns true if this style of builder needs to be able to clone its |
| 24 | /// fields during the `build` method. |
| 25 | pub fn requires_clone(&self) -> bool { |
| 26 | *self != Self::Owned |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /// Defaults to `Mutable`. |
| 31 | impl Default for BuilderPattern { |
| 32 | fn default() -> Self { |
| 33 | Self::Mutable |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | #[derive (Debug, Clone, FromMeta)] |
| 38 | pub struct Each { |
| 39 | pub name: syn::Ident, |
| 40 | #[darling(default)] |
| 41 | pub into: bool, |
| 42 | } |
| 43 | |
| 44 | impl From<syn::Ident> for Each { |
| 45 | fn from(name: syn::Ident) -> Self { |
| 46 | Self { name, into: false } |
| 47 | } |
| 48 | } |
| 49 | |