Expand description
§Module :: derive_tools
§Basic use-case
{
use derive_tools::*;
#[ derive( From, InnerFrom, Display, FromStr, PartialEq, Debug ) ]
#[ display( "{a}-{b}" ) ]
struct Struct1
{
a : i32,
b : i32,
}
// derived InnerFrom
let src = Struct1 { a : 1, b : 3 };
let got : ( i32, i32 ) = src.into();
let exp = ( 1, 3 );
assert_eq!( got, exp );
// derived From
let src : Struct1 = ( 1, 3 ).into();
let got : ( i32, i32 ) = src.into();
let exp = ( 1, 3 );
assert_eq!( got, exp );
// derived Display
let src = Struct1 { a : 1, b : 3 };
let got = format!( "{}", src );
let exp = "1-3";
println!( "{}", got );
assert_eq!( got, exp );
// derived FromStr
use std::str::FromStr;
let src = Struct1::from_str( "1-3" );
let exp = Ok( Struct1 { a : 1, b : 3 } );
assert_eq!( src, exp );
}
§To add to your project
cargo add derive_tools
§Try out from the repository
git clone https://github.com/Wandalen/wTools
cd wTools
cd examples/derive_tools_trivial
cargo run
Modules§
- _private_
phf_ reexport_ for_ macro_ if_ phf_ feature - Rust-PHF is a library to generate efficient lookup tables at compile time using perfect hash functions.
- additional_
attributes - Documentation for Additional Attributes
- clone_
dyn - dependency
- Namespace with dependencies.
- exposed
- Exposed namespace of the module.
- orphan
- Orphan namespace of the module.
- own
- Own namespace of the module.
- prelude
- Prelude to use essentials:
use my_module::prelude::*
. - variadic
- Module ::
variadic_from
Macros§
- from
- Macro to construct a struct from variadic arguments.
Enums§
- Parse
Error - The
ParseError
enum is a collection of all the possible reasons an enum can fail to parse from a string.
Traits§
- Add
- The addition operator
+
. - AddAssign
- The addition assignment operator
+=
. - AsStatic
Ref Deprecated - A cheap reference-to-reference conversion. Used to convert a value to a
reference value with
'static
lifetime within generic code. - Clone
Dyn - A trait to upcast a clonable entity and clone it. It’s implemented for all entities which can be cloned.
- Div
- The division operator
/
. - DivAssign
- The division assignment operator
/=
. - Enum
Count - A trait for capturing the number of variants in Enum. This trait can be autoderived by
strum_macros
. - Enum
Message - Associates additional pieces of information with an Enum. This can be
autoimplemented by deriving
EnumMessage
and annotating your variants with#[strum(message="...")]
. - Enum
Property EnumProperty
is a trait that makes it possible to store additional information with enum variants. This trait is designed to be used with the macro of the same name in thestrum_macros
crate. Currently, the only string literals are supported in attributes, the other methods will be implemented as additional attribute types become stabilized.- Error
Error
is a trait representing the basic expectations for error values, i.e., values of typeE
inResult<T, E>
.- From1
- Trait for converting from one argument.
- From2
- Trait for converting from two arguments.
- From3
- Trait for converting from three arguments.
- Into
- A value-to-value conversion that consumes the input value. The
opposite of
From
. - Into
Enum Iterator - This trait designates that an
Enum
can be iterated over. It can be auto generated usingstrum_macros
on your behalf. - Into
Iterator - Conversion into an
Iterator
. - Mul
- The multiplication operator
*
. - MulAssign
- The multiplication assignment operator
*=
. - Sub
- The subtraction operator
-
. - SubAssign
- The subtraction assignment operator
-=
. - Sum
- Trait to represent types that can be created by summing up an iterator.
- TryInto
- An attempted conversion that consumes
self
, which may or may not be expensive. - Variant
Iterator - Variant
Metadata - Variant
Names - A trait for retrieving the names of each variant in Enum. This trait can
be autoderived by
strum_macros
.
Functions§
- clone
- True clone which is applicable not only to clonable entities, but to trait objects implementing
CloneDyn
. - clone_
into_ box - Clone boxed dyn.
Attribute Macros§
- clone_
dyn - Derive macro for
CloneDyn
trait.
Derive Macros§
- Add
- What
#[derive(Add)]
generates - AddAssign
- What
#[derive(AddAssign)]
generates - AsMut
- Implement
AsMut
for a structure. - AsRef
- Implement
AsRef
for a structure. - AsRef
Str - Converts enum variants to
&'static str
. - AsStatic
Str - Constructor
- What
#[derive(Constructor)]
generates - Deref
- Implement
Deref
for a structure. - Deref
Mut - Implement
DerefMut
for a structure. - Display
- Div
- What
#[derive(Mul)]
generates - DivAssign
- What
#[derive(MulAssign)]
generates - Enum
Count - Add a constant
usize
equal to the number of variants. - Enum
Discriminants - Generate a new type with only the discriminant names.
- EnumIs
- Generated
is_*()
methods for each variant. E.g.Color.is_red()
. - Enum
Iter - Creates a new type that iterates of the variants of an enum.
- Enum
Message - Add a verbose message to an enum variant.
- Enum
Property - Add custom properties to enum variants.
- Enum
String - Converts strings to enum variants based on their name.
- Enum
TryAs - Generated
try_as_*()
methods for all tuple-style variants. E.g.Message.try_as_write()
. - Enum
Variant Names - Implements
Strum::VariantNames
which adds an associated constantVARIANTS
which is an array of discriminant names. - Error
- Using
#[derive(Error)]
- From
- Implement
From
for a structure. - From
Repr - Add a function to enum that allows accessing variants by its discriminant
- FromStr
- Index
- Implement
Index
for a structure. - Index
Mut - Implement
IndexMut
for a structure. - Inner
From - Implement
InnerFrom
for a structure. - Into
- What
#[derive(Into)]
generates - Into
Iterator - Using
#[derive(IntoIterator)]
- Into
Static Str - Implements
From<MyEnum> for &'static str
on an enum. - IsVariant
- What
#[derive(IsVariant)]
generates - Mul
- What
#[derive(Mul)]
generates - MulAssign
- What
#[derive(MulAssign)]
generates - New
- Implement
New
for a structure. - Not
- Implement
Not
for a structure. - Sub
- What
#[derive(Add)]
generates - SubAssign
- What
#[derive(AddAssign)]
generates - Sum
- Using
#[derive(Sum)]
- ToString
- implements
std::string::ToString
on an enum - TryInto
- What
#[derive(TryInto)]
generates - Unwrap
- What
#[derive(Unwrap)]
generates - Variadic
From - Implement
VariadicFrom
for a structure.