Skip to content

Commit 6190617

Browse files
committed
codegen: Add integration tests for destructors.
1 parent 0045796 commit 6190617

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

bindgen-integration/cpp/Test.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ Test::Test(double foo)
2121
, m_double(foo)
2222
{}
2323

24+
AutoRestoreBool::AutoRestoreBool(bool* ptr)
25+
: m_ptr(ptr)
26+
, m_value(*ptr)
27+
{}
28+
29+
AutoRestoreBool::~AutoRestoreBool() {
30+
*m_ptr = m_value;
31+
}
32+
2433
namespace bitfields {
2534

2635
bool
@@ -47,4 +56,4 @@ Third::assert(int first, bool second, ItemKind third)
4756
kind == third;
4857
}
4958

50-
}
59+
} // namespace bitfields

bindgen-integration/cpp/Test.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ struct Third {
6464
};
6565

6666
} // namespace bitfields
67+
68+
struct AutoRestoreBool {
69+
bool* m_ptr;
70+
bool m_value;
71+
72+
AutoRestoreBool(bool*);
73+
~AutoRestoreBool();
74+
};

bindgen-integration/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,21 @@ fn test_bitfields_third() {
101101
bindings::bitfields::ItemKind::ITEM_KIND_TRES)
102102
});
103103
}
104+
105+
impl Drop for bindings::AutoRestoreBool {
106+
fn drop(&mut self) {
107+
unsafe { bindings::AutoRestoreBool::destruct(self) }
108+
}
109+
}
110+
111+
#[test]
112+
fn test_destructors() {
113+
let mut v = true;
114+
115+
{
116+
let auto_restore = unsafe { bindings::AutoRestoreBool::new(&mut v) };
117+
v = false;
118+
}
119+
120+
assert!(v, "Should've been restored when going out of scope");
121+
}

0 commit comments

Comments
 (0)