Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
269 changes: 151 additions & 118 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,127 +52,127 @@ fn main() {
You can use `cargo-expand` to generate the output. Here are the functions that the above generates (Replicate with `cargo expand --example simple`):

```rust
use std::sync::Arc;
use getset::{CloneGetters, CopyGetters, Getters, MutGetters, Setters, WithSetters};
pub struct Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[getset(get, set, get_mut, set_with)]
private: T,
/// Doc comments are supported!
/// Multiline, even.
#[getset(get_copy = "pub", set = "pub", get_mut = "pub", set_with = "pub")]
public: T,
/// Arc supported through CloneGetters
#[getset(get_clone = "pub", set = "pub", get_mut = "pub", set_with = "pub")]
arc: Arc<u16>,
}
impl<T> Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
fn private(&self) -> &T {
&self.private
}
}
impl<T> Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
fn set_private(&mut self, val: T) -> &mut Self {
use std::sync::Arc;
use getset::{CloneGetters, CopyGetters, Getters, MutGetters, Setters, WithSetters};
pub struct Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[getset(get, set, get_mut, set_with)]
private: T,
/// Doc comments are supported!
/// Multiline, even.
#[getset(get_copy = "pub", set = "pub", get_mut = "pub", set_with = "pub")]
public: T,
/// Arc supported through CloneGetters
#[getset(get_clone = "pub", set = "pub", get_mut = "pub", set_with = "pub")]
arc: Arc<u16>,
}
impl<T> Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
fn private(&self) -> &T {
&self.private
}
}
impl<T> Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
fn set_private(&mut self, val: T) -> &mut Self {
self.private = val;
self
}
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
pub fn set_public(&mut self, val: T) -> &mut Self {
self.public = val;
self
}
/// Arc supported through CloneGetters
#[inline(always)]
self
}
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
pub fn set_public(&mut self, val: T) -> &mut Self {
self.public = val;
self
}
/// Arc supported through CloneGetters
#[inline(always)]
pub fn set_arc(&mut self, val: Arc<u16>) -> &mut Self {
self.arc = val;
self
}
}
impl<T> Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
fn with_private(mut self, val: T) -> Self {
self.private = val;
self
}
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
pub fn with_public(mut self, val: T) -> Self {
self.public = val;
self
}
/// Arc supported through CloneGetters
#[inline(always)]
pub fn with_arc(mut self, val: Arc<u16>) -> Self {
self.arc = val;
self
}
}
impl<T> Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
self.arc = val;
self
}
}
impl<T> Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
fn with_private(mut self, val: T) -> Self {
self.private = val;
self
}
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
pub fn with_public(mut self, val: T) -> Self {
self.public = val;
self
}
/// Arc supported through CloneGetters
#[inline(always)]
pub fn with_arc(mut self, val: Arc<u16>) -> Self {
self.arc = val;
self
}
}
impl<T> Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
fn private_mut(&mut self) -> &mut T {
&mut self.private
}
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
pub fn public_mut(&mut self) -> &mut T {
&mut self.public
}
/// Arc supported through CloneGetters
#[inline(always)]
pub fn arc_mut(&mut self) -> &mut Arc<u16> {
&mut self.arc
}
}
impl<T> Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
pub fn public(&self) -> T {
self.public
}
}
impl<T> Foo<T>
where
T: Copy + Clone + Default,
{
/// Arc supported through CloneGetters
#[inline(always)]
pub fn arc(&self) -> Arc<u16> {
self.arc.clone()
}
&mut self.private
}
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
pub fn public_mut(&mut self) -> &mut T {
&mut self.public
}
/// Arc supported through CloneGetters
#[inline(always)]
pub fn arc_mut(&mut self) -> &mut Arc<u16> {
&mut self.arc
}
}
impl<T> Foo<T>
where
T: Copy + Clone + Default,
{
/// Doc comments are supported!
/// Multiline, even.
#[inline(always)]
pub fn public(&self) -> T {
self.public
}
}
impl<T> Foo<T>
where
T: Copy + Clone + Default,
{
/// Arc supported through CloneGetters
#[inline(always)]
pub fn arc(&self) -> Arc<u16> {
self.arc.clone()
}
}
```

Expand Down Expand Up @@ -257,3 +257,36 @@ impl Foo {
}
}
```

By default, a field of type `Option<T>` or `Result<T, E>` generates a getter
returning `&Option<T>` or `&Result<T, E>`. To get an `Option<&T>` or
`Result<&T, &E>` instead, opt in with the `as_ref` / `as_mut` flags:

```rust
use getset::{Getters, MutGetters};

#[derive(Getters, MutGetters)]
struct MyStruct<T, E> {
/// Returns `Option<&T>` instead of `&Option<T>`
#[getset(get = "as_ref", get_mut = "as_mut")]
maybe: Option<T>,

/// Returns `Result<&T, &E>` instead of `&Result<T, E>`
#[getset(get = "as_ref", get_mut = "as_mut")]
parsed: Result<T, E>,
}

let mut s = MyStruct {
maybe: Some(42),
parsed: Err("oops"),
};

assert_eq!(s.maybe(), Some(&42));
assert_eq!(s.maybe_mut(), Some(&mut 42));

assert_eq!(s.parsed(), Err(&"oops"));
assert_eq!(s.parsed_mut(), Err(&mut "oops"));
```

- `get = "as_ref"` makes the generated getter call `.as_ref()`.
- `get_mut = "as_mut"` makes the generated mutable getter call `.as_mut()`.
Loading
Loading