GithubHelp home page GithubHelp logo

enum_index's Introduction

EnumIndex

EnumIndex is a trait to extract variant index from enums in Rust.
IndexEnum is a trait that allows you to get an enum variant by indexing. It only works if all parameters of Enum implement Default

The main case is implementation of your own enum serialization.

pub trait EnumIndex {
    fn enum_index(&self) -> usize;
}

pub trait IndexEnum {
    fn index_enum(index: usize) -> Option<Self> where Self: Sized;
}

Example

// Contains needed traits
extern crate enum_index;
// Contains derives
#[macro_use]
extern crate enum_index_derive;
use enum_index::{EnumIndex, IndexEnum};

// IndexEnum can only be derived for enums with parameters implementing Default
// In this example Default is checked for String, f32 and u64.
#[derive(EnumIndex, IndexEnum, Debug)]
enum OpCode {
    Disconnect,                       // 0
    SendData(String),                 // 1
    SomethingElse { a: f32, b: u64 }  // 2
}

fn main() {
    let first = OpCode::Disconnect;
    let second = OpCode::SendData("hello");
    let third = OpCode::SomethingElse {a: 1f32, b: 2u64};
    println!("{}", first.enum_index()); // prints 0
    println!("{}", second.enum_index()); // prints 1
    println!("{}", third.enum_index()); // prints 2

    let opcode_variant = OpCode::index_enum(2).unwrap();
    println!("{:?}", opcode_variant); // prints SomethingElse{a: 0f32, b: 0u64}
}

Using in your project

Add the following in your Cargo.toml file

[dependencies]
enum_index = "0.2.0"
enum_index_derive = "0.2.0"

Then import needed trait and macro

extern crate enum_index;
#[macro_use]
extern crate enum_index_derive;

enum_index's People

Contributors

regresscheck avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.