Skip to content

Commit

Permalink
Remove extern crate where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenByteDev committed Jul 5, 2022
1 parent e62bba4 commit ac364c6
Show file tree
Hide file tree
Showing 33 changed files with 511 additions and 600 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ I hope that this library will help you to quickly get what you need and avoid er
## Quick example

```rust
extern crate dlopen;
#[macro_use]
extern crate dlopen_derive;
use dlopen::wrapper::{Container, WrapperApi};

#[derive(WrapperApi)]
Expand Down
3 changes: 0 additions & 3 deletions examples/commons/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
extern crate dlopen;
extern crate libc;
extern crate regex;
use dlopen::utils::{PLATFORM_FILE_EXTENSION, PLATFORM_FILE_PREFIX};
use libc::c_int;
use std::env;
Expand Down
5 changes: 1 addition & 4 deletions examples/raw.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#[macro_use]
extern crate const_cstr;
extern crate libc;

mod commons;

use commons::{example_lib_path, SomeData};
use const_cstr::const_cstr;
use dlopen::raw::Library;
use libc::{c_char, c_int};
use std::ffi::CStr;
Expand Down
2 changes: 0 additions & 2 deletions examples/raw_addr_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate libc;

mod commons;

use commons::example_lib_path;
Expand Down
6 changes: 1 addition & 5 deletions examples/symbor.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#[macro_use]
extern crate const_cstr;
extern crate dlopen;
extern crate libc;

mod commons;

use commons::{example_lib_path, SomeData};
use const_cstr::const_cstr;
use dlopen::symbor::Library;
use libc::{c_char, c_int};
use std::ffi::CStr;
Expand Down
127 changes: 62 additions & 65 deletions examples/symbor_api.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,62 @@
#[macro_use]
extern crate dlopen_derive;
extern crate libc;

mod commons;
use commons::{example_lib_path, SomeData};
use dlopen::symbor::{Library, PtrOrNull, Ref, RefMut, SymBorApi, Symbol};
use libc::{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::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);
}
128 changes: 62 additions & 66 deletions examples/wrapper_api.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,62 @@
extern crate dlopen;
#[macro_use]
extern crate dlopen_derive;
extern crate libc;
use dlopen::wrapper::{Container, WrapperApi};
use libc::{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::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);
}
Loading

0 comments on commit ac364c6

Please sign in to comment.