Rust derive macros for automating the boring stuff.

Overview

derived: Macros for automating the boring stuff

GitHub Workflow Status Crates.io docs.rs GitHub

The derived crate provides macros that can simplify all the boring stuff, like writing constructors for example. With this crate, you can create compile-time, constant default implementations, constructors, getters and setters with full generics and lifetime support.

Features

  • Ctor: To generate constructors
  • Gtor: To generate getters
  • Stor: To generate setters
  • Constdef: To generate constant, compile-time default implementations.

    🎉 Arrays, tuples, nested tuples in arrays and nested arrays in tuples included!

  • 💯 Full lifetimes, generics and where clause support
  • 🤓 Advanced features:
    • Use the gtor attribute to get either immutable or mutable or both references (see example below)
    • Skip generation of setters or getters with the #[stor_skip] or #[gtor_skip] attributes for specific fields
    • Make ctors and gtors const with the #[ctor_const] and #[gtor_const] attributes
    • Skip ctors, gtors and stors for PhantomData fields with the #[phantom] attribute

Example: Constant default implementations

use derived::Constdef;

#[derive(Constdef)]
struct MyConst {
    a: u8,
    b: bool,
    c: char,
}

const MYCONST: MyConst = MyConst::default();
// use it with other consts
const ZERO_U8: u8 = MYCONST.a;

assert_eq!(MYCONST.a, 0);
assert_eq!(MYCONST.b, false);
assert_eq!(MYCONST.c, '\0');

Example: Generating constructors

use derived::Ctor;

#[derive(Ctor)]
pub struct MyStruct {
    a: u8,
    b: i8,
}

let mystruct = MyStruct::new(1, -1);
assert_eq!(mystruct.a, 1);
assert_eq!(mystruct,b, -1);

Example: Generating getters

use derived::{Ctor, Gtor};

// we'll derive `Ctor` to avoid having to write ctors
#[derive(Ctor, Gtor)]
pub struct MyStruct {
    name: String,
    userid: u64,
}

let ms = MyStruct::new("Sayan".to_owned(), 1);
assert_eq!(ms.get_name(), "sayan");
// we don't need to deref because u64 is a copy type
assert_eq!(ms.get_userid(), 1);

Example: Generating setters

use derived::{Ctor, Stor};

// we'll derive `Ctor` to avoid having to write ctors
#[derive(Ctor, Stor)]
pub struct MyStruct {
    name: String,
    userid: u64,
}

let mut ms = MyStruct::new("Sayan".to_owned(), 1);
assert_eq!(ms.get_name(), "sayan");
// we don't need to deref because u64 is a copy type
assert_eq!(ms.get_userid(), 1);
ms.set_userid(0);
assert_eq!(ms.get_userid(), 0);

Example: Adanced generics and lifetimes in structs

use derived::{Ctor, Gtor};

#[derive(Ctor, Gtor)]
struct MyTag<'a, T: ToString> {
    val: &'a T,
    tag: u8,
}

let mut x = MyTag::new(&10i32, 20); // this will have type MyTag<i32>
// you can now use getters and setters as you please!
assert_eq!(x.get_val().to_string(), "10");
x.set_val(11);
assert_eq!(x.get_val().to_string(), "11");

Example: get_mut and get

use derived::{Ctor, Gtor};

#[derive(Ctor, Gtor)]
#[gtor(get, get_mut)]
pub struct Mutable {
    x_axis: u8,
    y_axis: u8,
}

#[test]
fn test_get_and_get_mut() {
    let mut m = Mutable::new(0, 0);
    // move x by 1 unit
    *m.get_x_axis_mut() = 1;
    // move y by 2 units
    *m.get_y_axis_mut() = 2;
    assert_eq!(m.get_x_axis(), 1);
    assert_eq!(m.get_y_axis(), 2);
}

License

This crate is distributed under the Apache-2.0 License.

You might also like...
Experimental Rust tool for generating FFI definitions allowing many other languages to call Rust code

Diplomat is an experimental Rust tool for generating FFI definitions allowing many other languages to call Rust code. With Diplomat, you can simply define Rust APIs to be exposed over FFI and get high-level C, C++, and JavaScript bindings automatically!

Aws-sdk-rust - AWS SDK for the Rust Programming Language

