MCPcopy Create free account
hub / github.com/Lymia/enumset / generate_code

Function generate_code

enumset_derive/src/gen.rs:51–471  ·  view source on GitHub ↗

Generates the actual `EnumSetType` impl.

(info: EnumSetInfo)

Source from the content-addressed store, hash-verified

49
50/// Generates the actual `EnumSetType` impl.
51pub fn generate_code(info: EnumSetInfo) -> SynTokenStream {
52 let paths = Paths::from(&info);
53 let name = &info.name;
54 let enumset = &paths.enumset;
55 let typed_enumset = &paths.typed_enumset;
56 let core = &paths.core;
57 let internal = &paths.internal;
58 let serde = &paths.serde;
59 let is_uninhabited = info.variants.is_empty();
60
61 let repr = match info.internal_repr() {
62 InternalRepr::U8 => quote! { u8 },
63 InternalRepr::U16 => quote! { u16 },
64 InternalRepr::U32 => quote! { u32 },
65 InternalRepr::U64 => quote! { u64 },
66 InternalRepr::U128 => quote! { u128 },
67 InternalRepr::Array(size) => quote! { #internal::ArrayRepr<{ #size }> },
68 };
69 let variant_map = info.variant_map();
70 let all_variants = match info.internal_repr() {
71 InternalRepr::U8 | InternalRepr::U16 | InternalRepr::U32 | InternalRepr::U64 => {
72 let lit = Literal::u64_unsuffixed(variant_map[0]);
73 quote! { #lit }
74 }
75 InternalRepr::U128 => {
76 let lit = Literal::u128_unsuffixed(
77 variant_map[0] as u128 | variant_map.get(1).map_or(0, |x| (*x as u128) << 64),
78 );
79 quote! { #lit }
80 }
81 InternalRepr::Array(size) => {
82 let mut new = Vec::new();
83 for i in 0..size {
84 new.push(Literal::u64_unsuffixed(*variant_map.get(i).unwrap_or(&0)));
85 }
86 quote! { #internal::ArrayRepr::<{ #size }>([#(#new,)*]) }
87 }
88 };
89
90 //
91 // Implements operator overloading on the enum type.
92 //
93 let impl_ops = if info.no_ops {
94 quote! {}
95 } else {
96 quote! {
97 #[automatically_derived]
98 impl<O: Into<#typed_enumset>> #core::ops::Sub<O> for #name {
99 type Output = #typed_enumset;
100 fn sub(self, other: O) -> Self::Output {
101 #enumset::EnumSet::only(self) - other.into()
102 }
103 }
104 #[automatically_derived]
105 impl<O: Into<#typed_enumset>> #core::ops::BitAnd<O> for #name {
106 type Output = #typed_enumset;
107 fn bitand(self, other: O) -> Self::Output {
108 #enumset::EnumSet::only(self) & other.into()

Callers 1

derive_enum_set_type_0Function · 0.85

Calls 12

newFunction · 0.85
create_enum_conversionsFunction · 0.85
create_enum_const_opersFunction · 0.85
is_emptyMethod · 0.80
internal_reprMethod · 0.80
variant_mapMethod · 0.80
serde_reprMethod · 0.80
extendMethod · 0.80
enum_reprMethod · 0.80
bit_widthMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected