|
| 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 | +} |
0 commit comments