#enum #trait #derive #deriving #macro

enum_traits_macros

Procedural macros that adds functionality to enums by deriving traits from the crate enum_traits

9 releases (5 stable)

2.0.0 Jan 17, 2017
1.2.1 Nov 11, 2016
0.1.3 Dec 29, 2015
0.1.2 Nov 27, 2015
0.1.0 Aug 22, 2015

#44 in Procedural macro helpers

Download history 29/week @ 2018-05-27 32/week @ 2018-06-03 5/week @ 2018-06-10 31/week @ 2018-06-17 5/week @ 2018-06-24 140/week @ 2018-07-01 12/week @ 2018-07-08 12/week @ 2018-07-15 36/week @ 2018-07-22 27/week @ 2018-07-29 45/week @ 2018-08-05 32/week @ 2018-08-12

55 downloads per month
Used in 2 crates

MIT license

18KB
518 lines

enum_traits

A library with traits and accompanying procedural macros that adds functionality to enums.

Provides traits and "derives" for enum items in the Rust programming language:

Derives

  • EnumIndex (impl Index)
  • EnumFromIndex (impl FromIndex)
  • EnumToIndex (impl ToIndex)
  • EnumLen (impl Len)
  • EnumEnds (impl Ends)
  • EnumDiscriminant (impl Discriminant)
  • EnumIter (impl Iterable)
  • EnumIterator (impl Iterator)
  • EnumVariantName (impl VariantName)
  • EnumBitPattern (impl BitPattern)
  • EnumUnitVariant (impl UnitVariant)
  • EnumIsVariantFns
  • EnumFromVariantName (impl FromStr)

Traits

  • Index
  • FromIndex
  • ToIndex
  • Len
  • Ends
  • Discriminant
  • Iterable
  • VariantName
  • BitPattern
  • UnitVariant

Usage

Cargo.toml:

[dependencies]
enum_traits        = "*"
enum_traits_macros = "*"

With no_std:

[dependencies]
enum_traits        = {version="*",features=["no_std"]}
enum_traits_macros = {version="*",features=["no_std"]}

Examples

//#![feature(associated_consts)]

#[macro_use]extern crate enum_traits_macros;
extern crate enum_traits;

use enum_traits::*;

fn f1(){
	#[derive(Debug,EnumIndex,EnumToIndex,EnumLen)]
	enum Enum<'t,T: 't>{
		VariantA(&'t T),
		VariantB(T),
		VariantC(T,T,T),
		VariantD{d: i32},
		VariantE{a: i8,b: i16,c: i32},
		VariantF,
	}

	assert_eq!(Enum::VariantB("OK").into_index(),1);
	assert_eq!(Enum::<'static,&'static str>::LEN,6);
}

fn f2(){
	#[derive(Debug,EnumIndex,EnumFromIndex,EnumToIndex,EnumLen,EnumIter,EnumIterator,EnumDiscriminant,EnumEnds)]
	enum Enum{
		VariantA = 10,
		VariantB = 20,
		VariantC = 30,
	}

	//From EnumToIndex
	assert_eq!(Enum::VariantB.into_index(),1);

	//From EnumLen
	//assert_eq!(Enum::LEN,3);
	assert_eq!(Enum::len(),3);

	//From EnumFromIndex
	assert!(match Enum::from_index(1){
		Some(Enum::VariantB) => true,
		_ => false
	});

	//From EnumDiscriminant
	assert!(match Enum::from_discriminant(20){
		Some(Enum::VariantB) => true,
		_ => false
	});

	//From EnumEnds
	assert!(match Enum::first(){
		Enum::VariantA => true,
		_ => false
	});

	//From EnumEnds
	assert!(match <Enum as Ends>::last(){
		Enum::VariantC => true,
		_ => false
	});

	//From EnumIter
	assert!(match Enum::variants().next(){
		Some(Enum::VariantA) => true,
		_ => false
	});

	//From EnumIterator
	assert!(match Enum::VariantA.next(){
		Some(Enum::VariantB) => true,
		_ => false
	});
}

See the tests for more examples. See the docs for the library, docs for the derives, the tests or the source code for more information.

Dependencies

~328KB