Skip to content

Commit 4b982b5

Browse files
committed
[bindings] Un-Box Tuple mapping
I'm honestly not quite sure why we'd put all the tuple elements behind pointers, it generates a ton of needless pointer indirection. Luckily, its trivial to remove, so let's just do it.
1 parent 9418cb2 commit 4b982b5

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
@@ -1702,11 +1702,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
17021702
assert!(self.write_c_type_intern(w, gen, None, false, false, false));
17031703
}
17041704
writeln!(w, ") -> {} {{", mangled_container).unwrap();
1705-
writeln!(w, "\t{} {{", mangled_container).unwrap();
1705+
write!(w, "\t{} {{ ", mangled_container).unwrap();
17061706
for idx in 0..args.len() {
1707-
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();
1707+
write!(w, "{}, ", ('a' as u8 + idx as u8) as char).unwrap();
17081708
}
1709-
writeln!(w, "\t}}\n}}\n").unwrap();
1709+
writeln!(w, "}}\n}}\n").unwrap();
17101710
} else {
17111711
writeln!(w, "").unwrap();
17121712
}

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)