Warning
This package is under construction, handle with care!
attrimpl
The aim of the package is to reduce boilerplate code by adding implementations in trivial cases.
Directives
Directives that can be added before fields
from: implementsFromtrait for the given typeinto: implementsIntotrait for the given typeconvert: adds bothfromandintodirectives for the given fieldderef: implementsDereftrait for the given typederef_mut: implementsDerefandDerefMuttraits for the given typeas_ref: implementsAsReftrait for the given typeas_mut: implementsAsMuttrait for the given typeas: adds bothas_refandas_mutdirectives for the given fieldget_ref: adds a getter method for the field, a reference is returned- accepted params
name: specifies the name of the getter function, default value is<field_name>(e.g.,get_ref(name = "foobar"))
- accepted params
get_clone: adds a getter method for the field, the value is cloned- accepted params
name: specifies the name of the getter function, default value is<field_name>(e.g.,get_clone(name = "foobar"))
- accepted params
get_copy: adds a getter method for the field, the value is copied- accepted params
name: specifies the name of the getter function, default value is<field_name>(e.g.,get_copy(name = "foobar"))
- accepted params
get_mut: adds a mutable getter method for the field- accepted params
name: specifies the name of the getter function (<name>_mut), default value is<field_name>_mut(e.g.,get_mut(name = "foobar"))
- accepted params
access: adds bothget_refandget_mutdirectives for the given field- accepted params
name: specifies the name of both the getter and the get_mut function (e.g.,access(name = "foobar"))get_ref: specifies the type of the getter function, this is the defaultget_clone: specifies the type of the getter functionget_copy: specifies the type of the getter function
- accepted params
Debugging
If the debug argument is added to the macro, then the generated code will be printed to stderr during compilation. Example:
Examples
Named struct (from, into, deref_mut):
// non-boxed
let mut value = from;
value.push_str;
let value: String = value.into;
// boxed
let mut value = Box::from;
value.push_str;
let value: String = .into;
Named struct (into, as_ref, as_mut, get_ref, get_mut):
let mut value = NamedStruct ;
// deref_mut
*value = "ice climbing".to_string;
// deref
assert_eq!;
// access(name = "hobbyy")
*value.hobbyy_mut = "rock climbing".to_string;
assert_eq!;
// get_ref
assert_eq!;
// as_ref
assert_eq!;
// as_mut
*value.as_mut = "John Doe".to_string;
assert_eq!;
// into
let value: String = value.into;
assert_eq!;
Tuple struct:
] String);
// non-boxed
let mut value = from;
value.push_str;
let value: String = value.into;
// boxed
let mut value = Box::from;
value.push_str;
let value: String = .into;
Enum:
// non-boxed
let value = from;
// boxed
let value = Box::from;
Todo
- consider implementing
from_boxeddirective or (better)from(boxed)* - accept only valid directives during parsing for the given item type (e.g., for enums no Deref or DerefMut should be accepted) (better error handling)
- handle errors in the package instead of relying on the Rust compiler where possible
- examine whether it is possible to implement deref, deref_mut, into, as_ref, as_mut on enums if every variant contains the same type
#[attrimpl(from)]should work with multiple field structs and enums. Should be able to set the default values of the other fields (e.g.,#[attrimpl(from(field_default | container_default))]).- write test framework for compile time errors
- write a failing test where non-defined directive is given
- implement the following directives
#[attrimpl(display("asdasd {}"))]- not sure whether to implement that one: #[attrimpl(borrow)]
- search for other possibilities of useful directives