|
| 1 | +use proc_macro2::TokenStream; |
| 2 | +use quote::{quote, quote_spanned}; |
| 3 | +use syn::Data; |
| 4 | +use syn::spanned::Spanned; |
| 5 | +use synstructure::Structure; |
| 6 | + |
| 7 | +pub(crate)fntry_from_u32(s:Structure<'_>) -> TokenStream{ |
| 8 | +let span_error = |span,message:&str| { |
| 9 | +quote_spanned!{ span => const _:() = ::core::compile_error!(#message);} |
| 10 | +}; |
| 11 | + |
| 12 | +// Must be applied to an enum type. |
| 13 | +ifletSome(span) = match&s.ast().data{ |
| 14 | +Data::Enum(_) => None, |
| 15 | +Data::Struct(s) => Some(s.struct_token.span()), |
| 16 | +Data::Union(u) => Some(u.union_token.span()), |
| 17 | +}{ |
| 18 | +returnspan_error(span,"type is not an enum (TryFromU32)"); |
| 19 | +} |
| 20 | + |
| 21 | +// The enum's variants must not have fields. |
| 22 | +let variant_field_errors = s |
| 23 | +.variants() |
| 24 | +.iter() |
| 25 | +.filter_map(|v| v.ast().fields.iter().map(|f| f.span()).next()) |
| 26 | +.map(|span| span_error(span,"enum variant cannot have fields (TryFromU32)")) |
| 27 | +.collect::<TokenStream>(); |
| 28 | +if !variant_field_errors.is_empty(){ |
| 29 | +return variant_field_errors; |
| 30 | +} |
| 31 | + |
| 32 | +let ctor = s |
| 33 | +.variants() |
| 34 | +.iter() |
| 35 | +.map(|v| v.construct(|_, _| -> TokenStream{unreachable!()})) |
| 36 | +.collect::<Vec<_>>(); |
| 37 | + s.gen_impl(quote!{ |
| 38 | +// The surrounding code might have shadowed these identifiers. |
| 39 | +use::core::convert::TryFrom; |
| 40 | +use::core::primitive::u32; |
| 41 | +use::core::result::Result::{self,Ok,Err}; |
| 42 | + |
| 43 | +genimplTryFrom<u32> for @Self{ |
| 44 | +typeError = u32; |
| 45 | + |
| 46 | + #[allow(deprecated)]// Don't warn about deprecated variants. |
| 47 | +fn try_from(value:u32) -> Result<Self,Self::Error> { |
| 48 | + #(if value == const{ #ctor asu32}{returnOk(#ctor)})* |
| 49 | +Err(value) |
| 50 | +} |
| 51 | +} |
| 52 | +}) |
| 53 | +} |
0 commit comments