GithubHelp home page GithubHelp logo

soccer's Introduction

soccer

Associated constants for Rust enums

NOTE: Tests and documentation are very much still a work in progress for the time being

A Basic Example

soccer can generate code to make treating a fieldless enum as a discrete set of constants easier by deriving TryFrom, Into, and Display:

#[derive(Clone, Copy, PartialEq, Eq, TryFrom, Into, Display)]
#[const_ty(char)]
enum Punctuation {
    Plus,
    Minus,
    Star,
    Equals,
}

fn main() {
    assert_eq!(Punctuation::try_from('+'), Ok(Punctuation::Plus));

    let val: char = Punctuation::Star.into();
    assert_eq!(val, '*');

    println!("{}", Punctuation::Minus); // prints "-"
}

It can also use the discriminant to do this:

#[derive(Clone, Copy, PartialEq, Eq, TryFrom, Into, Display)]
#[repr(u8)]
enum Opcode {
    Add,
    Sub,
    Mul,
    Load,
    Store,
}

fn main() {
    assert_eq!(Opcode::try_from(0), Ok(Opcode::Add));

    let val: u8 = Opcode::Load.into();
    assert_eq!(val, 3);

    println!("{}", Opcode::Store); // prints "4"
}

Those are both actual use cases I've encountered in my own Rust projects that motivated the creation of this crate.

(I used to maintain the discrim crate for this discriminant-based conversion codegen, but since it got absorbed into soccer I've archived it and don't work on it anymore)

soccer's People

Contributors

a-lafrance avatar

Watchers

 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.