Skip to content

Commit 2d0cdbd

Browse files
committed
[bindings] Un-Box Tuple mapping
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.
1 parent 65884ff commit 2d0cdbd

File tree

2 files changed

+17
-47
lines changed

2 files changed

+17
-47
lines changed

c-bindings-gen/src/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,11 +1697,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
16971697
assert!(self.write_c_type_intern(w, gen, None, false, false, false));
16981698
}
16991699
writeln!(w, ") -> {} {{", mangled_container).unwrap();
1700-
writeln!(w, "\t{} {{", mangled_container).unwrap();
1700+
write!(w, "\t{} {{ ", mangled_container).unwrap();
17011701
for idx in 0..args.len() {
1702-
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();
1702+
write!(w, "{}, ", ('a' as u8 + idx as u8) as char).unwrap();
17031703
}
1704-
writeln!(w, "\t}}\n}}\n").unwrap();
1704+
writeln!(w, "}}\n}}\n").unwrap();
17051705
} else {
17061706
writeln!(w, "").unwrap();
17071707
}

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

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -326,83 +326,53 @@ impl<T: Clone> Clone for CVecTempl<T> {
326326

327327
#[repr(C)]
328328
pub struct C2TupleTempl<A, B> {
329-
pub a: *mut A,
330-
pub b: *mut B,
329+
pub a: A,
330+
pub b: B,
331331
}
332332
impl<A, B> From<(A, B)> for C2TupleTempl<A, B> {
333333
fn from(tup: (A, B)) -> Self {
334334
Self {
335-
a: Box::into_raw(Box::new(tup.0)),
336-
b: Box::into_raw(Box::new(tup.1)),
335+
a: tup.0,
336+
b: tup.1,
337337
}
338338
}
339339
}
340340
impl<A, B> C2TupleTempl<A, B> {
341341
pub(crate) fn to_rust(mut self) -> (A, B) {
342-
let res = (unsafe { *Box::from_raw(self.a) }, unsafe { *Box::from_raw(self.b) });
343-
self.a = std::ptr::null_mut();
344-
self.b = std::ptr::null_mut();
345-
res
342+
(self.a, self.b)
346343
}
347344
}
348345
pub extern "C" fn C2TupleTempl_free<A, B>(_res: C2TupleTempl<A, B>) { }
349-
impl<A, B> Drop for C2TupleTempl<A, B> {
350-
fn drop(&mut self) {
351-
if !self.a.is_null() {
352-
unsafe { Box::from_raw(self.a) };
353-
}
354-
if !self.b.is_null() {
355-
unsafe { Box::from_raw(self.b) };
356-
}
357-
}
358-
}
359346
impl <A: Clone, B: Clone> Clone for C2TupleTempl<A, B> {
360347
fn clone(&self) -> Self {
361348
Self {
362-
a: Box::into_raw(Box::new(unsafe { &*self.a }.clone())),
363-
b: Box::into_raw(Box::new(unsafe { &*self.b }.clone()))
349+
a: self.a.clone(),
350+
b: self.b.clone()
364351
}
365352
}
366353
}
367354

368355
#[repr(C)]
369356
pub struct C3TupleTempl<A, B, C> {
370-
pub a: *mut A,
371-
pub b: *mut B,
372-
pub c: *mut C,
357+
pub a: A,
358+
pub b: B,
359+
pub c: C,
373360
}
374361
impl<A, B, C> From<(A, B, C)> for C3TupleTempl<A, B, C> {
375362
fn from(tup: (A, B, C)) -> Self {
376363
Self {
377-
a: Box::into_raw(Box::new(tup.0)),
378-
b: Box::into_raw(Box::new(tup.1)),
379-
c: Box::into_raw(Box::new(tup.2)),
364+
a: tup.0,
365+
b: tup.1,
366+
c: tup.2,
380367
}
381368
}
382369
}
383370
impl<A, B, C> C3TupleTempl<A, B, C> {
384371
pub(crate) fn to_rust(mut self) -> (A, B, C) {
385-
let res = (unsafe { *Box::from_raw(self.a) }, unsafe { *Box::from_raw(self.b) }, unsafe { *Box::from_raw(self.c) });
386-
self.a = std::ptr::null_mut();
387-
self.b = std::ptr::null_mut();
388-
self.c = std::ptr::null_mut();
389-
res
372+
(self.a, self.b, self.c)
390373
}
391374
}
392375
pub extern "C" fn C3TupleTempl_free<A, B, C>(_res: C3TupleTempl<A, B, C>) { }
393-
impl<A, B, C> Drop for C3TupleTempl<A, B, C> {
394-
fn drop(&mut self) {
395-
if !self.a.is_null() {
396-
unsafe { Box::from_raw(self.a) };
397-
}
398-
if !self.b.is_null() {
399-
unsafe { Box::from_raw(self.b) };
400-
}
401-
if !self.c.is_null() {
402-
unsafe { Box::from_raw(self.c) };
403-
}
404-
}
405-
}
406376

407377
/// Utility to make it easy to set a pointer to null and get its original value in line.
408378
pub(crate) trait TakePointer<T> {

0 commit comments

Comments
 (0)