pgrx_sql_entity_graph/
lifetimes.rs

1//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
2//LICENSE
3//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
4//LICENSE
5//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <contact@pgcentral.org>
6//LICENSE
7//LICENSE All rights reserved.
8//LICENSE
9//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
10
11pub fn anonymize_lifetimes_in_type_path(value: syn::TypePath) -> syn::TypePath {
12    let mut ty = syn::Type::Path(value);
13    anonymize_lifetimes(&mut ty);
14    match ty {
15        syn::Type::Path(type_path) => type_path,
16
17        // shouldn't happen
18        _ => panic!("not a TypePath"),
19    }
20}
21
22pub fn anonymize_lifetimes(value: &mut syn::Type) {
23    match value {
24        syn::Type::Path(syn::TypePath { path: syn::Path { segments, .. }, .. }) => segments
25            .iter_mut()
26            .filter_map(|segment| match &mut segment.arguments {
27                syn::PathArguments::AngleBracketed(bracketed) => Some(bracketed),
28                _ => None,
29            })
30            .flat_map(|bracketed| &mut bracketed.args)
31            .for_each(|arg| match arg {
32                // rename lifetimes to the anonymous lifetime
33                syn::GenericArgument::Lifetime(lifetime) => {
34                    lifetime.ident = syn::Ident::new("_", lifetime.ident.span());
35                }
36                // recurse
37                syn::GenericArgument::Type(ty) => anonymize_lifetimes(ty),
38                syn::GenericArgument::AssocType(binding) => anonymize_lifetimes(&mut binding.ty),
39                syn::GenericArgument::Constraint(constraint) => {
40                    constraint.bounds.iter_mut().for_each(|bound| {
41                        if let syn::TypeParamBound::Lifetime(lifetime) = bound {
42                            lifetime.ident = syn::Ident::new("_", lifetime.ident.span())
43                        }
44                    })
45                }
46                // nothing to do otherwise
47                _ => {}
48            }),
49
50        syn::Type::Reference(type_ref) => {
51            if let Some(lifetime) = type_ref.lifetime.as_mut() {
52                lifetime.ident = syn::Ident::new("_", lifetime.ident.span());
53            }
54        }
55        syn::Type::Tuple(type_tuple) => type_tuple.elems.iter_mut().for_each(anonymize_lifetimes),
56        _ => {}
57    }
58}