]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Handle `Vec`s resolved as `alloc::vec::Vec`
authorMatt Corallo <git@bluematt.me>
Tue, 4 Jun 2024 17:47:02 +0000 (17:47 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 4 Jun 2024 17:47:02 +0000 (17:47 +0000)
Up until now we'd always relied on `Vec`s being imported through a
prelude, but LDK sometimes forgets to keep doing that. Thus, here
we move to also accepting `alloc::vec::Vec` by mapping it to `Vec`
during name resolution.

c-bindings-gen/src/types.rs

index 0da41e60ec5a8c148ee873da61c00eab5ff81c80..877331e88a6e2eb04133a97fafd6d0c466d59ed0 100644 (file)
@@ -656,7 +656,7 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr
                        }
                }
 
-               if p.leading_colon.is_some() {
+               let result = if p.leading_colon.is_some() {
                        let mut res: String = p.segments.iter().enumerate().map(|(idx, seg)| {
                                format!("{}{}", if idx == 0 { "" } else { "::" }, seg.ident)
                        }).collect();
@@ -667,11 +667,10 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr
                        Some(res)
                } else if let Some(id) = p.get_ident() {
                        self.maybe_resolve_ident(id)
+               } else if p.segments.len() == 1 {
+                       let seg = p.segments.iter().next().unwrap();
+                       self.maybe_resolve_ident(&seg.ident)
                } else {
-                       if p.segments.len() == 1 {
-                               let seg = p.segments.iter().next().unwrap();
-                               return self.maybe_resolve_ident(&seg.ident);
-                       }
                        let mut seg_iter = p.segments.iter();
                        let first_seg = seg_iter.next().unwrap();
                        let remaining: String = seg_iter.map(|seg| {
@@ -693,6 +692,13 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr
                        } else if self.library.modules.get(&format!("{}::{}", self.module_path, first_seg.ident)).is_some() {
                                Some(format!("{}::{}{}", self.module_path, first_seg.ident, remaining))
                        } else { None }
+               };
+               // We're only set up to handle `Vec` imported via the prelude, but its often imported
+               // via the alloc stdlib crate, so map it here.
+               if result.as_ref().map(|s| s.as_str()) == Some("alloc::vec::Vec") {
+                       Some("Vec".to_string())
+               } else {
+                       result
                }
        }