1 unstable release

0.1.0 Jan 26, 2020

#9 in #shorthand

Download history 713/week @ 2024-07-19 449/week @ 2024-07-26 520/week @ 2024-08-02 735/week @ 2024-08-09 519/week @ 2024-08-16 452/week @ 2024-08-23 471/week @ 2024-08-30 586/week @ 2024-09-06 403/week @ 2024-09-13 435/week @ 2024-09-20 522/week @ 2024-09-27 394/week @ 2024-10-04 424/week @ 2024-10-11 529/week @ 2024-10-18 393/week @ 2024-10-25 514/week @ 2024-11-01

1,912 downloads per month
Used in 7 crates (via shorthand)

Unlicense OR MIT OR Apache-2.0

5KB
72 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 is based on https://cprimozic.net/blog/writing-a-hashmap-to-struct-procedural-macro-in-rust/

There was sadly no crate available, so I had to make my own :(

I made some improvements to the code and ported it to a newer version of syn. For example the FromMap trait doesn't need a type parameter and the HashMap should contain a static str (field names should be known at compile time).

Dependencies

~1.5MB
~37K SLoC