MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / erase_generic_param

Function erase_generic_param

src/expr-derive-impl/src/sqlfunc.rs:733–765  ·  view source on GitHub ↗

Replaces occurrences of a generic type parameter with `Datum<'a>` in a type. Used to convert types from the user's generic function signature into concrete types for the generated trait impl's associated types, where `T` is not in scope.

(ty: &syn::Type, generic_name: &Ident)

Source from the content-addressed store, hash-verified

731/// Used to convert types from the user's generic function signature into concrete
732/// types for the generated trait impl's associated types, where `T` is not in scope.
733fn erase_generic_param(ty: &syn::Type, generic_name: &Ident) -> syn::Type {
734 match ty {
735 syn::Type::Path(type_path) => {
736 if type_path.path.is_ident(generic_name) {
737 return syn::parse_quote!(Datum<'a>);
738 }
739 let mut type_path = type_path.clone();
740 for segment in &mut type_path.path.segments {
741 if let syn::PathArguments::AngleBracketed(args) = &mut segment.arguments {
742 for arg in &mut args.args {
743 if let syn::GenericArgument::Type(inner) = arg {
744 *inner = erase_generic_param(inner, generic_name);
745 }
746 }
747 }
748 }
749 syn::Type::Path(type_path)
750 }
751 syn::Type::Reference(r) => {
752 let elem = Box::new(erase_generic_param(&r.elem, generic_name));
753 syn::Type::Reference(syn::TypeReference { elem, ..r.clone() })
754 }
755 syn::Type::Tuple(t) => {
756 let elems = t
757 .elems
758 .iter()
759 .map(|e| erase_generic_param(e, generic_name))
760 .collect();
761 syn::Type::Tuple(syn::TypeTuple { elems, ..t.clone() })
762 }
763 _ => ty.clone(),
764 }
765}
766
767/// Erases all generic type parameters from a type, replacing each with `Datum<'a>`.
768fn erase_all_generic_params(ty: &syn::Type, generic_names: &[Ident]) -> syn::Type {

Callers 2

classify_generic_usageFunction · 0.85
erase_all_generic_paramsFunction · 0.85

Calls 4

cloneMethod · 0.45
collectMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected