Skip to content

Commit 3594e93

Browse files
author
Joe Ellis
committed
Add tests for dynamic binding generation
1 parent a086e56 commit 3594e93

11 files changed

+491
-1
lines changed

tests/expectations/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ authors = [
1010

1111
[dependencies]
1212
objc = "0.2"
13-
block = "0.1"
13+
block = "0.1"
14+
libloading = "0.6.2"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
extern crate libloading;
9+
pub struct TestLib {
10+
__library: ::libloading::Library,
11+
foo: Result<
12+
unsafe extern "C" fn(
13+
x: ::std::os::raw::c_int,
14+
y: ::std::os::raw::c_int,
15+
) -> ::std::os::raw::c_int,
16+
::libloading::Error,
17+
>,
18+
bar: Result<
19+
unsafe extern "C" fn(
20+
x: *mut ::std::os::raw::c_void,
21+
) -> ::std::os::raw::c_int,
22+
::libloading::Error,
23+
>,
24+
baz: Result<
25+
unsafe extern "C" fn() -> ::std::os::raw::c_int,
26+
::libloading::Error,
27+
>,
28+
}
29+
impl TestLib {
30+
pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error>
31+
where
32+
P: AsRef<::std::ffi::OsStr>,
33+
{
34+
let __library = ::libloading::Library::new(path)?;
35+
let foo = __library.get("foo".as_bytes()).map(|sym| *sym);
36+
let bar = __library.get("bar".as_bytes()).map(|sym| *sym);
37+
let baz = __library.get("baz".as_bytes()).map(|sym| *sym);
38+
Ok(TestLib {
39+
__library: __library,
40+
foo,
41+
bar,
42+
baz,
43+
})
44+
}
45+
pub fn can_call(&self) -> CheckTestLib {
46+
CheckTestLib { __library: self }
47+
}
48+
pub unsafe fn foo(
49+
&self,
50+
x: ::std::os::raw::c_int,
51+
y: ::std::os::raw::c_int,
52+
) -> ::std::os::raw::c_int {
53+
let sym = self.foo.as_ref().expect("Expected function, got error.");
54+
(sym)(x, y)
55+
}
56+
pub unsafe fn bar(
57+
&self,
58+
x: *mut ::std::os::raw::c_void,
59+
) -> ::std::os::raw::c_int {
60+
let sym = self.bar.as_ref().expect("Expected function, got error.");
61+
(sym)(x)
62+
}
63+
pub unsafe fn baz(&self) -> ::std::os::raw::c_int {
64+
let sym = self.baz.as_ref().expect("Expected function, got error.");
65+
(sym)()
66+
}
67+
}
68+
pub struct CheckTestLib<'a> {
69+
__library: &'a TestLib,
70+
}
71+
impl<'a> CheckTestLib<'a> {
72+
pub fn foo(&self) -> Result<(), &'a ::libloading::Error> {
73+
self.__library.foo.as_ref().map(|_| ())
74+
}
75+
pub fn bar(&self) -> Result<(), &'a ::libloading::Error> {
76+
self.__library.bar.as_ref().map(|_| ())
77+
}
78+
pub fn baz(&self) -> Result<(), &'a ::libloading::Error> {
79+
self.__library.baz.as_ref().map(|_| ())
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
extern crate libloading;
9+
pub struct TestLib {
10+
__library: ::libloading::Library,
11+
foo: Result<
12+
unsafe extern "C" fn(x: ::std::os::raw::c_int) -> ::std::os::raw::c_int,
13+
::libloading::Error,
14+
>,
15+
foo1: Result<unsafe extern "C" fn(x: f32) -> f32, ::libloading::Error>,
16+
}
17+
impl TestLib {
18+
pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error>
19+
where
20+
P: AsRef<::std::ffi::OsStr>,
21+
{
22+
let __library = ::libloading::Library::new(path)?;
23+
let foo = __library.get("foo".as_bytes()).map(|sym| *sym);
24+
let foo1 = __library.get("foo1".as_bytes()).map(|sym| *sym);
25+
Ok(TestLib {
26+
__library: __library,
27+
foo,
28+
foo1,
29+
})
30+
}
31+
pub fn can_call(&self) -> CheckTestLib {
32+
CheckTestLib { __library: self }
33+
}
34+
pub unsafe fn foo(
35+
&self,
36+
x: ::std::os::raw::c_int,
37+
) -> ::std::os::raw::c_int {
38+
let sym = self.foo.as_ref().expect("Expected function, got error.");
39+
(sym)(x)
40+
}
41+
pub unsafe fn foo1(&self, x: f32) -> f32 {
42+
let sym = self.foo1.as_ref().expect("Expected function, got error.");
43+
(sym)(x)
44+
}
45+
}
46+
pub struct CheckTestLib<'a> {
47+
__library: &'a TestLib,
48+
}
49+
impl<'a> CheckTestLib<'a> {
50+
pub fn foo(&self) -> Result<(), &'a ::libloading::Error> {
51+
self.__library.foo.as_ref().map(|_| ())
52+
}
53+
pub fn foo1(&self) -> Result<(), &'a ::libloading::Error> {
54+
self.__library.foo1.as_ref().map(|_| ())
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
#[repr(C)]
9+
#[derive(Debug, Default, Copy, Clone)]
10+
pub struct X {
11+
pub _x: ::std::os::raw::c_int,
12+
}
13+
#[test]
14+
fn bindgen_test_layout_X() {
15+
assert_eq!(
16+
::std::mem::size_of::<X>(),
17+
4usize,
18+
concat!("Size of: ", stringify!(X))
19+
);
20+
assert_eq!(
21+
::std::mem::align_of::<X>(),
22+
4usize,
23+
concat!("Alignment of ", stringify!(X))
24+
);
25+
assert_eq!(
26+
unsafe { &(*(::std::ptr::null::<X>()))._x as *const _ as usize },
27+
0usize,
28+
concat!("Offset of field: ", stringify!(X), "::", stringify!(_x))
29+
);
30+
}
31+
extern "C" {
32+
#[link_name = "\u{1}_ZN1X13some_functionEv"]
33+
pub fn X_some_function(this: *mut X);
34+
}
35+
extern "C" {
36+
#[link_name = "\u{1}_ZN1X19some_other_functionEv"]
37+
pub fn X_some_other_function(this: *mut X);
38+
}
39+
extern "C" {
40+
#[link_name = "\u{1}_ZN1XC1Ei"]
41+
pub fn X_X(this: *mut X, x: ::std::os::raw::c_int);
42+
}
43+
impl X {
44+
#[inline]
45+
pub unsafe fn some_function(&mut self) {
46+
X_some_function(self)
47+
}
48+
#[inline]
49+
pub unsafe fn some_other_function(&mut self) {
50+
X_some_other_function(self)
51+
}
52+
#[inline]
53+
pub unsafe fn new(x: ::std::os::raw::c_int) -> Self {
54+
let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit();
55+
X_X(__bindgen_tmp.as_mut_ptr(), x);
56+
__bindgen_tmp.assume_init()
57+
}
58+
}
59+
extern crate libloading;
60+
pub struct TestLib {
61+
__library: ::libloading::Library,
62+
foo: Result<
63+
unsafe extern "C" fn(
64+
x: *mut ::std::os::raw::c_void,
65+
) -> ::std::os::raw::c_int,
66+
::libloading::Error,
67+
>,
68+
bar: Result<
69+
unsafe extern "C" fn(
70+
x: *mut ::std::os::raw::c_void,
71+
) -> ::std::os::raw::c_int,
72+
::libloading::Error,
73+
>,
74+
}
75+
impl TestLib {
76+
pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error>
77+
where
78+
P: AsRef<::std::ffi::OsStr>,
79+
{
80+
let __library = ::libloading::Library::new(path)?;
81+
let foo = __library.get("foo".as_bytes()).map(|sym| *sym);
82+
let bar = __library.get("bar".as_bytes()).map(|sym| *sym);
83+
Ok(TestLib {
84+
__library: __library,
85+
foo,
86+
bar,
87+
})
88+
}
89+
pub fn can_call(&self) -> CheckTestLib {
90+
CheckTestLib { __library: self }
91+
}
92+
pub unsafe fn foo(
93+
&self,
94+
x: *mut ::std::os::raw::c_void,
95+
) -> ::std::os::raw::c_int {
96+
let sym = self.foo.as_ref().expect("Expected function, got error.");
97+
(sym)(x)
98+
}
99+
pub unsafe fn bar(
100+
&self,
101+
x: *mut ::std::os::raw::c_void,
102+
) -> ::std::os::raw::c_int {
103+
let sym = self.bar.as_ref().expect("Expected function, got error.");
104+
(sym)(x)
105+
}
106+
}
107+
pub struct CheckTestLib<'a> {
108+
__library: &'a TestLib,
109+
}
110+
impl<'a> CheckTestLib<'a> {
111+
pub fn foo(&self) -> Result<(), &'a ::libloading::Error> {
112+
self.__library.foo.as_ref().map(|_| ())
113+
}
114+
pub fn bar(&self) -> Result<(), &'a ::libloading::Error> {
115+
self.__library.bar.as_ref().map(|_| ())
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
#[repr(C)]
9+
#[derive(Debug, Default, Copy, Clone)]
10+
pub struct A {
11+
pub _x: ::std::os::raw::c_int,
12+
}
13+
#[test]
14+
fn bindgen_test_layout_A() {
15+
assert_eq!(
16+
::std::mem::size_of::<A>(),
17+
4usize,
18+
concat!("Size of: ", stringify!(A))
19+
);
20+
assert_eq!(
21+
::std::mem::align_of::<A>(),
22+
4usize,
23+
concat!("Alignment of ", stringify!(A))
24+
);
25+
assert_eq!(
26+
unsafe { &(*(::std::ptr::null::<A>()))._x as *const _ as usize },
27+
0usize,
28+
concat!("Offset of field: ", stringify!(A), "::", stringify!(_x))
29+
);
30+
}
31+
extern "C" {
32+
#[link_name = "\u{1}_ZN1A13some_functionEv"]
33+
pub fn A_some_function(this: *mut A);
34+
}
35+
extern "C" {
36+
#[link_name = "\u{1}_ZN1A19some_other_functionEv"]
37+
pub fn A_some_other_function(this: *mut A);
38+
}
39+
extern "C" {
40+
#[link_name = "\u{1}_ZN1AC1Ei"]
41+
pub fn A_A(this: *mut A, x: ::std::os::raw::c_int);
42+
}
43+
impl A {
44+
#[inline]
45+
pub unsafe fn some_function(&mut self) {
46+
A_some_function(self)
47+
}
48+
#[inline]
49+
pub unsafe fn some_other_function(&mut self) {
50+
A_some_other_function(self)
51+
}
52+
#[inline]
53+
pub unsafe fn new(x: ::std::os::raw::c_int) -> Self {
54+
let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit();
55+
A_A(__bindgen_tmp.as_mut_ptr(), x);
56+
__bindgen_tmp.assume_init()
57+
}
58+
}
59+
extern crate libloading;
60+
pub struct TestLib {
61+
__library: ::libloading::Library,
62+
foo: Result<
63+
unsafe extern "C" fn(
64+
x: *mut ::std::os::raw::c_void,
65+
) -> ::std::os::raw::c_int,
66+
::libloading::Error,
67+
>,
68+
bar: Result<unsafe extern "C" fn(), ::libloading::Error>,
69+
}
70+
impl TestLib {
71+
pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error>
72+
where
73+
P: AsRef<::std::ffi::OsStr>,
74+
{
75+
let __library = ::libloading::Library::new(path)?;
76+
let foo = __library.get("foo".as_bytes()).map(|sym| *sym);
77+
let bar = __library.get("bar".as_bytes()).map(|sym| *sym);
78+
Ok(TestLib {
79+
__library: __library,
80+
foo,
81+
bar,
82+
})
83+
}
84+
pub fn can_call(&self) -> CheckTestLib {
85+
CheckTestLib { __library: self }
86+
}
87+
pub unsafe fn foo(
88+
&self,
89+
x: *mut ::std::os::raw::c_void,
90+
) -> ::std::os::raw::c_int {
91+
let sym = self.foo.as_ref().expect("Expected function, got error.");
92+
(sym)(x)
93+
}
94+
pub unsafe fn bar(&self) -> () {
95+
let sym = self.bar.as_ref().expect("Expected function, got error.");
96+
(sym)()
97+
}
98+
}
99+
pub struct CheckTestLib<'a> {
100+
__library: &'a TestLib,
101+
}
102+
impl<'a> CheckTestLib<'a> {
103+
pub fn foo(&self) -> Result<(), &'a ::libloading::Error> {
104+
self.__library.foo.as_ref().map(|_| ())
105+
}
106+
pub fn bar(&self) -> Result<(), &'a ::libloading::Error> {
107+
self.__library.bar.as_ref().map(|_| ())
108+
}
109+
}

0 commit comments

Comments
 (0)