Skip to content

Commit

Permalink
feat(getset-kinds): add set_borrow and set_own
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsonin committed Jul 23, 2024
1 parent 22845c9 commit 0d39138
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gset"
version = "1.0.5"
version = "1.1.0"
authors = ["Andrew Sonin <sonin.cel@yandex.ru>"]
description = "A procedural macro for generating the most basic getters and setters on fields."
categories = ["development-tools::procedural-macro-helpers"]
Expand Down
78 changes: 76 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ struct Struct<T>(

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.
Expand Down Expand Up @@ -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
}
}
```
16 changes: 16 additions & 0 deletions src/field_attribute_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion src/field_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
);
8 changes: 7 additions & 1 deletion tests/getset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
};

#[derive(Getset)]
struct Struct1<T, M: Default>
struct Struct1<'a, T, M: Default>
where
T: Debug,
{
Expand Down Expand Up @@ -43,6 +43,12 @@ where
ty = "Option<&mut f64>"
)]
field_5: Option<F64>,

/// 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)]
Expand Down

0 comments on commit 0d39138

Please sign in to comment.