Skip to content

Commit

Permalink
Add docs for skipping fields
Browse files Browse the repository at this point in the history
  • Loading branch information
pnevyk committed Jul 25, 2021
1 parent 4ded087 commit f0cd4d9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,35 @@ fn main() {
let val = foo.get_field();
}
```

Skipping setters and getters generation for a field when struct level attribute is used
is possible with `#[getset(skip)]`.

```rust
use getset::{CopyGetters, Setters};

#[derive(CopyGetters, Setters)]
#[getset(get_copy, set)]
pub struct Foo {
// If the field was not skipped, the compiler would complain about moving
// a non-copyable type in copy getter.
#[getset(skip)]
skipped: String,

field1: usize,
field2: usize,
}

impl Foo {
// It is possible to write getters and setters manually,
// possibly with a custom logic.
fn skipped(&self) -> &str {
&self.skipped
}

fn set_skipped(&mut self, val: &str) -> &mut Self {
self.skipped = val.to_string();
self
}
}
```
34 changes: 33 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ foo.public();
```
For some purposes, it's useful to have the `get_` prefix on the getters for
either legacy of compatability reasons. It is done with `with_prefix`.
either legacy of compatibility reasons. It is done with `with_prefix`.
```rust
use getset::{Getters, MutGetters, CopyGetters, Setters};
Expand All @@ -141,6 +141,38 @@ pub struct Foo {
let mut foo = Foo::default();
let val = foo.get_field();
```
Skipping setters and getters generation for a field when struct level attribute is used
is possible with `#[getset(skip)]`.
```rust
use getset::{CopyGetters, Setters};
#[derive(CopyGetters, Setters)]
#[getset(get_copy, set)]
pub struct Foo {
// If the field was not skipped, the compiler would complain about moving
// a non-copyable type in copy getter.
#[getset(skip)]
skipped: String,
field1: usize,
field2: usize,
}
impl Foo {
// It is possible to write getters and setters manually,
// possibly with a custom logic.
fn skipped(&self) -> &str {
&self.skipped
}
fn set_skipped(&mut self, val: &str) -> &mut Self {
self.skipped = val.to_string();
self
}
}
```
*/

extern crate proc_macro;
Expand Down

0 comments on commit f0cd4d9

Please sign in to comment.