GithubHelp home page GithubHelp logo

zaaarf / from-to-repr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ravualhemio/from-to-repr

0.0 0.0 0.0 33 KB

Derives TryFrom and From implementations for the representation type of a Rust enumeration.

License: Creative Commons Zero v1.0 Universal

Rust 100.00%

from-to-repr's Introduction

from-to-repr

Provides the procedural macro FromToRepr which can be applied to a enum with an explicit representation (e.g #[repr(u8)]) and derives TryFrom from the representation type to the enum and From from the enum to the representation type.

As an example,

#[derive(FromToRepr)]
#[repr(u8)]
enum ColorChannel {
    RED = 0,
    GREEN = 1,
    BLUE = 2,
}

becomes

#[repr(u8)]
enum ColorChannel {
    RED = 0,
    GREEN = 1,
    BLUE = 2,
}
impl ::std::convert::TryFrom<u8> for ColorChannel {
    type Error = u8;
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        if value == 0 {
            Ok(Self::RED)
        } else if value == 1 {
            Ok(Self::GREEN)
        } else if value == 2 {
            Ok(Self::BLUE)
        } else {
            Err(value)
        }
    }
}
impl ::std::convert::From<ColorChannel> for u8 {
    fn from(value: ColorChannel) -> Self {
        match value {
            ColorChannel::RED => 0,
            ColorChannel::GREEN => 1,
            ColorChannel::BLUE => 2,
        }
    }
}

from_to_other

Additionally, when the feature from_to_other is enabled, an attribute macro named from_to_other is enabled, which generates conversions to and from a base type, representing unknown values using an Other enum variant. For example:

use from_to_repr::from_to_other;

#[from_to_other(base_type = u8)]
enum ColorCommand {
    SetRed = 0,
    SetGreen = 1,
    SetBlue = 2,
    Other(u8),
}

is equivalent to

enum ColorCommand {
    SetRed,
    SetGreen,
    SetBlue,
    Other(u8),
}
impl ::core::convert::From<u8> for ColorCommand {
    fn from(base_value: u8) -> Self {
        if base_value == 0 {
            Self::SetRed
        } else if base_value == 1 {
            Self::SetGreen
        } else if base_value == 2 {
            Self::SetBlue
        } else {
            Self::Other(value)
        }
    }
}
impl ::core::convert::From<ColorCommand> for u8 {
    fn from(enum_value: ColorCommand) -> Self {
        match enum_value {
            ColorCommand::SetRed => 0,
            ColorCommand::SetGreen => 1,
            ColorCommand::SetBlue => 2,
            ColorCommand::Other(other) => other,
        }
    }
}

from-to-repr's People

Contributors

ravualhemio avatar zaaarf 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.