Skip to content

Commit

Permalink
Add TryFrom to convert repr to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ModProg committed Sep 1, 2023
1 parent 58f6dd3 commit fa44984
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
([#279](https://github.com/JelteF/derive_more/pull/279))
- `derive_more::derive` module exporting only macros, without traits.
([#290](https://github.com/JelteF/derive_more/pull/290))
- Add `TryFrom` derive for enums with `#[repr(u/i*)]`.
([#300](https://github.com/JelteF/derive_more/pull/300))

### Changed

Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ mul_assign = ["derive_more-impl/mul_assign"]
mul = ["derive_more-impl/mul"]
not = ["derive_more-impl/not"]
sum = ["derive_more-impl/sum"]
try_from = ["derive_more-impl/try_from"]
try_into = ["derive_more-impl/try_into"]
is_variant = ["derive_more-impl/is_variant"]
unwrap = ["derive_more-impl/unwrap"]
Expand Down Expand Up @@ -92,6 +93,7 @@ full = [
"mul_assign",
"not",
"sum",
"try_from",
"try_into",
"try_unwrap",
"unwrap",
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ These are traits that are used to convert automatically between types.
1. [`From`]
2. [`Into`]
3. [`FromStr`]
4. [`TryInto`]
5. [`IntoIterator`]
6. [`AsRef`], [`AsMut`]
4. [`TryFrom`]
5. [`TryInto`]
6. [`IntoIterator`]
7. [`AsRef`], [`AsMut`]


### Formatting traits
Expand Down
3 changes: 2 additions & 1 deletion impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ repository = "https://github.com/JelteF/derive_more"
documentation = "https://docs.rs/derive_more"

# explicitly no keywords or categories so it cannot be found easily

include = [
"src/**/*.rs",
"doc/**/*.md",
Expand Down Expand Up @@ -66,6 +65,7 @@ mul = ["syn/extra-traits"]
mul_assign = ["syn/extra-traits"]
not = ["syn/extra-traits"]
sum = []
try_from = []
try_into = ["syn/extra-traits"]
try_unwrap = ["dep:convert_case"]
unwrap = ["dep:convert_case"]
Expand All @@ -91,6 +91,7 @@ full = [
"mul_assign",
"not",
"sum",
"try_from",
"try_into",
"try_unwrap",
"unwrap",
Expand Down
27 changes: 27 additions & 0 deletions impl/doc/try_from.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# What `#[derive(TryFrom)]` generates

This derive allows you to convert enum discriminants into their corresponding variants.
A requirement is, that the enum has a `#[repr(u/i*)]` attribute, e.g., `#[repr(u8)]`.
Only field-less variants can be constructed from their variant, therefor the `TryFrom` implementation will return an error for a discriminant representing a variant with fields.

## Example usage

```rust
# use derive_more::TryFrom;
#
#[derive(TryFrom, Debug, PartialEq)]
#[repr(u32)]
enum Enum {
Implicit,
Explicit = 5,
Field(usize),
Empty{},
}

assert_eq!(Enum::Implicit, Enum::try_from(0).unwrap());
assert_eq!(Enum::Explicit, Enum::try_from(5).unwrap());
assert_eq!(Enum::Empty{}, Enum::try_from(7).unwrap());

// variants with fields are not supported
assert!(Enum::try_from(6).is_error());
```
4 changes: 4 additions & 0 deletions impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ mod not_like;
pub(crate) mod parsing;
#[cfg(feature = "sum")]
mod sum_like;
#[cfg(feature = "try_from")]
mod try_from;
#[cfg(feature = "try_into")]
mod try_into;
#[cfg(feature = "try_unwrap")]
Expand Down Expand Up @@ -265,6 +267,8 @@ create_derive!("not", not_like, Neg, neg_derive);
create_derive!("sum", sum_like, Sum, sum_derive);
create_derive!("sum", sum_like, Product, product_derive);

create_derive!("try_from", try_from, TryFrom, try_from_derive, try_from);

create_derive!("try_into", try_into, TryInto, try_into_derive, try_into);

create_derive!(
Expand Down
146 changes: 146 additions & 0 deletions impl/src/try_from.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
//! Implementation of a [`TryFrom`] derive macro.

use proc_macro2::{Literal, Span, TokenStream};
use quote::{format_ident, quote, ToTokens as _};
use syn::{spanned::Spanned as _, Ident, Variant};

/// Expands a [`TryFrom`] derive macro.
pub fn expand(input: &syn::DeriveInput, _: &'static str) -> syn::Result<TokenStream> {
match &input.data {
syn::Data::Struct(data) => Err(syn::Error::new(
data.struct_token.span(),
"`TryFrom` cannot be derived for structs",
)),
syn::Data::Enum(data) => {
let Some(repr) = ReprAttribute::parse_attrs(&input.attrs)? else {
return Err(syn::Error::new(
Span::call_site(),
"`TryFrom` needs repr specified `#[repr(u*/i*)]`",
));
};

Expansion {
repr,
ident: &input.ident,
variants: data.variants.iter().collect(),
generics: &input.generics,
}
.expand()
}
syn::Data::Union(data) => Err(syn::Error::new(
data.union_token.span(),
"`TryFrom` cannot be derived for unions",
)),
}
}

/// Representation of a [`Repr`] derive macro struct container attribute.
///
/// ```rust,ignore
/// #[repr(<type>)]
/// ```
struct ReprAttribute(Ident);

impl ReprAttribute {
/// Parses a [`StructAttribute`] from the provided [`syn::Attribute`]s.
fn parse_attrs(attrs: impl AsRef<[syn::Attribute]>) -> syn::Result<Option<Self>> {
Ok(attrs
.as_ref()
.iter()
.filter(|attr| attr.path().is_ident("repr"))
.try_fold(None, |attrs, attr| {
if attrs.is_some() {
Err(syn::Error::new(
attr.path().span(),
"only a single `#[repr(...)]` attribute is supported",
))
} else {
let repr: Ident = attr.parse_args()?;
match repr.to_string().as_str() {
"u8" | "u16" | "u32" | "u64" | "u128" | "usize" | "i8"
| "i16" | "i32" | "i64" | "i128" | "isize" => Ok(Some(repr)),
_ => Err(syn::Error::new(
repr.span(),
"only integer discriminants (u*/i*) are supported",
)),
}
}
})?
.map(Self))
}
}

/// Expansion of a macro for generating [`TryFrom`] implementation of an enum
struct Expansion<'a> {
/// Enum `#[repr(u/i*)]`
repr: ReprAttribute,
/// Enum [`Ident`].
ident: &'a Ident,

/// Variant [`Ident`] in case of enum expansion.
variants: Vec<&'a syn::Variant>,

/// Struct or enum [`syn::Generics`].
generics: &'a syn::Generics,
}

impl<'a> Expansion<'a> {
/// Expands [`TryFrom`] implementations for a struct.
fn expand(&self) -> syn::Result<TokenStream> {
let ident = self.ident;
let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl();

let repr = &self.repr.0;

let mut last_discriminant = quote!(0);
let mut inc = 0usize;
let (consts, (discriminants, variants)): (
Vec<Ident>,
(Vec<TokenStream>, Vec<TokenStream>),
) = self
.variants
.iter()
.filter_map(
|Variant {
ident,
fields,
discriminant,
..
}| {
if let Some(discriminant) = discriminant {
last_discriminant = discriminant.1.to_token_stream();
inc = 0;
}
let ret = {
let inc = Literal::usize_unsuffixed(inc);
fields.is_empty().then_some((
format_ident!("__DISCRIMINANT_{ident}"),
(quote!(#last_discriminant + #inc), quote!(#ident #fields)),
))
};
inc += 1;
ret
},
)
.unzip();

Ok(quote! {
#[automatically_derived]
impl #impl_generics
::core::convert::TryFrom<#repr #ty_generics> for #ident
#where_clause
{
type Error = ::derive_more::TryFromError<#repr>;

#[inline]
fn try_from(value: #repr) -> ::core::result::Result<Self, Self::Error> {
#(#[allow(non_upper_case_globals)] const #consts: #repr = #discriminants;)*
match value {
#(#consts => ::core::result::Result::Ok(#ident::#variants),)*
_ => ::core::result::Result::Err(::derive_more::TryFromError::new(value)),
}
}
}
})
}
}
32 changes: 32 additions & 0 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,35 @@ impl<T> fmt::Display for TryIntoError<T> {

#[cfg(feature = "std")]
impl<T: fmt::Debug> std::error::Error for TryIntoError<T> {}

/// Error returned by the derived [`TryFrom`] implementation.
///
/// [`TryFrom`]: macro@crate::TryFrom
#[derive(Clone, Copy, Debug)]
pub struct TryFromError<T> {
/// Original input value which failed to convert via the derived
/// [`TryFrom`] implementation.
///
/// [`TryFrom`]: macro@crate::TryFrom
pub input: T,
}

impl<T> TryFromError<T> {
#[doc(hidden)]
#[must_use]
#[inline]
pub const fn new(input: T) -> Self {
Self { input }
}
}

// `T` should only be an integer type and therefor display
impl<T: fmt::Display> fmt::Display for TryFromError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "`{}` does not respond to a unit variant", self.input)
}
}

#[cfg(feature = "std")]
// `T` should only be an integer type and therefor display and debug
impl<T: fmt::Debug + fmt::Display> std::error::Error for TryFromError<T> {}
12 changes: 11 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! [`From`]: macro@crate::From
//! [`Into`]: macro@crate::Into
//! [`FromStr`]: macro@crate::FromStr
//! [`TryFrom`]: macro@crate::TryInto
//! [`TryInto`]: macro@crate::TryInto
//! [`IntoIterator`]: macro@crate::IntoIterator
//! [`AsRef`]: macro@crate::AsRef
Expand Down Expand Up @@ -89,8 +90,11 @@ mod r#str;
#[doc(inline)]
pub use crate::r#str::FromStrError;

#[cfg(feature = "try_into")]
#[cfg(any(feature = "try_into", feature = "try_from"))]
mod convert;
#[cfg(feature = "try_from")]
#[doc(inline)]
pub use crate::convert::TryFromError;
#[cfg(feature = "try_into")]
#[doc(inline)]
pub use crate::convert::TryIntoError;
Expand Down Expand Up @@ -203,6 +207,8 @@ re_export_traits!("not", not_traits, core::ops, Neg, Not);

re_export_traits!("sum", sum_traits, core::iter, Product, Sum);

re_export_traits!("try_from", try_from_traits, core::convert, TryFrom);

re_export_traits!("try_into", try_into_traits, core::convert, TryInto);

// Now re-export our own derives by their exact name to overwrite any derives that the trait
Expand Down Expand Up @@ -271,6 +277,9 @@ pub use derive_more_impl::{Neg, Not};
#[cfg(feature = "sum")]
pub use derive_more_impl::{Product, Sum};

#[cfg(feature = "try_from")]
pub use derive_more_impl::TryFrom;

#[cfg(feature = "try_into")]
pub use derive_more_impl::TryInto;

Expand Down Expand Up @@ -303,6 +312,7 @@ pub use derive_more_impl::Unwrap;
feature = "mul_assign",
feature = "not",
feature = "sum",
feature = "try_from",
feature = "try_into",
feature = "try_unwrap",
feature = "unwrap",
Expand Down
23 changes: 23 additions & 0 deletions tests/try_from.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(dead_code)]

use derive_more::TryFrom;

#[derive(TryFrom, Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i16)]
enum Enum {
A,
Discriminant = 5,
Field(usize),
Empty {},
FieldWithDiscriminant(u8, i64) = -14,
EmptyTuple(),
}

#[test]
fn test() {
assert_eq!(Enum::A, Enum::try_from(0).unwrap());
assert_eq!(Enum::Discriminant, Enum::try_from(5).unwrap());
assert_eq!(Enum::Empty {}, Enum::try_from(7).unwrap());
assert_eq!(Enum::EmptyTuple(), Enum::try_from(-13).unwrap());
}

0 comments on commit fa44984

Please sign in to comment.