diff --git a/Cargo.toml b/Cargo.toml index 1a4e1e6..281c113 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gset" -version = "1.0.5" +version = "1.1.0" authors = ["Andrew Sonin "] description = "A procedural macro for generating the most basic getters and setters on fields." categories = ["development-tools::procedural-macro-helpers"] diff --git a/README.md b/README.md index 7255d14..9cfd07f 100644 --- a/README.md +++ b/README.md @@ -67,8 +67,8 @@ struct Struct( All field attributes have the following named parameters: - `name` — name of the method being inferred. -Must be a valid Rust [identifier](https://docs.rs/syn/1.0.109/syn/struct.Ident.html). -This is a required parameter for tuple structs. + Must be a valid Rust [identifier](https://docs.rs/syn/1.0.109/syn/struct.Ident.html). + This is a required parameter for tuple structs. - `vis` — visibility of the method being inferred. Must be a valid Rust [visibility modifier](https://docs.rs/syn/1.0.109/syn/enum.Visibility.html). Visibility is `private` by default. @@ -477,4 +477,78 @@ impl Struct { self.a = value } } +``` + +### 11. `set_borrow` + +Derives a borrowing setter for a field. + +#### Parameters +- `name` — name of the resulting method. If not set, it will be named as `set_field`. +- `vis` — visibility of the resulting method. If not set, it will be private. + +#### Example + +```rust +use gset::Getset; + +#[derive(Getset)] +struct Struct { + /// Doc comment. + #[getset(set_borrow, vis = "pub")] + a: f64, +} +``` +will expand into +```rust +struct Struct { + /// Doc comment. + a: f64, +} + +impl Struct { + /// Doc comment. + #[inline] + pub fn set_a(&mut self, value: f64) -> &mut Self { + self.a = value; + self + } +} +``` + +### 12. `set_own` + +Derives an owning setter for a field. + +#### Parameters +- `name` — name of the resulting method. If not set, it will be named as `set_field`. +- `vis` — visibility of the resulting method. If not set, it will be private. + +#### Example + +```rust +use gset::Getset; + +#[derive(Getset)] +struct Struct { + /// Doc comment. + #[getset(set_own, vis = "pub")] + a: f64, +} +``` +will expand into +```rust +struct Struct { + /// Doc comment. + a: f64, +} + +impl Struct { + /// Doc comment. + #[inline] + pub fn set_a(mut self, value: f64) -> Self { + self.a = value; + self + } +} ``` \ No newline at end of file diff --git a/src/field_attribute_layout.rs b/src/field_attribute_layout.rs index c61261d..e1741b5 100644 --- a/src/field_attribute_layout.rs +++ b/src/field_attribute_layout.rs @@ -229,6 +229,22 @@ impl AttributeLayout { get_ty_override().unwrap_or_else(|| quote! { () }), ) } + AttributeKind::SetBorrowed => { + let fn_name = setter_fn_name(); + ( + quote! { #fn_name(&mut self, value: #field_type) }, + quote! { self.#field_ident_or_idx = value; self }, + get_ty_override().unwrap_or_else(|| quote! { &mut Self }), + ) + } + AttributeKind::SetOwned => { + let fn_name = setter_fn_name(); + ( + quote! { #fn_name(mut self, value: #field_type) }, + quote! { self.#field_ident_or_idx = value; self }, + get_ty_override().unwrap_or_else(|| quote! { Self }), + ) + } }; quote! { #visibility fn #signature -> #ty { diff --git a/src/field_attributes.rs b/src/field_attributes.rs index 9205dd8..38e91ce 100644 --- a/src/field_attributes.rs +++ b/src/field_attributes.rs @@ -68,6 +68,8 @@ define_field_attributes!( GetAsRef => "get_as_ref", GetAsDeref => "get_as_deref", GetAsDerefMut => "get_as_deref_mut", - Set => "set" + Set => "set", + SetBorrowed => "set_borrow", + SetOwned => "set_own" } ); diff --git a/tests/getset.rs b/tests/getset.rs index 38fdf1a..b79511f 100644 --- a/tests/getset.rs +++ b/tests/getset.rs @@ -8,7 +8,7 @@ use std::{ }; #[derive(Getset)] -struct Struct1 +struct Struct1<'a, T, M: Default> where T: Debug, { @@ -43,6 +43,12 @@ where ty = "Option<&mut f64>" )] field_5: Option, + + /// Field 6. + #[getset(set, name = "set_field_6")] + #[getset(set_borrow, name = "set_field_6_borrow")] + #[getset(set_own, name = "set_field_6_own")] + field_6: &'a f64, } #[derive(Getset)]