forked from ahmed-masud/rust-dlopen
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace libc with std::os::raw for c types
- Loading branch information
1 parent
2a58493
commit 828cf2d
Showing
19 changed files
with
249 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
mod commons; | ||
|
||
use commons::{example_lib_path, SomeData}; | ||
use dlopen::symbor::{Library, PtrOrNull, Ref, RefMut, SymBorApi, Symbol}; | ||
use std::os::raw::{c_char, c_int}; | ||
use std::ffi::CStr; | ||
|
||
#[derive(SymBorApi)] | ||
struct Api<'a> { | ||
pub rust_fun_print_something: Symbol<'a, fn()>, | ||
pub rust_fun_add_one: Symbol<'a, fn(i32) -> i32>, | ||
pub c_fun_print_something_else: Symbol<'a, unsafe extern "C" fn()>, | ||
pub c_fun_add_two: Symbol<'a, unsafe extern "C" fn(c_int) -> c_int>, | ||
pub rust_i32: Ref<'a, i32>, | ||
pub rust_i32_mut: RefMut<'a, i32>, | ||
#[dlopen_name = "rust_i32_mut"] | ||
pub rust_i32_ptr: Symbol<'a, *const i32>, | ||
pub c_int: Ref<'a, c_int>, | ||
pub c_struct: Ref<'a, SomeData>, | ||
pub rust_str: Ref<'a, &'static str>, | ||
pub c_const_char_ptr: PtrOrNull<'a, c_char>, | ||
} | ||
|
||
fn main() { | ||
let lib_path = example_lib_path(); | ||
let lib = Library::open(lib_path).expect("Could not open library"); | ||
let mut api = unsafe { Api::load(&lib) }.expect("Could not load the API"); | ||
|
||
(api.rust_fun_print_something)(); | ||
|
||
println!(" 5+1={}", (api.rust_fun_add_one)(5)); | ||
|
||
unsafe { (api.c_fun_print_something_else)() }; | ||
|
||
println!("2+2={}", unsafe { (api.c_fun_add_two)(2) }); | ||
|
||
println!("const rust i32 value: {}", *api.rust_i32); | ||
|
||
println!("mutable rust i32 value: {}", *api.rust_i32_mut); | ||
|
||
*api.rust_i32_mut = 55; | ||
|
||
//for a change use pointer to obtain its value | ||
println!("after change: {}", unsafe { **api.rust_i32_ptr }); | ||
|
||
//the same with C | ||
println!("c_int={}", *api.c_int); | ||
|
||
//now static c struct | ||
println!( | ||
"c struct first: {}, second:{}", | ||
api.c_struct.first, api.c_struct.second | ||
); | ||
|
||
//let's play with strings | ||
println!("Rust says: {}", *api.rust_str); | ||
|
||
let converted = unsafe { CStr::from_ptr(*api.c_const_char_ptr) } | ||
.to_str() | ||
.unwrap(); | ||
println!("And now C says: {}", converted); | ||
} | ||
mod commons; | ||
|
||
use commons::{example_lib_path, SomeData}; | ||
use dlopen::symbor::{Library, PtrOrNull, Ref, RefMut, SymBorApi, Symbol}; | ||
use std::ffi::CStr; | ||
use std::os::raw::{c_char, c_int}; | ||
|
||
#[derive(SymBorApi)] | ||
struct Api<'a> { | ||
pub rust_fun_print_something: Symbol<'a, fn()>, | ||
pub rust_fun_add_one: Symbol<'a, fn(i32) -> i32>, | ||
pub c_fun_print_something_else: Symbol<'a, unsafe extern "C" fn()>, | ||
pub c_fun_add_two: Symbol<'a, unsafe extern "C" fn(c_int) -> c_int>, | ||
pub rust_i32: Ref<'a, i32>, | ||
pub rust_i32_mut: RefMut<'a, i32>, | ||
#[dlopen_name = "rust_i32_mut"] | ||
pub rust_i32_ptr: Symbol<'a, *const i32>, | ||
pub c_int: Ref<'a, c_int>, | ||
pub c_struct: Ref<'a, SomeData>, | ||
pub rust_str: Ref<'a, &'static str>, | ||
pub c_const_char_ptr: PtrOrNull<'a, c_char>, | ||
} | ||
|
||
fn main() { | ||
let lib_path = example_lib_path(); | ||
let lib = Library::open(lib_path).expect("Could not open library"); | ||
let mut api = unsafe { Api::load(&lib) }.expect("Could not load the API"); | ||
|
||
(api.rust_fun_print_something)(); | ||
|
||
println!(" 5+1={}", (api.rust_fun_add_one)(5)); | ||
|
||
unsafe { (api.c_fun_print_something_else)() }; | ||
|
||
println!("2+2={}", unsafe { (api.c_fun_add_two)(2) }); | ||
|
||
println!("const rust i32 value: {}", *api.rust_i32); | ||
|
||
println!("mutable rust i32 value: {}", *api.rust_i32_mut); | ||
|
||
*api.rust_i32_mut = 55; | ||
|
||
//for a change use pointer to obtain its value | ||
println!("after change: {}", unsafe { **api.rust_i32_ptr }); | ||
|
||
//the same with C | ||
println!("c_int={}", *api.c_int); | ||
|
||
//now static c struct | ||
println!( | ||
"c struct first: {}, second:{}", | ||
api.c_struct.first, api.c_struct.second | ||
); | ||
|
||
//let's play with strings | ||
println!("Rust says: {}", *api.rust_str); | ||
|
||
let converted = unsafe { CStr::from_ptr(*api.c_const_char_ptr) } | ||
.to_str() | ||
.unwrap(); | ||
println!("And now C says: {}", converted); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
use dlopen::wrapper::{Container, WrapperApi}; | ||
use std::os::raw::{c_char, c_int}; | ||
use std::ffi::CStr; | ||
|
||
mod commons; | ||
use commons::{example_lib_path, SomeData}; | ||
|
||
#[derive(WrapperApi)] | ||
struct Api<'a> { | ||
rust_fun_print_something: fn(), | ||
rust_fun_add_one: fn(arg: i32) -> i32, | ||
c_fun_print_something_else: unsafe extern "C" fn(), | ||
c_fun_add_two: unsafe extern "C" fn(arg: c_int) -> c_int, | ||
rust_i32: &'a i32, | ||
rust_i32_mut: &'a mut i32, | ||
#[dlopen_name = "rust_i32_mut"] | ||
rust_i32_ptr: *const i32, | ||
c_int: &'a c_int, | ||
c_struct: &'a SomeData, | ||
rust_str: &'a &'static str, | ||
c_const_char_ptr: *const c_char, | ||
} | ||
|
||
//those methods won't be generated | ||
impl<'a> Api<'a> { | ||
fn rust_i32_ptr(&self) -> *const i32 { | ||
self.rust_i32_ptr | ||
} | ||
|
||
fn c_const_str(&self) -> &CStr { | ||
unsafe { CStr::from_ptr(self.c_const_char_ptr) } | ||
} | ||
} | ||
|
||
fn main() { | ||
let lib_path = example_lib_path(); | ||
let mut cont: Container<Api> = | ||
unsafe { Container::load(lib_path) }.expect("Could not open library or load symbols"); | ||
|
||
cont.rust_fun_print_something(); | ||
println!(" 5+1={}", cont.rust_fun_add_one(5)); | ||
unsafe { cont.c_fun_print_something_else() }; | ||
println!("2+2={}", unsafe { cont.c_fun_add_two(2) }); | ||
println!("const rust i32 value: {}", *cont.rust_i32()); | ||
println!("mutable rust i32 value: {}", *cont.rust_i32_mut()); | ||
*cont.rust_i32_mut_mut() = 55; | ||
println!("after change: {}", unsafe { *cont.rust_i32_ptr() }); | ||
//the same with C | ||
println!("c_int={}", *cont.c_int()); | ||
//now static c struct | ||
|
||
println!( | ||
"c struct first: {}, second:{}", | ||
cont.c_struct().first, | ||
cont.c_struct().second | ||
); | ||
//let's play with strings | ||
|
||
println!("Rust says: {}", *cont.rust_str()); | ||
let converted = cont.c_const_str().to_str().unwrap(); | ||
println!("And now C says: {}", converted); | ||
} | ||
use dlopen::wrapper::{Container, WrapperApi}; | ||
use std::ffi::CStr; | ||
use std::os::raw::{c_char, c_int}; | ||
|
||
mod commons; | ||
use commons::{example_lib_path, SomeData}; | ||
|
||
#[derive(WrapperApi)] | ||
struct Api<'a> { | ||
rust_fun_print_something: fn(), | ||
rust_fun_add_one: fn(arg: i32) -> i32, | ||
c_fun_print_something_else: unsafe extern "C" fn(), | ||
c_fun_add_two: unsafe extern "C" fn(arg: c_int) -> c_int, | ||
rust_i32: &'a i32, | ||
rust_i32_mut: &'a mut i32, | ||
#[dlopen_name = "rust_i32_mut"] | ||
rust_i32_ptr: *const i32, | ||
c_int: &'a c_int, | ||
c_struct: &'a SomeData, | ||
rust_str: &'a &'static str, | ||
c_const_char_ptr: *const c_char, | ||
} | ||
|
||
//those methods won't be generated | ||
impl<'a> Api<'a> { | ||
fn rust_i32_ptr(&self) -> *const i32 { | ||
self.rust_i32_ptr | ||
} | ||
|
||
fn c_const_str(&self) -> &CStr { | ||
unsafe { CStr::from_ptr(self.c_const_char_ptr) } | ||
} | ||
} | ||
|
||
fn main() { | ||
let lib_path = example_lib_path(); | ||
let mut cont: Container<Api> = | ||
unsafe { Container::load(lib_path) }.expect("Could not open library or load symbols"); | ||
|
||
cont.rust_fun_print_something(); | ||
println!(" 5+1={}", cont.rust_fun_add_one(5)); | ||
unsafe { cont.c_fun_print_something_else() }; | ||
println!("2+2={}", unsafe { cont.c_fun_add_two(2) }); | ||
println!("const rust i32 value: {}", *cont.rust_i32()); | ||
println!("mutable rust i32 value: {}", *cont.rust_i32_mut()); | ||
*cont.rust_i32_mut_mut() = 55; | ||
println!("after change: {}", unsafe { *cont.rust_i32_ptr() }); | ||
//the same with C | ||
println!("c_int={}", *cont.c_int()); | ||
//now static c struct | ||
|
||
println!( | ||
"c struct first: {}, second:{}", | ||
cont.c_struct().first, | ||
cont.c_struct().second | ||
); | ||
//let's play with strings | ||
|
||
println!("Rust says: {}", *cont.rust_str()); | ||
let converted = cont.c_const_str().to_str().unwrap(); | ||
println!("And now C says: {}", converted); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,4 @@ proc-macro = true | |
[dependencies] | ||
syn = "1.0" | ||
quote = "1.0" | ||
libc = "0.2" | ||
proc-macro2 = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.