[bindings] Keep track of all types which derive[Clone]
[rust-lightning] / c-bindings-gen / src / types.rs
index 628d263bf9a9775939a1c88ca3ec57e96840e362..22f1d9e5a8596fbbffd9728ecd88d0d5fa86f6ac 100644 (file)
@@ -1,4 +1,4 @@
-use std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
 use std::fs::File;
 use std::io::Write;
 use std::hash;
@@ -39,6 +39,30 @@ pub fn single_ident_generic_path_to_ident(p: &syn::Path) -> Option<&syn::Ident>
        } else { None }
 }
 
+pub fn attrs_derives_clone(attrs: &[syn::Attribute]) -> bool {
+       for attr in attrs.iter() {
+               let tokens_clone = attr.tokens.clone();
+               let mut token_iter = tokens_clone.into_iter();
+               if let Some(token) = token_iter.next() {
+                       match token {
+                               TokenTree::Group(g) => {
+                                       if format!("{}", single_ident_generic_path_to_ident(&attr.path).unwrap()) == "derive" {
+                                               for id in g.stream().into_iter() {
+                                                       if let TokenTree::Ident(i) = id {
+                                                               if i == "Clone" {
+                                                                       return true;
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               },
+                               _ => {},
+                       }
+               }
+       }
+       false
+}
+
 #[derive(Debug, PartialEq)]
 pub enum ExportStatus {
        Export,
@@ -293,6 +317,8 @@ pub struct CrateTypes<'a> {
        /// The output file for any created template container types, written to as we find new
        /// template containers which need to be defined.
        pub template_file: &'a mut File,
+       /// Set of containers which are clonable
+       pub clonable_types: HashSet<String>,
 }
 
 /// A struct which tracks resolving rust types into C-mapped equivalents, exists for one specific
@@ -368,6 +394,16 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        _ => false,
                }
        }
+       pub fn is_clonable(&self, ty: &str) -> bool {
+               if self.crate_types.clonable_types.contains(ty) { return true; }
+               if self.is_primitive(ty) { return true; }
+               match ty {
+                       "()" => true,
+                       "crate::c_types::Signature" => true,
+                       "crate::c_types::TxOut" => true,
+                       _ => false,
+               }
+       }
        /// Gets the C-mapped type for types which are outside of the crate, or which are manually
        /// ignored by for some reason need mapping anyway.
        fn c_type_from_path<'b>(&self, full_path: &'b str, is_ref: bool, ptr_for_ref: bool) -> Option<&'b str> {