Coder Social home page Coder Social logo

linesprower / table-enum Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sirwhinesalot/table-enum

0.0 0.0 0.0 96 KB

A convenient rust macro to create enums with associated constant data (note: this is different from normal rust enums which are really tagged unions)

License: MIT License

Rust 100.00%

table-enum's Introduction

table-enum

Icon

A convenient rust macro to create enums with associated constant data. It lets you associate constant values to enum variants, similar to how enums work in Java, or how X macros are often used in C.

Only the enum tag is ever passed around, the data is accessed through generated const fn functions that match the enum tag to the relevant data.

This is different from how enums are typically used in Rust, which are actually tagged unions. (also known as variant types or sum types in computer science theory).

When would you use this?

An example where non-tagged-union1 enums are very useful is compiler or interpreter development. For example:

use table_enum::table_enum;

table_enum! {
    enum BinaryOp(#[constructor] text: &'static str, precedence: i32, #[default] right_assoc: bool) {
        Add("+", 10, _),
        Sub("-", 10, _),
        Mul("*", 20, _),
        Div("/", 20, _),
        Pow("**", 30, true),
        ...
    }
}

How does it work?

The example above expands into the following code:

enum BinaryOp {
    Add,
    Sub,
    Mul,
    Div,
    Pow,
    ...
}
impl BinaryOp {
    const fn text(&self) -> &'static str {
        match self {
            BinaryOp::Add => "+",
            BinaryOp::Sub => "-",
            BinaryOp::Mul => "*",
            BinaryOp::Div => "/",
            BinaryOp::Pow => "**",
            ...
        }
    }
    pub fn new(text: &'static str) -> Option<Self> {
        match text {
            "+" => Some(BinaryOp::Add),
            "-" => Some(BinaryOp::Sub),
            "*" => Some(BinaryOp::Mul),
            "/" => Some(BinaryOp::Div),
            "**" => Some(BinaryOp::Pow),
            ...
            _ => None
        }
    }
    const fn precedence(&self) -> i32 {
        match self {
            BinaryOp::Add => 10,
            BinaryOp::Sub => 10,
            BinaryOp::Mul => 20,
            BinaryOp::Div => 20,
            BinaryOp::Pow => 30,
            ...
        }
    }
    // cannot be const because Default::default() is not const
    fn right_assoc(&self) -> bool {
        match self {
            BinaryOp::Add => bool::default(),
            BinaryOp::Sub => bool::default(),
            BinaryOp::Mul => bool::default(),
            BinaryOp::Div => bool::default(),
            BinaryOp::Pow => true,
            ...
        }
    }
}

Note that no more than one field can have the #[constructor] attribute. If a field has the #[constructor] attribute, that field must have different values for every enum tag.

Changelog

  • 0.3.0: Added #[constructor] attribute.
  • 0.2.0: Added #[option] and#[default] attributes to the enum field declarations, see the documentation for more.
  • 0.1.0: First version

Alternative Crates

  • enum-assoc: more powerful but less convenient.
  • const-table: similar idea but as an attribute macro. Honestly a better approach to the same problem syntax wise. Uses an Array of Structs layout instead of Struct of Arrays unfortunately.

Footnotes

  1. I really wish Rust and Swift hadn't called their tagged unions "enums". To me enums are meant to be used as in this macro. A tagged union should be a kind of union. โ†ฉ

table-enum's People

Contributors

sirwhinesalot avatar linesprower 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.