Derive Macro derive_tools::VariadicFrom
source · #[derive(VariadicFrom)]
{
// Attributes available to this derive:
#[debug]
}
Expand description
The derive_variadic_from macro is designed to provide a way to implement the From-like
traits for structs with a variable number of fields, allowing them to be constructed from
tuples of different lengths or from individual arguments. This functionality is particularly
useful for creating flexible constructors that enable different methods of instantiation for
a struct. By automating the implementation of traits, this macro reduces boilerplate code
and enhances code readability and maintainability.
§Key Features
- Flexible Construction: Allows a struct to be constructed from different numbers of arguments, converting each to the appropriate type.
- Tuple Conversion: Enables the struct to be constructed from tuples, leveraging the
FromandIntotraits for seamless conversion. - Code Generation: Automates the implementation of these traits, reducing the need for manual coding and ensuring consistent constructors.
§Limitations
Currently, the macro supports up to 3 arguments. If your struct has more than 3 fields, the
derive macro will generate no implementation. It supports tuple conversion, allowing structs
to be instantiated from tuples by leveraging the From and Into traits for seamless conversion.
§Example Usage
This example demonstrates the use of the variadic_from macro to implement flexible
constructors for a struct, allowing it to be instantiated from different numbers of
arguments or tuples. It also showcases how to derive common traits like Debug,
PartialEq, Default, and VariadicFrom for the struct.
#[ cfg( not( all(feature = "enabled", feature = "type_variadic_from", feature = "derive_variadic_from" ) ) ) ]
fn main(){}
#[ cfg( all( feature = "enabled", feature = "type_variadic_from", feature = "derive_variadic_from" ) )]
fn main()
{
use variadic_from::exposed::*;
// Define a struct `MyStruct` with fields `a` and `b`.
// The struct derives common traits like `Debug`, `PartialEq`, `Default`, and `VariadicFrom`.
#[ derive( Debug, PartialEq, Default, VariadicFrom ) ]
// Use `#[ debug ]` to expand and debug generate code.
// #[ debug ]
struct MyStruct
{
a : i32,
b : i32,
}
// Implement the `From1` trait for `MyStruct`, which allows constructing a `MyStruct` instance
// from a single `i32` value by assigning it to both `a` and `b` fields.
impl From1< i32 > for MyStruct
{
fn from1( a : i32 ) -> Self { Self { a, b : a } }
}
let got : MyStruct = from!();
let exp = MyStruct { a : 0, b : 0 };
assert_eq!( got, exp );
let got : MyStruct = from!( 13 );
let exp = MyStruct { a : 13, b : 13 };
assert_eq!( got, exp );
let got : MyStruct = from!( 13, 14 );
let exp = MyStruct { a : 13, b : 14 };
assert_eq!( got, exp );
dbg!( exp );
//> MyStruct {
//> a: 13,
//> b: 14,
//> }
}§Debugging
If your struct has a debug attribute, the macro will print information about the generated code for diagnostic purposes.
#[ derive( Debug, PartialEq, Default, VariadicFrom ) ]
// Use `#[ debug ]` to expand and debug generate code.
// #[ debug ]
struct MyStruct
{
a: i32,
b: i32,
}