enum_to_u8_slice_derive-0.2.0/.cargo_vcs_info.json0000644000000001121370166631600157330ustar00{ "git": { "sha1": "298cd8a505add184edb3eea2561c3b71f6f4f9ae" } } enum_to_u8_slice_derive-0.2.0/.gitignore010064400007650000024000000000461370166615000165240ustar0000000000000000/target/ **/*.rs.bk Cargo.lock .idea/ enum_to_u8_slice_derive-0.2.0/Cargo.toml0000644000000017541370166631600137460ustar00# 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_to_u8_slice_derive" version = "0.2.0" authors = ["Yiming Jing ", "Yu Ding "] description = "A simple fork of enum_to_str_derive (by @DCjanus), convert enum to u8 slice ref" license = "BSD-3-Clause" repository = "https://github.com/mesalock-linux/enum_to_u8_slice_derive" [lib] proc-macro = true [dependencies.proc-macro2] version = "1.0" [dependencies.quote] version = "1.0" [dependencies.syn] version = "1.0" enum_to_u8_slice_derive-0.2.0/Cargo.toml.orig010064400007650000024000000006501370166615000174240ustar0000000000000000[package] name = "enum_to_u8_slice_derive" version = "0.2.0" authors = ["Yiming Jing ", "Yu Ding "] description = "A simple fork of enum_to_str_derive (by @DCjanus), convert enum to u8 slice ref" repository = "https://github.com/mesalock-linux/enum_to_u8_slice_derive" license = "BSD-3-Clause" [dependencies] syn = "1.0" quote = "1.0" proc-macro2 = "1.0" [lib] proc-macro = true enum_to_u8_slice_derive-0.2.0/README.MD010064400007650000024000000004341370166615000157140ustar0000000000000000# enum_to_u8_slice_derive This crate is a fork of the [enum_to_str_derive](https://github.com/kevinis/enum_to_str_derive) crate, written by @[DCjanus](https://github.com/DCjanus). Unlike `enum_to_str_derive` which outputs a `&'static str`, this crate generates a `&'static [u8]` . enum_to_u8_slice_derive-0.2.0/src/lib.rs010064400007650000024000000024031370166615000164360ustar0000000000000000extern crate proc_macro; extern crate syn; #[macro_use] extern crate quote; extern crate proc_macro2; use proc_macro::TokenStream; use syn::{punctuated::Punctuated, token::Comma, DeriveInput, LitByteStr, Variant}; #[proc_macro_derive(EnumToU8)] pub fn enum_to_u8(input: TokenStream) -> TokenStream { //let s = input.to_string(); let ast = syn::parse_macro_input!(input as DeriveInput); let name = &ast.ident; if let syn::Data::Enum(data) = ast.data { impl_enum_to_u8(name, data.variants) } else { panic!("Only work for enum"); } } fn impl_enum_to_u8(name: &syn::Ident, body: Punctuated) -> TokenStream { let get_items: Vec<_> = body .iter() .map(|field| { let ident = field.ident.clone(); let mut name_c_bytes = ident.to_string().as_bytes().to_vec(); name_c_bytes.push(0); let lbs: LitByteStr = LitByteStr::new(&name_c_bytes, ident.span()); quote!( #name::#ident => #lbs ) }) .collect(); quote!( impl #name{ fn enum_to_u8(&self) -> &'static [u8] { match *self{ #(#get_items),* } } } ) .into() }