]> git.bitcoin.ninja Git - rust-lightning/commitdiff
[bindings] Un-Box Tuple mapping
authorMatt Corallo <git@bluematt.me>
Thu, 8 Oct 2020 23:35:50 +0000 (19:35 -0400)
committerMatt Corallo <git@bluematt.me>
Wed, 21 Oct 2020 18:54:51 +0000 (14:54 -0400)
Because the C++ wrappers require being able to memset(0) the C
structs to skip free(), we'd previously mapped tuples with two
pointer indirections. However, because all other types already
support memset(0)'ing to disable free() logic, we can skip the
pointer indirections and the behavior is still correct.

c-bindings-gen/src/types.rs
lightning-c-bindings/src/c_types/mod.rs

index 32036180d69d8282236fa3e8002696698fbd0d4c..3fc35e8b2558f8de6f7da2d4d017664a8b3390d1 100644 (file)
@@ -1697,11 +1697,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                assert!(self.write_c_type_intern(w, gen, None, false, false, false));
                        }
                        writeln!(w, ") -> {} {{", mangled_container).unwrap();
-                       writeln!(w, "\t{} {{", mangled_container).unwrap();
+                       write!(w, "\t{} {{ ", mangled_container).unwrap();
                        for idx in 0..args.len() {
-                               writeln!(w, "\t\t{}: Box::into_raw(Box::new({})),", ('a' as u8 + idx as u8) as char, ('a' as u8 + idx as u8) as char).unwrap();
+                               write!(w, "{}, ", ('a' as u8 + idx as u8) as char).unwrap();
                        }
-                       writeln!(w, "\t}}\n}}\n").unwrap();
+                       writeln!(w, "}}\n}}\n").unwrap();
                } else {
                        writeln!(w, "").unwrap();
                }
index 813e401b8e7e2b5f567b8791117c815c02aa0d61..d14fe6fc792df937f914aae656e8b61dfd52e773 100644 (file)
@@ -326,83 +326,53 @@ impl<T: Clone> Clone for CVecTempl<T> {
 
 #[repr(C)]
 pub struct C2TupleTempl<A, B> {
-       pub a: *mut A,
-       pub b: *mut B,
+       pub a: A,
+       pub b: B,
 }
 impl<A, B> From<(A, B)> for C2TupleTempl<A, B> {
        fn from(tup: (A, B)) -> Self {
                Self {
-                       a: Box::into_raw(Box::new(tup.0)),
-                       b: Box::into_raw(Box::new(tup.1)),
+                       a: tup.0,
+                       b: tup.1,
                }
        }
 }
 impl<A, B> C2TupleTempl<A, B> {
        pub(crate) fn to_rust(mut self) -> (A, B) {
-               let res = (unsafe { *Box::from_raw(self.a) }, unsafe { *Box::from_raw(self.b) });
-               self.a = std::ptr::null_mut();
-               self.b = std::ptr::null_mut();
-               res
+               (self.a, self.b)
        }
 }
 pub extern "C" fn C2TupleTempl_free<A, B>(_res: C2TupleTempl<A, B>) { }
-impl<A, B> Drop for C2TupleTempl<A, B> {
-       fn drop(&mut self) {
-               if !self.a.is_null() {
-                       unsafe { Box::from_raw(self.a) };
-               }
-               if !self.b.is_null() {
-                       unsafe { Box::from_raw(self.b) };
-               }
-       }
-}
 impl <A: Clone, B: Clone> Clone for C2TupleTempl<A, B> {
        fn clone(&self) -> Self {
                Self {
-                       a: Box::into_raw(Box::new(unsafe { &*self.a }.clone())),
-                       b: Box::into_raw(Box::new(unsafe { &*self.b }.clone()))
+                       a: self.a.clone(),
+                       b: self.b.clone()
                }
        }
 }
 
 #[repr(C)]
 pub struct C3TupleTempl<A, B, C> {
-       pub a: *mut A,
-       pub b: *mut B,
-       pub c: *mut C,
+       pub a: A,
+       pub b: B,
+       pub c: C,
 }
 impl<A, B, C> From<(A, B, C)> for C3TupleTempl<A, B, C> {
        fn from(tup: (A, B, C)) -> Self {
                Self {
-                       a: Box::into_raw(Box::new(tup.0)),
-                       b: Box::into_raw(Box::new(tup.1)),
-                       c: Box::into_raw(Box::new(tup.2)),
+                       a: tup.0,
+                       b: tup.1,
+                       c: tup.2,
                }
        }
 }
 impl<A, B, C> C3TupleTempl<A, B, C> {
        pub(crate) fn to_rust(mut self) -> (A, B, C) {
-               let res = (unsafe { *Box::from_raw(self.a) }, unsafe { *Box::from_raw(self.b) }, unsafe { *Box::from_raw(self.c) });
-               self.a = std::ptr::null_mut();
-               self.b = std::ptr::null_mut();
-               self.c = std::ptr::null_mut();
-               res
+               (self.a, self.b, self.c)
        }
 }
 pub extern "C" fn C3TupleTempl_free<A, B, C>(_res: C3TupleTempl<A, B, C>) { }
-impl<A, B, C> Drop for C3TupleTempl<A, B, C> {
-       fn drop(&mut self) {
-               if !self.a.is_null() {
-                       unsafe { Box::from_raw(self.a) };
-               }
-               if !self.b.is_null() {
-                       unsafe { Box::from_raw(self.b) };
-               }
-               if !self.c.is_null() {
-                       unsafe { Box::from_raw(self.c) };
-               }
-       }
-}
 
 /// Utility to make it easy to set a pointer to null and get its original value in line.
 pub(crate) trait TakePointer<T> {