for field in fields { // It's safe to unwrap because struct fields always have an identifier let field_id = field.ident.as_ref(); // generates `x: self.x.not()` let expr = quote! { #field_id: self.#field_id.#method_ident() };
exprs.push(expr)
}
quote! { #input_type{#(#exprs),*} }
}
fn enum_output_type_and_content(
input: &DeriveInput,
data_enum: &DataEnum,
method_ident: &Ident,
) -> (TokenStream, TokenStream) { let input_type = &input.ident; let (_, ty_generics, _) = input.generics.split_for_impl(); letmut matches = vec![]; letmut method_iter = iter::repeat(method_ident); // If the enum contains unit types that means it can error. let has_unit_type = data_enum.variants.iter().any(|v| v.fields == Fields::Unit);
for variant in &data_enum.variants { let subtype = &variant.ident; let subtype = quote! { #input_type::#subtype };
match variant.fields {
Fields::Unnamed(ref fields) => { // The pattern that is outputted should look like this: // (Subtype(vars)) => Ok(TypePath(exprs)) let size = unnamed_to_vec(fields).len(); let vars: &Vec<_> =
&(0..size).map(|i| format_ident!("__{i}")).collect(); let method_iter = method_iter.by_ref(); letmut body = quote! { #subtype(#(#vars.#method_iter()),*) }; if has_unit_type {
body = quote! { ::core::result::Result::Ok(#body) }
} let matcher = quote! { #subtype(#(#vars),*) => { #body
}
};
matches.push(matcher);
}
Fields::Named(ref fields) => { // The pattern that is outputted should look like this: // (Subtype{a: __l_a, ...} => { // Ok(Subtype{a: __l_a.neg(__r_a), ...}) // } let field_vec = named_to_vec(fields); let size = field_vec.len(); let field_names: &Vec<_> = &field_vec
.iter()
.map(|f| f.ident.as_ref().unwrap())
.collect(); let vars: &Vec<_> =
&(0..size).map(|i| format_ident!("__{i}")).collect(); let method_iter = method_iter.by_ref(); letmut body = quote! { #subtype{#(#field_names: #vars.#method_iter()),*}
}; if has_unit_type {
body = quote! { ::core::result::Result::Ok(#body) }
} let matcher = quote! { #subtype{#(#field_names: #vars),*} => { #body
}
};
matches.push(matcher);
}
Fields::Unit => { let operation_name = method_ident.to_string();
matches.push(quote! { #subtype => ::core::result::Result::Err(
::derive_more::ops::UnitError::new(#operation_name)
)
});
}
}
}
let body = quote! { matchself { #(#matches),*
}
};
let output_type = if has_unit_type {
quote! { ::core::result::Result<#input_type#ty_generics, ::derive_more::ops::UnitError> }
} else {
quote! { #input_type#ty_generics }
};
(output_type, body)
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-23)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.