Skip to content

Commit

Permalink
add impl for TryFrom for integer types
Browse files Browse the repository at this point in the history
Implements TryFrom for all the integer types. fixes #4.
  • Loading branch information
cardoe committed May 30, 2020
1 parent 220897c commit bac3d33
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
stages:
- build

build-1_32:
build-1_34:
stage: build
image: rust:1.32-slim
image: rust:1.34-slim
dependencies: []
script:
- cargo build --verbose
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# ChangeLog

## 0.2.0
## 0.2.1
### Added
- support TryFrom

## 0.2.0 (yanked)
### Changed
- Upgrade [syn](https://crates.io/crates/syn) and [quote](https://crates.io/crates/quote) to 1.0
- add a better diagnostic for the case where a discriminant isn't specified for
Expand Down
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ gitlab = { repository = "cardoe/enum-primitive-derive" }
proc-macro = true

[dependencies]
num-traits = "0.2"
quote = "1"
syn = "1"

[dev-dependencies]
num-traits = "0.2"
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![Build status](https://gitlab.com/cardoe/enum-primitive-derive/badges/master/pipeline.svg)](https://gitlab.com/cardoe/enum-primitive-derive/commits/master)
[![Rust version]( https://img.shields.io/badge/rust-1.15+-blue.svg)]()
[![Rust version]( https://img.shields.io/badge/rust-1.34+-blue.svg)]()
[![Documentation](https://docs.rs/enum-primitive-derive/badge.svg)](https://docs.rs/enum-primitive-derive)
[![Latest version](https://img.shields.io/crates/v/enum-primitive-derive.svg)](https://crates.io/crates/enum-primitive-derive)
[![All downloads](https://img.shields.io/crates/d/enum-primitive-derive.svg)](https://crates.io/crates/enum-primitive-derive)
Expand All @@ -8,6 +8,8 @@
This is a custom derive, using procedural macros, implementation of
[enum_primitive](https://crates.io/crates/enum_primitive).

MSRV is 1.34.0

## Documentation

[https://docs.rs/enum-primitive-derive/](https://docs.rs/enum-primitive-derive/)
Expand Down
105 changes: 105 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,31 @@
//! assert_eq!(abc.to_u32(), Some(1));
//! }
//! ```
//!
//! # TryFrom Example
//!
//! ```rust
//! use enum_primitive_derive::Primitive;
//! use std::convert::TryFrom;
//!
//! #[derive(Debug, Eq, PartialEq, Primitive)]
//! enum Foo {
//! Bar = 32,
//! Dead = 42,
//! Beef = 50,
//! }
//!
//! fn main() {
//! let bar = Foo::try_from(32);
//! assert_eq!(bar, Ok(Foo::Bar));
//!
//! let dead = Foo::try_from(42);
//! assert_eq!(dead, Ok(Foo::Dead));
//!
//! let unknown = Foo::try_from(12);
//! assert!(unknown.is_err());
//! }
//! ```
extern crate proc_macro;

Expand Down Expand Up @@ -166,6 +191,86 @@ fn impl_primitive(ast: &syn::DeriveInput) -> TokenStream {
}
}
}

impl ::std::convert::TryFrom<u64> for #to_name {
type Error = &'static str;

fn try_from(value: u64) -> Result<Self, Self::Error> {
use ::num_traits::FromPrimitive;

#to_name::from_u64(value).ok_or_else(|| "Unknown variant")
}
}

impl ::std::convert::TryFrom<u32> for #to_name {
type Error = &'static str;

fn try_from(value: u32) -> Result<Self, Self::Error> {
use ::num_traits::FromPrimitive;

#to_name::from_u32(value).ok_or_else(|| "Unknown variant")
}
}

impl ::std::convert::TryFrom<u16> for #to_name {
type Error = &'static str;

fn try_from(value: u16) -> Result<Self, Self::Error> {
use ::num_traits::FromPrimitive;

#to_name::from_u16(value).ok_or_else(|| "Unknown variant")
}
}

impl ::std::convert::TryFrom<u8> for #to_name {
type Error = &'static str;

fn try_from(value: u8) -> Result<Self, Self::Error> {
use ::num_traits::FromPrimitive;

#to_name::from_u8(value).ok_or_else(|| "Unknown variant")
}
}

impl ::std::convert::TryFrom<i64> for #name {
type Error = &'static str;

fn try_from(value: i64) -> Result<Self, Self::Error> {
use ::num_traits::FromPrimitive;

#to_name::from_i64(value).ok_or_else(|| "Unknown variant")
}
}

impl ::std::convert::TryFrom<i32> for #name {
type Error = &'static str;

fn try_from(value: i32) -> Result<Self, Self::Error> {
use ::num_traits::FromPrimitive;

#to_name::from_i32(value).ok_or_else(|| "Unknown variant")
}
}

impl ::std::convert::TryFrom<i16> for #name {
type Error = &'static str;

fn try_from(value: i16) -> Result<Self, Self::Error> {
use ::num_traits::FromPrimitive;

#to_name::from_i16(value).ok_or_else(|| "Unknown variant")
}
}

impl ::std::convert::TryFrom<i8> for #name {
type Error = &'static str;

fn try_from(value: i8) -> Result<Self, Self::Error> {
use ::num_traits::FromPrimitive;

#to_name::from_i8(value).ok_or_else(|| "Unknown variant")
}
}
})
} else {
panic!("#[derive(Primitive)] is only valid for C-like enums");
Expand Down

0 comments on commit bac3d33

Please sign in to comment.