Skip to content

Commit

Permalink
Replace libc with std::os::raw for c types
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenByteDev committed Jul 5, 2022
1 parent 2a58493 commit 828cf2d
Show file tree
Hide file tree
Showing 19 changed files with 249 additions and 255 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ once_cell = "1.12"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winnt", "minwindef", "winerror", "libloaderapi", "errhandlingapi", "dbghelp", "processthreadsapi", "basetsd"] }

[target.'cfg(unix)'.dependencies]
libc = "0.2"

[dev-dependencies]
const-cstr = "0.3"
libc = "0.2"
regex = "1.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/commons/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dlopen::utils::{PLATFORM_FILE_EXTENSION, PLATFORM_FILE_PREFIX};
use libc::c_int;
use std::env;
use std::os::raw::c_int;
use std::path::PathBuf;

//Rust when building dependencies adds some weird numbers to file names
Expand Down
2 changes: 1 addition & 1 deletion examples/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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;
use std::os::raw::{c_char, c_int};

fn main() {
let lib_path = example_lib_path();
Expand Down
2 changes: 1 addition & 1 deletion examples/raw_addr_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod commons;

use commons::example_lib_path;
use dlopen::raw::{AddressInfoObtainer, Library};
use libc::c_int;
use std::os::raw::c_int;

fn main() {
let lib_path = example_lib_path();
Expand Down
2 changes: 1 addition & 1 deletion examples/symbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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;
use std::os::raw::{c_char, c_int};

fn main() {
let lib_path = example_lib_path();
Expand Down
124 changes: 62 additions & 62 deletions examples/symbor_api.rs
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);
}
124 changes: 62 additions & 62 deletions examples/wrapper_api.rs
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);
}
1 change: 0 additions & 1 deletion rust-dlopen-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ proc-macro = true
[dependencies]
syn = "1.0"
quote = "1.0"
libc = "0.2"
proc-macro2 = "1.0"
3 changes: 0 additions & 3 deletions rust-example-dylib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ description = "Example dynamic link library for executing tests of libraries tha
[lib]
name = "example"
crate-type = ["cdylib"]

[dependencies]
libc = "0.2"
4 changes: 1 addition & 3 deletions rust-example-dylib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
//! It exports multiple symbols with different types and abis.
//! It's main purpose is to be used in tests of dynlib crate.

extern crate libc;

use libc::{c_char, c_int};
use std::os::raw::{c_char, c_int};

//FUNCTIONS
#[no_mangle]
Expand Down
5 changes: 2 additions & 3 deletions src/raw/unix.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use super::super::err::Error;
use super::common::{AddressInfo, OverlappingSymbol};
use libc::{
c_int, c_void, dladdr, dlclose, dlerror, dlopen, dlsym, Dl_info, RTLD_LAZY, RTLD_LOCAL,
};
use libc::{dladdr, dlclose, dlerror, dlopen, dlsym, Dl_info, RTLD_LAZY, RTLD_LOCAL};
use once_cell::sync::Lazy;
use std::ffi::{CStr, OsStr};
use std::io::{Error as IoError, ErrorKind};
use std::os::raw::{c_int, c_void};
use std::os::unix::ffi::OsStrExt;
use std::ptr::{null, null_mut};
use std::sync::Mutex;
Expand Down
2 changes: 1 addition & 1 deletion src/symbor/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ generated `load(&Library)` function to load all symbols from previously opened l
```no_run
use dlopen::symbor::{Library, Symbol, SymBorApi, PtrOrNull, RefMut, PtrOrNullMut};
use libc::{c_double, c_char};
use std::os::raw::{c_double, c_char};
#[derive(SymBorApi)]
struct Example<'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/wrapper/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Wrappers are not generated only for:
```no_run
use dlopen::wrapper::{WrapperApi, Container};
use libc::{c_char};
use std::os::raw::{c_char};
use std::ffi::CStr;
#[derive(WrapperApi)]
Expand Down
2 changes: 1 addition & 1 deletion src/wrapper/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ easy to use `Container` inside structures.
```no_run
use dlopen::wrapper::{Container, WrapperApi};
use libc::{c_char};
use std::os::raw::{c_char};
use std::ffi::CStr;
#[derive(WrapperApi)]
Expand Down
2 changes: 1 addition & 1 deletion tests/commons/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dlopen::utils::{PLATFORM_FILE_EXTENSION, PLATFORM_FILE_PREFIX};
use libc::c_int;
use serde::Deserialize;
use std::fs;
use std::os::raw::c_int;
use std::path::{Path, PathBuf};

#[derive(Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion tests/raw.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use const_cstr::const_cstr;
use dlopen::raw::{AddressInfoObtainer, Library};
use libc::{c_char, c_int};
use std::ffi::CStr;
use std::os::raw::{c_char, c_int};

mod commons;
use commons::{example_lib_path, SomeData};
Expand Down
2 changes: 1 addition & 1 deletion tests/symbor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use const_cstr::const_cstr;
use dlopen::symbor::Library;
use libc::{c_char, c_int};
use std::ffi::CStr;
use std::os::raw::{c_char, c_int};

mod commons;
use commons::{example_lib_path, SomeData};
Expand Down
Loading

0 comments on commit 828cf2d

Please sign in to comment.