#traits #struct #getter-setter #mut #from-map

macro hashmap_derive

A proc_macro to derive the FromMap trait

1 unstable release

0.1.0 Jan 26, 2020

#31 in #getter-setter

Download history 234/week @ 2023-11-27 203/week @ 2023-12-04 116/week @ 2023-12-11 151/week @ 2023-12-18 81/week @ 2023-12-25 32/week @ 2024-01-01 95/week @ 2024-01-08 178/week @ 2024-01-15 237/week @ 2024-01-22 95/week @ 2024-01-29 105/week @ 2024-02-05 308/week @ 2024-02-12 138/week @ 2024-02-19 435/week @ 2024-02-26 170/week @ 2024-03-04 272/week @ 2024-03-11

1,049 downloads per month
Used in 6 crates (via from_map)

Unlicense OR MIT OR Apache-2.0

5KB
81 lines

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"

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


lib.rs:

This crate only exists, because it is not possible to export a trait and a proc_macro in the same crate.

You should instead use the from_map crate.

Dependencies

~1.5MB
~33K SLoC