enum-map-derive-0.4.3/Cargo.toml.orig010064400017500001751000000011071353577005500156330ustar0000000000000000[package] name = "enum-map-derive" version = "0.4.3" authors = ["Konrad Borowski "] repository = "https://gitlab.com/KonradBorowski/enum-map" license = "MIT/Apache-2.0" description = "Macros 1.1 implementation of #[derive(Enum)]" keywords = ["data-structure", "no_std", "enum"] categories = ["data-structures", "no-std"] [badges] gitlab = { repository = "KonradBorowski/enum-map" } maintenance = { status = "actively-developed" } [lib] proc-macro = true [dependencies] proc-macro2 = "1.0.0" quote = "1.0.0" syn = "1.0.1" [dev-dependencies] enum-map = "0.5.0" enum-map-derive-0.4.3/Cargo.toml0000644000000022170000000000000120720ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "enum-map-derive" version = "0.4.3" authors = ["Konrad Borowski "] description = "Macros 1.1 implementation of #[derive(Enum)]" keywords = ["data-structure", "no_std", "enum"] categories = ["data-structures", "no-std"] license = "MIT/Apache-2.0" repository = "https://gitlab.com/KonradBorowski/enum-map" [lib] proc-macro = true [dependencies.proc-macro2] version = "1.0.0" [dependencies.quote] version = "1.0.0" [dependencies.syn] version = "1.0.1" [dev-dependencies.enum-map] version = "0.5.0" [badges.gitlab] repository = "KonradBorowski/enum-map" [badges.maintenance] status = "actively-developed" enum-map-derive-0.4.3/src/lib.rs010064400017500001751000000065041353577004700146560ustar0000000000000000//! Procedural macro implementing `#[derive(Enum)]` //! //! This is supposed to used with `enum-map` crate, which provides the //! actual usage documentation. #![recursion_limit = "128"] extern crate proc_macro; extern crate proc_macro2; #[macro_use] extern crate quote; extern crate syn; use std::iter; use syn::{Data, DataEnum, DeriveInput, Fields, Ident, Variant}; fn generate_enum_code(name: Ident, data_enum: DataEnum) -> proc_macro2::TokenStream { let enum_count = data_enum.variants.len(); let mut has_discriminants = false; for &Variant { ref fields, ref discriminant, .. } in &data_enum.variants { match *fields { Fields::Unit => (), _ => return quote!(compile_error! {"#[derive(Enum)] requires C style style enum"}), } if discriminant.is_some() { has_discriminants = true; } } let variants_names_a = data_enum.variants.iter().map(|variant| &variant.ident); let variants_names_b = data_enum.variants.iter().map(|variant| &variant.ident); let repeat_name_a = iter::repeat(&name); let repeat_name_b = repeat_name_a.clone(); let counter = 0..enum_count; let to_usize = if enum_count == 0 || has_discriminants { let variants_names = data_enum.variants.iter().map(|variant| &variant.ident); let repeat_name = repeat_name_a.clone(); let counter = counter.clone(); quote! { match self { #( #repeat_name::#variants_names => #counter, )* } } } else { quote! { self as usize } }; quote! { #[automatically_derived] impl ::enum_map::Enum for #name { type Array = [V; #enum_count]; const POSSIBLE_VALUES: usize = #enum_count; #[inline] fn slice(array: &Self::Array) -> &[V] { array } #[inline] fn slice_mut(array: &mut Self::Array) -> &mut [V] { array } #[inline] fn from_usize(value: usize) -> Self { match value { #( #counter => #repeat_name_a::#variants_names_a, )* _ => unreachable!() } } #[inline] fn to_usize(self) -> usize { #to_usize } #[inline] fn from_function V>(mut _f: F) -> Self::Array { [#( _f(#repeat_name_b::#variants_names_b), )*] } } } } /// Procedural derive generating `enum_map::Enum` implementation. /// /// # Examples /// /// ``` /// # extern crate enum_map; /// use enum_map::Enum; /// /// #[derive(Enum)] /// enum A { /// B, /// C, /// D, /// } /// /// assert_eq!(Enum::<()>::to_usize(A::C), 1); /// ``` #[proc_macro_derive(Enum)] pub fn derive_enum_map(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let input: DeriveInput = syn::parse(input).unwrap(); let result = match input.data { Data::Enum(data_enum) => generate_enum_code(input.ident, data_enum), _ => quote!(compile_error! {"#[derive(Enum)] is only defined for enums"}), }; result.into() } enum-map-derive-0.4.3/.cargo_vcs_info.json0000644000000001120000000000000140640ustar00{ "git": { "sha1": "a466a169c5b6d12a31f2cbea258a76d7ec9193df" } }