#getter-setter #getter #setter

macro shorthand

A proc_macro to derive getter and setter for structs

2 releases

0.1.1 Feb 1, 2020
0.1.0 Jan 26, 2020

#21 in #setter

Download history 148/week @ 2023-12-20 26/week @ 2023-12-27 59/week @ 2024-01-03 90/week @ 2024-01-10 170/week @ 2024-01-17 223/week @ 2024-01-24 84/week @ 2024-01-31 142/week @ 2024-02-07 309/week @ 2024-02-14 251/week @ 2024-02-21 323/week @ 2024-02-28 203/week @ 2024-03-06 475/week @ 2024-03-13 219/week @ 2024-03-20 208/week @ 2024-03-27 198/week @ 2024-04-03

1,125 downloads per month
Used in 4 crates (2 directly)

MIT/Apache

110KB
2K SLoC

shorthand

Crates.io: shorthand Documentation Build Status

shorthand is defined as a system of fast writing and that is exactly what this library is for; to remove the annoying boilerplate code, that comes with writing your own library.

What does this library do?

It makes coding in rust a lot more convenient, by deriving getters and setters for the fields of a struct.

use shorthand::ShortHand;

#[derive(ShortHand, Default)]
pub struct Example {
    number: usize,
    data: String,
}

let mut example = Example::default();

assert_eq!(example.number(), 0);
example.set_number(1);
assert_eq!(example.number(), 1);

assert_eq!(example.data(), &"".to_string());
example.set_data("hi".to_string());
assert_eq!(example.data(), &"hi".to_string());

Otherwise, you would have to write the this by hand

pub struct Example {
    number: usize,
    data: String,
}

#[allow(dead_code)]
impl Example {
    #[inline(always)]
    pub fn number(&self) -> usize { self.number }

    #[inline(always)]
    pub fn set_number(&mut self, value: usize) -> &mut Self {
        self.number = value;
        self
    }

    #[inline(always)]
    pub fn data(&self) -> &String { &self.data }

    #[inline(always)]
    pub fn set_data(&mut self, value: String) -> &mut Self {
        self.data = value;
        self
    }
}

How do I get started?

Simply add this library under [dependencies] to your Cargo.toml

[dependencies]
shorthand = "0.1.0"

You can then derive ShortHand for any struct

use shorthand::ShortHand;

#[derive(ShortHand)]
struct Example {
    field: usize,
}

You can find the documentation here.

Feature Requests and Bug Reports

Feel free to ask questions or report bugs here. There are no stupid questions.

This library should be as convenient as possible, so please do not hesitate to request a feature.

Reference

This library has been inspired by the following crates

Dependencies

~1.5MB
~32K SLoC