From: Matt Corallo Date: Wed, 22 Sep 2021 06:04:50 +0000 (+0000) Subject: Use `GenericTypes`'s type resolver instead of re-resolving X-Git-Tag: v0.0.101.1~1^2~6 X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-c-bindings;a=commitdiff_plain;h=19ebe5e21418685c3393e788a4af576830b983e7 Use `GenericTypes`'s type resolver instead of re-resolving When we resolve a type with `GenericTypes::maybe_resolve_path` it returns a resolved-path string as well as a syn::Path. When resolving we'd previously then re-resolve the Path with the current type/import information, instead of just accepting what was already resolved. Simply accepting what was already resolved not only simplifies a few cases, but also allows us to clean up where the "crate::" prefix is printed, keeping "crate::" prefixes out of all type resolution. --- diff --git a/c-bindings-gen/src/blocks.rs b/c-bindings-gen/src/blocks.rs index 9c32ef1..fab69f6 100644 --- a/c-bindings-gen/src/blocks.rs +++ b/c-bindings-gen/src/blocks.rs @@ -737,7 +737,7 @@ pub fn maybe_write_generics(w: &mut W, generics: &syn::Generi for bound in type_param.bounds.iter() { if let syn::TypeParamBound::Trait(trait_bound) = bound { assert_simple_bound(&trait_bound); - write!(w, "{}{}", if idx != 0 { ", " } else { "" }, gen_types.maybe_resolve_ident(&type_param.ident).unwrap()).unwrap(); + write!(w, "{}crate::{}", if idx != 0 { ", " } else { "" }, gen_types.maybe_resolve_ident(&type_param.ident).unwrap()).unwrap(); if printed_param { unimplemented!("Can't print generic params that have multiple non-lifetime bounds"); } diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index b4b46a8..50800bb 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -205,13 +205,12 @@ impl<'a, 'p: 'a> GenericTypes<'a, 'p> { if path_matches_nongeneric(&trait_bound.path, &["core", "clone", "Clone"]) { continue; } assert_simple_bound(&trait_bound); - if let Some(mut path) = types.maybe_resolve_path(&trait_bound.path, None) { + if let Some(path) = types.maybe_resolve_path(&trait_bound.path, None) { if types.skip_path(&path) { continue; } if path == "Sized" { continue; } if non_lifetimes_processed { return false; } non_lifetimes_processed = true; let new_ident = if path != "std::ops::Deref" && path != "core::ops::Deref" { - path = "crate::".to_string() + &path; Some(&trait_bound.path) } else if trait_bound.path.segments.len() == 1 { // If we're templated on Deref, store @@ -267,7 +266,7 @@ impl<'a, 'p: 'a> GenericTypes<'a, 'p> { if non_lifetimes_processed { return false; } non_lifetimes_processed = true; assert_simple_bound(&trait_bound); - *gen = ("crate::".to_string() + &types.resolve_path(&trait_bound.path, None), + *gen = (types.resolve_path(&trait_bound.path, None), Some(&trait_bound.path)); } } @@ -292,14 +291,13 @@ impl<'a, 'p: 'a> GenericTypes<'a, 'p> { match bounds_iter.next().unwrap() { syn::TypeParamBound::Trait(tr) => { assert_simple_bound(&tr); - if let Some(mut path) = types.maybe_resolve_path(&tr.path, None) { + if let Some(path) = types.maybe_resolve_path(&tr.path, None) { if types.skip_path(&path) { continue; } // In general we handle Deref as if it were just X (and // implement Deref for relevant types). We don't // bother to implement it for associated types, however, so we just // ignore such bounds. let new_ident = if path != "std::ops::Deref" && path != "core::ops::Deref" { - path = "crate::".to_string() + &path; Some(&tr.path) } else { None }; self.typed_generics.insert(&t.ident, (path, new_ident)); @@ -581,12 +579,12 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr } else { None } } - pub fn maybe_resolve_path(&self, p_arg: &syn::Path, generics: Option<&GenericTypes>) -> Option { - let p = if let Some(gen_types) = generics { - if let Some((_, synpath)) = gen_types.maybe_resolve_path(p_arg) { - synpath - } else { p_arg } - } else { p_arg }; + pub fn maybe_resolve_path(&self, p: &syn::Path, generics: Option<&GenericTypes>) -> Option { + if let Some(gen_types) = generics { + if let Some((resp, _)) = gen_types.maybe_resolve_path(p) { + return Some(resp.clone()); + } + } if p.leading_colon.is_some() { let mut res: String = p.segments.iter().enumerate().map(|(idx, seg)| {