| Crates.io | gset |
| lib.rs | gset |
| version | 1.1.0 |
| created_at | 2023-01-04 10:57:03.355463+00 |
| updated_at | 2024-07-23 15:50:41.063408+00 |
| description | A procedural macro for generating the most basic getters and setters on fields. |
| homepage | https://github.com/andrewsonin/gset |
| repository | https://github.com/andrewsonin/gset |
| max_upload_size | |
| id | 750772 |
| size | 35,960 |
Getters and Setters for Rust.
Provides a procedural macro capable of deriving basic getters and setters for structs.
An example of using this library is provided below.
use gset::Getset;
#[derive(Getset)]
struct Struct<T>
{
/// Field 1.
#[getset(get_copy, name = "get_field_1", vis = "pub")]
#[getset(set)]
field_1: f64,
/// Field 2.
#[getset(get_deref, vis = "pub")]
#[getset(get_deref_mut, vis = "pub")]
#[getset(set, vis = "pub")]
field_2: Vec<T>,
}
This also works well for tuple structures,
but the name parameter becomes mandatory.
use gset::Getset;
#[derive(Getset)]
struct Struct<T>(
/// Field 1.
#[getset(get_copy, name = "get_field_1", vis = "pub")]
#[getset(set, name = "set_field_1")]
f64,
/// Field 2.
#[getset(get_deref, name = "get_field_2", vis = "pub")]
#[getset(get_deref_mut, name = "get_field_2_mut", vis = "pub")]
#[getset(set, name = "set_field_2", vis = "pub")]
Vec<T>,
);
All field attributes have the following named parameters:
name — name of the method being inferred.
Must be a valid Rust identifier.
This is a required parameter for tuple structs.vis — visibility of the method being inferred.
Must be a valid Rust visibility modifier.
Visibility is private by default.And some of them have the following named parameter:
ty — return type of the method being inferred. Must be a valid Rust type.Here and further we will adhere to the following notation.
field — field name.T — field type.The field attributes currently supported are listed below.
getDerives a reference getter for a field.
name — name of the resulting method. If not set, it will be named as field.vis — visibility of the resulting method. If not set, it will be private.ty — return type of the resulting method. If not set, it will have the &T return type.use gset::Getset;
#[derive(Getset)]
struct Struct {
/// Doc comment.
#[getset(get, vis = "pub")]
a: f64,
}
will expand into
struct Struct {
/// Doc comment.
a: f64,
}
impl Struct {
/// Doc comment.
#[inline]
pub fn a(&self) -> &f64 {
&self.a
}
}
get_mutDerives a mutable getter for a field.
name — name of the resulting method. If not set, it will be named as field_mut.vis — visibility of the resulting method. If not set, it will be private.ty — return type of the resulting method. If not set, it will have the &mut T return type.use gset::Getset;
#[derive(Getset)]
struct Struct {
/// Doc comment.
#[getset(get_mut, vis = "pub")]
a: f64,
}
will expand into
struct Struct {
/// Doc comment.
a: f64,
}
impl Struct {
/// Doc comment.
#[inline]
pub fn a_mut(&mut self) -> &mut f64 {
&mut self.a
}
}
get_copyDerives a copy getter for a field.
name — name of the resulting method. If not set, it will be named as field.vis — visibility of the resulting method. If not set, it will be private.ty — return type of the resulting method. If not set, it will have the T return type.use gset::Getset;
#[derive(Getset)]
struct Struct {
/// Doc comment.
#[getset(get_copy, vis = "pub")]
a: f64,
}
will expand into
struct Struct {
/// Doc comment.
a: f64,
}
impl Struct {
/// Doc comment.
#[inline]
pub fn a(&self) -> f64 {
self.a
}
}
get_derefDerives a reference getter for a field, which applies the deref operation to the resulting reference.
name — name of the resulting method. If not set, it will be named as field.vis — visibility of the resulting method. If not set, it will be private.ty — return type of the resulting method. If not set, it will have the &<T as ::std::ops:Deref>::Target return type.use gset::Getset;
#[derive(Getset)]
struct Struct {
/// Doc comment.
#[getset(get_deref, vis = "pub")]
a: Vec<f64>,
}
will expand into
struct Struct {
/// Doc comment.
a: Vec<f64>,
}
impl Struct {
/// Doc comment.
#[inline]
pub fn a(&self) -> &[f64] {
&self.a
}
}
get_deref_mutDerives a mutable getter for a field, which applies the deref_mut operation to the resulting reference.
name — name of the resulting method. If not set, it will be named as field_mut.vis — visibility of the resulting method. If not set, it will be private.ty — return type of the resulting method. If not set, it will have the &mut <T as ::std::ops:Deref>::Target return type.use gset::Getset;
#[derive(Getset)]
struct Struct {
/// Doc comment.
#[getset(get_deref_mut, vis = "pub")]
a: Vec<f64>,
}
will expand into
struct Struct {
/// Doc comment.
a: Vec<f64>,
}
impl Struct {
/// Doc comment.
#[inline]
pub fn a_mut(&mut self) -> &mut [f64] {
&mut self.a
}
}
get_deref_copyDerives a copy getter for a field, which applies dereferencing to the field value.
name — name of the resulting method. If not set, it will be named as field.vis — visibility of the resulting method. If not set, it will be private.ty — return type of the resulting method. If not set, it will have the <T as ::std::ops:Deref>::Target return type.use derive_more::Deref;
use gset::Getset;
#[derive(Getset)]
struct Struct {
/// Doc comment.
#[getset(get_deref_copy, vis = "pub")]
a: F64,
}
#[derive(Deref)]
struct F64(f64);
will expand into
use derive_more::Deref;
struct Struct {
/// Doc comment.
a: F64,
}
#[derive(Deref)]
struct F64(f64);
impl Struct {
/// Doc comment.
#[inline]
pub fn a(&self) -> f64 {
*self.a
}
}
get_as_refDerives a reference getter for a field, which applies the as_ref operation to the resulting reference.
name — name of the resulting method. If not set, it will be named as field.vis — visibility of the resulting method. If not set, it will be private.ty — return type of the resulting method. Required parameter.use gset::Getset;
#[derive(Getset)]
struct Struct {
/// Doc comment.
#[getset(get_as_ref, vis = "pub", ty = "Option<&f64>")]
a: Option<f64>,
}
will expand into
struct Struct {
/// Doc comment.
a: Option<f64>,
}
impl Struct {
/// Doc comment.
#[inline]
pub fn a(&self) -> Option<&f64> {
self.a.as_ref()
}
}
get_as_derefDerives a reference getter for a field, which applies the as_deref operation to the resulting reference.
name — name of the resulting method. If not set, it will be named as field.vis — visibility of the resulting method. If not set, it will be private.ty — return type of the resulting method. Required parameter.use derive_more::Deref;
use gset::Getset;
#[derive(Getset)]
struct Struct {
/// Doc comment.
#[getset(get_as_deref, vis = "pub", ty = "Option<&f64>")]
a: Option<F64>,
}
#[derive(Deref)]
struct F64(f64);
will expand into
use derive_more::Deref;
struct Struct {
/// Doc comment.
a: Option<F64>,
}
#[derive(Deref)]
struct F64(f64);
impl Struct {
/// Doc comment.
#[inline]
pub fn a(&self) -> Option<&f64> {
self.a.as_deref()
}
}
get_as_deref_mutDerives a mutable getter for a field, which applies the as_deref_mut operation to the resulting reference.
name — name of the resulting method. If not set, it will be named as field_mut.vis — visibility of the resulting method. If not set, it will be private.ty — return type of the resulting method. Required parameter.use derive_more::{Deref, DerefMut};
use gset::Getset;
#[derive(Getset)]
struct Struct {
/// Doc comment.
#[getset(get_as_deref_mut, vis = "pub", ty = "Option<&mut f64>")]
a: Option<F64>,
}
#[derive(Deref, DerefMut)]
struct F64(f64);
will expand into
use derive_more::{Deref, DerefMut};
struct Struct {
/// Doc comment.
a: Option<F64>,
}
#[derive(Deref, DerefMut)]
struct F64(f64);
impl Struct {
/// Doc comment.
#[inline]
pub fn a_mut(&mut self) -> Option<&mut f64> {
self.a.as_deref_mut()
}
}
setDerives a setter for a field.
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.use gset::Getset;
#[derive(Getset)]
struct Struct {
/// Doc comment.
#[getset(set, vis = "pub")]
a: f64,
}
will expand into
struct Struct {
/// Doc comment.
a: f64,
}
impl Struct {
/// Doc comment.
#[inline]
pub fn set_a(&mut self, value: f64) {
self.a = value
}
}
set_borrowDerives a borrowing setter for a field.
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.use gset::Getset;
#[derive(Getset)]
struct Struct {
/// Doc comment.
#[getset(set_borrow, vis = "pub")]
a: f64,
}
will expand into
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
}
}
set_ownDerives an owning setter for a field.
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.use gset::Getset;
#[derive(Getset)]
struct Struct {
/// Doc comment.
#[getset(set_own, vis = "pub")]
a: f64,
}
will expand into
struct Struct {
/// Doc comment.
a: f64,
}
impl Struct {
/// Doc comment.
#[inline]
pub fn set_a(mut self, value: f64) -> Self {
self.a = value;
self
}
}