MCPcopy Index your code
hub / github.com/SoftbearStudios/bitcode / enum_impl

Method enum_impl

bitcode_derive/src/encode.rs:100–219  ·  view source on GitHub ↗
(
        self,
        variant_count: usize,
        pattern: impl Fn(usize) -> TokenStream,
        inner: impl Fn(Self, usize) -> TokenStream,
    )

Source from the content-addressed store, hash-verified

98 }
99
100 fn enum_impl(
101 self,
102 variant_count: usize,
103 pattern: impl Fn(usize) -> TokenStream,
104 inner: impl Fn(Self, usize) -> TokenStream,
105 ) -> TokenStream {
106 // if variant_count is 0 or 1 variants don't have to be encoded.
107 let encode_variants = variant_count > 1;
108 match self {
109 Self::Type => {
110 let variants = encode_variants
111 .then(|| {
112 let private = private();
113 quote! { variants: #private::VariantEncoder<#variant_count>, }
114 })
115 .unwrap_or_default();
116 let inners: TokenStream = (0..variant_count).map(|i| inner(self, i)).collect();
117 quote! {
118 #variants
119 #inners
120 }
121 }
122 Self::Default => {
123 let variants = encode_variants
124 .then(|| quote! { variants: Default::default(), })
125 .unwrap_or_default();
126 let inners: TokenStream = (0..variant_count).map(|i| inner(self, i)).collect();
127 quote! {
128 #variants
129 #inners
130 }
131 }
132 Self::Encode => {
133 let variants = encode_variants
134 .then(|| {
135 let variants: TokenStream = (0..variant_count)
136 .map(|i| {
137 let pattern = pattern(i);
138 let i = variant_index(i);
139 quote! {
140 #pattern => #i,
141 }
142 })
143 .collect();
144 quote! {
145 #[allow(unused_variables)]
146 self.variants.encode(&match v {
147 #variants
148 });
149 }
150 })
151 .unwrap_or_default();
152 let inners: TokenStream = (0..variant_count)
153 .map(|i| {
154 // We don't know the exact number of this variant since there is more than
155 // one, so we have to reserve one at a time.
156 let reserve = encode_variants
157 .then(|| {

Callers 1

deriveMethod · 0.45

Calls 3

privateFunction · 0.85
variant_indexFunction · 0.85
collectMethod · 0.45

Tested by

no test coverage detected