Crate derive_tools

Source
Expand description

§Module :: derive_tools

experimental rust-status docs.rs Open in Gitpod discord

§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
Variadic constructor.

Enums§

ParseError
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 +=.
AsStaticRefDeprecated
A cheap reference-to-reference conversion. Used to convert a value to a reference value with 'static lifetime within generic code.
CloneDyn
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 /=.
EnumCount
A trait for capturing the number of variants in Enum. This trait can be autoderived by strum_macros.
EnumMessage
Associates additional pieces of information with an Enum. This can be autoimplemented by deriving EnumMessage and annotating your variants with #[strum(message="...")].
EnumProperty
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 the strum_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 type E in Result<T, E>.
From1
Constructor with single argument.
From2
Constructor with two arguments.
From3
Constructor with three arguments.
Into
A value-to-value conversion that consumes the input value. The opposite of From.
Into1
value-to-value conversion that consumes the input value. Change left and rught, but keep semantic of `From1``.
IntoEnumIterator
This trait designates that an Enum can be iterated over. It can be auto generated using strum_macros on your behalf.
IntoIterator
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.
VariantIterator
VariantMetadata
VariantNames
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 to generate former for a structure. Former is variation of Builder Pattern.
phantom
Provides an automatic PhantomData field for a struct based on its generic types.

Derive Macros§

Add
What #[derive(Add)] generates
AddAssign
What #[derive(AddAssign)] generates
AsMut
Derive macro to implement AsMut when-ever it’s possible to do automatically.
AsRef
Derive macro to implement AsRef when-ever it’s possible to do automatically.
AsRefStr
Converts enum variants to &'static str.
AsStaticStr
Constructor
What #[derive(Constructor)] generates
Deref
Derive macro to implement Deref when-ever it’s possible to do automatically.
DerefMut
Derive macro to implement Deref when-ever it’s possible to do automatically.
Display
Div
What #[derive(Mul)] generates
DivAssign
What #[derive(MulAssign)] generates
EnumCount
Add a constant usize equal to the number of variants.
EnumDiscriminants
Generate a new type with only the discriminant names.
EnumIs
Generated is_*() methods for each variant. E.g. Color.is_red().
EnumIter
Creates a new type that iterates of the variants of an enum.
EnumMessage
Add a verbose message to an enum variant.
EnumProperty
Add custom properties to enum variants.
EnumString
Converts strings to enum variants based on their name.
EnumTryAs
Generated try_as_*() methods for all tuple-style variants. E.g. Message.try_as_write().
EnumVariantNames
Implements Strum::VariantNames which adds an associated constant VARIANTS which is an array of discriminant names.
Error
Using #[derive(Error)]
From
Provides an automatic From implementation for struct wrapping a single value.
FromRepr
Add a function to enum that allows accessing variants by its discriminant
FromStr
Index
Provides an automatic Index trait implementation when-ever it’s possible.
IndexMut
Provides an automatic IndexMut trait implementation when-ever it’s possible.
InnerFrom
Derive macro to implement From converting outer type into inner when-ever it’s possible to do automatically.
Into
What #[derive(Into)] generates
IntoIterator
Using #[derive(IntoIterator)]
IntoStaticStr
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
Provides an automatic new implementation for struct wrapping a single value.
Not
Provides an automatic Not trait implementation for struct.
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
VariadicFrom
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.