Skip to content

Commit 6978bc5

Browse files
Merge pull request #40 from Reim-developer/dev
Feat: Added Rust `ffi_rs` crates & unit test. # Change Log: commit 2fb96e4 (HEAD -> dev, origin/dev) Author: Reim-developer <contact.kaxtr@gmail.com> Date: Mon Jul 28 18:34:48 2025 +0700 feat: + Added `free_c_str` for check is valid path + Addded test case for this function commit 3bf446f Author: Reim-developer <contact.kaxtr@gmail.com> Date: Mon Jul 28 18:34:32 2025 +0700 feat: + Added `free_c_str` for free `c_char` + Addded test case for this function commit 47fcd01 Author: Reim-developer <contact.kaxtr@gmail.com> Date: Mon Jul 28 18:33:55 2025 +0700 Added `mod` list & linter commit 5382cd2 Author: Reim-developer <contact.kaxtr@gmail.com> Date: Mon Jul 28 18:33:42 2025 +0700 Added `Bash` script for test FFI Rust -> C++ commit 0eb3f9f Author: Reim-developer <contact.kaxtr@gmail.com> Date: Mon Jul 28 18:33:24 2025 +0700 Added test case with `fn` `is_valid_path` from Rust commit 4a41584 Author: Reim-developer <contact.kaxtr@gmail.com> Date: Mon Jul 28 18:33:03 2025 +0700 Added `Cargo.lock` & `Cargo.toml` to `ffi_rs` crates. commit 6f28482 Author: Reim-developer <contact.kaxtr@gmail.com> Date: Mon Jul 28 03:28:01 2025 +0700 Added Bash script helper for test FFI Rust -> C++
2 parents 9f85f8e + 2fb96e4 commit 6978bc5

7 files changed

Lines changed: 124 additions & 0 deletions

File tree

ffi_rs/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi_rs/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "ffi_rs"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lib]
7+
crate-type = ["staticlib"]
8+
9+
[profile.dev]
10+
panic = "abort"
11+
12+
[dependencies]

ffi_rs/src/alloc/memory.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::ffi::{c_char, CString};
2+
3+
#[unsafe(no_mangle)]
4+
pub unsafe extern "C" fn free_c_str(c_str: *mut c_char) { unsafe {
5+
if !c_str.is_null() {
6+
let _ = CString::from_raw(c_str);
7+
}
8+
}}
9+
10+
#[test]
11+
fn test_free_c_str() { unsafe {
12+
use std::ffi::CString;
13+
use std::ptr::null_mut;
14+
15+
let raw_str = CString
16+
::new("Reim-developer")
17+
.map(|raw| raw.into_raw())
18+
.unwrap_or(null_mut());
19+
20+
free_c_str(raw_str);
21+
}}

ffi_rs/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![deny(clippy::all, clippy::pedantic, clippy::nursery, clippy::perf)]
2+
3+
pub mod utils {
4+
pub mod path;
5+
}
6+
7+
pub mod alloc {
8+
pub mod memory;
9+
}

ffi_rs/src/utils/path.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::{ffi::{CStr}, os::raw::c_char, path::Path};
2+
3+
#[unsafe(no_mangle)]
4+
pub unsafe extern "C" fn is_valid_path(path: *mut c_char) -> bool { unsafe {
5+
if path.is_null() {
6+
return false;
7+
}
8+
9+
let c_str = match CStr::from_ptr(path).to_str() {
10+
Ok(string) => string,
11+
Err(_) => return false,
12+
};
13+
14+
if c_str.is_empty() {
15+
return false;
16+
}
17+
18+
let path = Path::new(c_str);
19+
20+
path
21+
.as_os_str()
22+
.as_encoded_bytes()
23+
.iter()
24+
.all(|&char| char != 0)
25+
}}
26+
27+
#[test]
28+
fn test_is_valid_path() {
29+
use std::ffi::{CString};
30+
use std::ptr::null_mut;
31+
use crate::alloc::memory::free_c_str;
32+
33+
unsafe {
34+
let c_string = CString
35+
::new("../../src/utils/path.rs") /* Path to some exits file ...*/
36+
.map(|raw| raw.into_raw())
37+
.unwrap_or(null_mut());
38+
39+
assert_eq!(true, is_valid_path(c_string));
40+
free_c_str(c_string);
41+
}
42+
}

ffi_rs/test_lib.cxx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
3+
using std::cout;
4+
5+
extern "C" {
6+
bool is_valid_path(char *path);
7+
}
8+
9+
int main() {
10+
char path[] = "./src/lib.rs";
11+
12+
auto result = is_valid_path(path) ? "Valid path" : "Not valid path";
13+
14+
cout << result << "\n";
15+
16+
return 0;
17+
}

ffi_rs/test_lib.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
function test_lib() {
4+
if [ ! -d "build" ]; then
5+
mkdir -p "build"
6+
fi
7+
8+
cargo build
9+
10+
local rust_lib="target/debug/libffi_rs.a"
11+
clang++ "test_lib.cxx" "$rust_lib" -o "build/test_lib"
12+
13+
"./build/test_lib"
14+
}
15+
16+
test_lib

0 commit comments

Comments
 (0)