Rust derive macros for some common traits for simple types
The traits that can be derived currently are From
and infix arithmetic traits
(Add
, Sub
, Mul
, Div
, Rem
, BitAnd
, BitOr
, BitXor
).
The arithmetic traits currently only work for structs and simply do the
respective operation on each pair of fields separately.
The From
trait only works for tuple structs with one element (newtypes) or
enums containing these tuple structs. The types wrapped by these tuple structs
can than simply be converted by using the .into()
method.
For enums no from code will be generated for types that occur multiple times since this would be ambiguous.
It can simply be used like this:
```rust
struct MyInt(i32);
enum MyIntEnum{ Int(i32), Bool(bool), UnsignedOne(u32), UnsignedTwo(u32), Nothing, }
```
The resulting code that will be compiled will look like this:
```rust
struct MyInt(i32);
impl ::std::convert::From
enum MyIntEnum {
Int(i32),
Bool(bool),
UnsignedOne(u32),
UnsignedTwo(u32),
Nothing,
}
impl ::std::convert::From
Because of this and Rust its built in type inference the following can be done now:
```rust fn main() { let myenumval = MyIntEnum::Int(5);
if en_enum_val == 5.into() {
println!("The content of my_enum_val is 5")
}
} ```