The AWS SDK for Rust This repo contains the new AWS SDK for Rust (the SDK) and its public roadmap. Please Note: The SDK is currently released as a dev

Rust + Yew + Axum + Tauri, full-stack Rust development for Desktop apps.

rust-yew-axum-tauri-desktop template Rust + Yew + Axum + Tauri, full-stack Rust development for Desktop apps. Crates frontend: Yew frontend app for de

A lightning fast version of tmux-fingers written in Rust, copy/pasting tmux like vimium/vimperator
A lightning fast version of tmux-fingers written in Rust, copy/pasting tmux like vimium/vimperator

tmux-thumbs A lightning fast version of tmux-fingers written in Rust for copy pasting with vimium/vimperator like hints. Usage Press ( prefix + Space

A command-line tool collection to assist development written in RUST

dtool dtool is a command-line tool collection to assist development Table of Contents Description Usage Tips Installation Description Now dtool suppor

Rust mid-level IR Abstract Interpreter

MIRAI MIRAI is an abstract interpreter for the Rust compiler's mid-level intermediate representation (MIR). It is intended to become a widely used sta

Migrate C code to Rust
Migrate C code to Rust

C2Rust helps you migrate C99-compliant code to Rust. The translator (or transpiler) produces unsafe Rust code that closely mirrors the input C code. T

C to Rust translator

Corrode: Automatic semantics-preserving translation from C to Rust This program reads a C source file and prints an equivalent module in Rust syntax.

Astronomical algorithms in Rust

astro-rust Contents API Docs About Usage Contributing References About astro-rust is a library of advanced astronomical algorithms for the Rust progra

Releases(v0.4.1)
Owner
Sayan
Fighting with CPU cycles @skytable. Writing open-source code. 99.9985% of the time. He/him
Sayan
Effects for simmsb/dsp-stuff that are gpl licensed

DSP-Stuff effects (gpl) Some of the effects I'm interested in have GPL licensed implementations, as the main project is MIT licensed, the effects that

Ben Simms 1 Jan 28, 2022
This crate allows writing a struct in Rust and have it derive a struct of arrays layed out in memory according to the arrow format.

Arrow2-derive - derive for Arrow2 This crate allows writing a struct in Rust and have it derive a struct of arrays layed out in memory according to th

Jorge Leitao 29 Dec 27, 2022
Yet another geter/setter derive macro.

Gusket Gusket is a getter/setter derive macro. Comparison with getset: gusket only exposes one derive macro. No need to derive(Getters, MutGetters, Se

Jonathan Chan Kwan Yin 2 Apr 12, 2022
Derive macro for encoding/decoding instructions and operands as bytecode

bytecoding Derive macro for encoding and decoding instructions and operands as bytecode. Documentation License Licensed under either of Apache License

Niklas Sauter 15 Mar 20, 2022
Derive macro implementing 'From' for structs

derive-from-ext A derive macro that auto implements 'std::convert::From' for structs. The default behaviour is to create an instance of the structure

Andrew Lowndes 4 Sep 18, 2022
Derive with constructor for each field in struct.

A custom derive implementation for #[derive(with)] Get started 1.Generate with constructor for each field use derive_with::with; #[derive(with, Defau

SystemX Labs 4 Oct 22, 2023
Helper macros: autoimpl, impl_scope

Impl-tools A set of helper macros Macros Autoimpl #[autoimpl] is a variant of #[derive], supporting: explicit generic parameter bounds ignored fields

null 38 Jan 1, 2023
An experimental transpiler to bring tailwind macros to SWC 🚀

stailwc (speedy tailwind compiler) This is an experimental SWC transpiler to bring compile time tailwind macros to SWC (and nextjs) a-la twin macro. T

Alexander Lyon 139 Dec 20, 2022
Simple procedural macros `tnconst![...]`, `pconst![...]`, `nconst![...]` and `uconst![...]` that returns the type level integer from `typenum` crate.

typenum-consts Procedural macros that take a literal integer (or the result of an evaluation of simple mathematical expressions or an environment vari

Jim Chng 3 Mar 30, 2024
k-mer counter in Rust using the rust-bio and rayon crates

krust is a k-mer counter written in Rust and run from the command line that will output canonical k-mers and their frequency across the records in a f

null 14 Jan 7, 2023