[][src]Crate shorthand

shorthand

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

#[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,
}

Customization

The derive macro can be heavily customized with the #[shorthand] attribute. It has the following main attributes:

enable

This attribute allows you to enable certain attributes. For example by default the attribute into is disabled.

use shorthand::ShortHand;

#[derive(ShortHand, Default)]
struct Example {
    #[shorthand(enable(into))]
    field: String,
    other: String,
}

let mut example = Example::default();

example.set_field("field"); // accepts any type, that implements Into<String> or From<String>
example.set_other("other".to_string());

assert_eq!(example.field(), &"field".to_string());
assert_eq!(example.other(), &"other".to_string());

You can find a list with all attributes, that can be enabled here.

disable

This attribute allows you to disable certain attributes. For example by default the attribute primitive_copy is enabled

use shorthand::ShortHand;

#[derive(ShortHand, Default)]
struct Example {
    #[shorthand(disable(primitive_copy))]
    field: usize,
    other: usize,
}

let example = Example::default();

assert_eq!(example.field(), &0); // returns a reference, instead of copying the value
assert_eq!(example.other(), 0);

You can find a list with all attributes, that can be disabled here.

visibility

This attribute allows you to change the visibility of the derived function. Anything from here is valid. You can also set the visibility to inherit, all derived functions will then have the visibility of the struct.

You can either apply this as a local or as a global attribute. The default visibility is pub.

use shorthand::ShortHand;

#[derive(ShortHand, Default)]
#[shorthand(visibility("pub(crate)"))]
struct Example {
    field: usize,
    #[shorthand(visibility("pub"))]
    data: String,
    #[shorthand(visibility(inherit))]
    xt: String,
}

rename

This attribute allows you to rename the derived function, with a pattern. You can either apply this as a local or as a global attribute.

use shorthand::ShortHand;

#[derive(ShortHand, Default)]
#[shorthand(rename("prefix_{}_suffix"))]
struct Example {
    field: usize,
    #[shorthand(rename(format = "example_{}"))]
    data: String,
}

let mut example = Example::default();
example.set_prefix_field_suffix(1);
example.set_example_data("Hello".to_string());

assert_eq!(example.prefix_field_suffix(), 1);
assert_eq!(example.example_data(), &"Hello".to_string());

This attribute also supports changing the getter and setter individually:

use shorthand::ShortHand;

#[derive(ShortHand, Default)]
#[shorthand(rename(format = "prefix_{}_suffix"))]
struct Example {
    #[shorthand(rename(get = "get_{}", set = "set_{}_a"))]
    field: usize,
    #[shorthand(rename(get = "get_{}"))] // this will not change the setter
    data: String,
}

let mut example = Example::default();
example.set_field_a(1);
example.set_data("Hello".to_string());

assert_eq!(example.get_field(), 1);
assert_eq!(example.get_data(), &"Hello".to_string());

In the case, that you have a rename attribute on the entire struct, but you do not want to apply it for one specific field, you can disable it.

use shorthand::ShortHand;

#[derive(ShortHand, Default)]
#[shorthand(rename("prefix_{}_suffix"))]
struct Example {
    #[shorthand(disable(rename))]
    field: usize,
    data: String,
}

let mut example = Example::default();
example.set_field(1);
example.set_prefix_data_suffix("Hello".to_string());

assert_eq!(example.field(), 1);
assert_eq!(example.prefix_data_suffix(), &"Hello".to_string());

It is also possible to rename single fields

use shorthand::ShortHand;

#[derive(ShortHand, Default)]
struct Example {
    #[shorthand(rename("is_default"))]
    default: bool,
}

assert_eq!(Example::default().is_default(), false);

List of Attributes

Enabled by default

The following attributes are enabled by default

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

Derive Macros

ShortHand

A proc_macro to derive getter, mutgetter and setter for fields.