Crates.io | derived |
lib.rs | derived |
version | 0.4.2 |
source | src |
created_at | 2021-09-25 07:20:24.917317 |
updated_at | 2021-09-29 16:34:43.875048 |
description | Derive macros for automating the boring stuff |
homepage | |
repository | https://github.com/ohsayan/derived |
max_upload_size | |
id | 456090 |
size | 34,496 |
derived
: Macros for automating the boring stuffThe 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.
Ctor
: To generate constructorsGtor
: To generate gettersStor
: To generate settersConstdef
: To generate constant, compile-time default implementations.
🎉 Arrays, tuples, nested tuples in arrays and nested arrays in tuples included!
where
clause supportgtor
attribute to get either immutable or mutable or both references (see example below)#[stor_skip]
or #[gtor_skip]
attributes for
specific fieldsconst
with the #[ctor_const]
and #[gtor_const]
attributesPhantomData
fields with the #[phantom]
attributedefault
implementationsuse 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');
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);
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);
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);
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");
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);
}
This crate is distributed under the Apache-2.0 License.