Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2d427f

Browse files
committed
add value-box-ffi crate
1 parent fd57eab commit a2d427f

31 files changed

Lines changed: 1595 additions & 1 deletion

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ members = [
55
"reference-box",
66
"string-box",
77
"value-box",
8-
"value-box-extensions"
8+
"value-box-extensions",
9+
"value-box-ffi"
910
]
1011
resolver = "2"
1112

value-box-ffi/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "value-box-ffi"
3+
version = "1.0.0"
4+
authors = ["feenk gmbh <[email protected]>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
value-box = { version = "2" }
9+
array-box = { version = "1", optional = true }
10+
geometry-box = { version = "1", optional = true }
11+
string-box = { version = "1", optional = true }
12+
phlow = { version = "1", optional = true, features = [ "phlow-derive" ] }
13+
phlow-extensions = { version = "1", optional = true }
14+
crossbeam = { version = "0.8", optional = true }
15+
16+
[features]
17+
phlow = [ "dep:phlow", "phlow-extensions", "value-box/phlow" ]
18+
array-box = [ "dep:array-box", "crossbeam" ]
19+
geometry-box = [ "dep:geometry-box" ]
20+
string-box = [ "dep:string-box" ]
21+
value-box = [ ]
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
use std::any::Any;
2+
3+
use array_box::ArrayBox;
4+
5+
use value_box::{BoxerError, ReturnBoxerResult, ValueBox, ValueBoxPointer};
6+
7+
pub trait ArrayBoxFFI<T>
8+
where
9+
T: Default + Copy + Any,
10+
{
11+
fn boxer_array_byte_size(count: usize) -> usize;
12+
fn boxer_array_create() -> *mut ValueBox<ArrayBox<T>>;
13+
fn boxer_array_create_with(element: T, amount: usize) -> *mut ValueBox<ArrayBox<T>>;
14+
15+
fn boxer_array_create_from_data(_data: *mut T, amount: usize) -> *mut ValueBox<ArrayBox<T>>;
16+
17+
fn boxer_array_drop(ptr: *mut ValueBox<ArrayBox<T>>);
18+
19+
fn boxer_array_copy_into(
20+
_maybe_null_source_ptr: *mut ValueBox<ArrayBox<T>>,
21+
_maybe_null_destination_ptr: *mut ValueBox<ArrayBox<T>>,
22+
);
23+
24+
fn boxer_array_copy_into_data(
25+
_maybe_null_source_ptr: *mut ValueBox<ArrayBox<T>>,
26+
_destination_data: *mut T,
27+
length: usize,
28+
);
29+
30+
fn boxer_array_get_length(_maybe_null_ptr: *mut ValueBox<ArrayBox<T>>) -> usize;
31+
32+
fn boxer_array_get_capacity(_maybe_null_ptr: *mut ValueBox<ArrayBox<T>>) -> usize;
33+
34+
fn boxer_array_get_data(_maybe_null_ptr: *mut ValueBox<ArrayBox<T>>) -> *mut T;
35+
36+
fn boxer_array_at_put(_maybe_null_ptr: *mut ValueBox<ArrayBox<T>>, index: usize, item: T)
37+
where
38+
T: Clone;
39+
40+
fn boxer_array_at(_maybe_null_ptr: *mut ValueBox<ArrayBox<T>>, index: usize, default: T) -> T
41+
where
42+
T: Clone;
43+
}
44+
45+
impl<T> ArrayBoxFFI<T> for ArrayBox<T>
46+
where
47+
T: Default + Copy + Any,
48+
{
49+
fn boxer_array_byte_size(count: usize) -> usize {
50+
std::mem::size_of::<T>() * count
51+
}
52+
53+
fn boxer_array_create() -> *mut ValueBox<ArrayBox<T>> {
54+
ValueBox::new(ArrayBox::<T>::default()).into_raw()
55+
}
56+
57+
fn boxer_array_create_with(element: T, amount: usize) -> *mut ValueBox<ArrayBox<T>> {
58+
ValueBox::new(ArrayBox::<T>::from_vector(vec![element; amount])).into_raw()
59+
}
60+
61+
fn boxer_array_create_from_data(_data: *mut T, amount: usize) -> *mut ValueBox<ArrayBox<T>> {
62+
ValueBox::new(ArrayBox::<T>::from_data(_data, amount)).into_raw()
63+
}
64+
65+
fn boxer_array_drop(ptr: *mut ValueBox<ArrayBox<T>>) {
66+
ptr.release();
67+
}
68+
69+
fn boxer_array_copy_into(
70+
source_array: *mut ValueBox<ArrayBox<T>>,
71+
destination_array: *mut ValueBox<ArrayBox<T>>,
72+
) {
73+
source_array
74+
.with_ref(|source_array| {
75+
destination_array.with_mut_ok(|destination_array| {
76+
source_array.copy_into(destination_array);
77+
})
78+
})
79+
.log();
80+
}
81+
82+
fn boxer_array_copy_into_data(
83+
source_array: *mut ValueBox<ArrayBox<T>>,
84+
destination_data: *mut T,
85+
length: usize,
86+
) {
87+
source_array
88+
.with_ref(|source_array| {
89+
if source_array.length > length {
90+
BoxerError::AnyError(
91+
format!(
92+
"The source (len = {}) does not fit into destination (len = {})",
93+
source_array.length, length
94+
)
95+
.into(),
96+
)
97+
.into()
98+
} else if source_array.data.is_null() {
99+
BoxerError::AnyError("The source data must not be nil".into()).into()
100+
} else if destination_data.is_null() {
101+
BoxerError::AnyError("The destination data must not be nil".into()).into()
102+
} else {
103+
Ok(unsafe {
104+
std::ptr::copy_nonoverlapping::<T>(
105+
source_array.data,
106+
destination_data,
107+
length,
108+
)
109+
})
110+
}
111+
})
112+
.log();
113+
}
114+
115+
fn boxer_array_get_length(array_box: *mut ValueBox<ArrayBox<T>>) -> usize {
116+
array_box.with_ref_ok(|array| array.length).or_log(0)
117+
}
118+
119+
fn boxer_array_get_capacity(array_box: *mut ValueBox<ArrayBox<T>>) -> usize {
120+
array_box.with_ref_ok(|array| array.capacity).or_log(0)
121+
}
122+
123+
fn boxer_array_get_data(array_box: *mut ValueBox<ArrayBox<T>>) -> *mut T {
124+
array_box
125+
.with_ref_ok(|array| array.data)
126+
.or_log(std::ptr::null_mut())
127+
}
128+
129+
fn boxer_array_at_put(array_box: *mut ValueBox<ArrayBox<T>>, index: usize, item: T)
130+
where
131+
T: Clone,
132+
{
133+
array_box
134+
.with_mut_ok(|array| array.at_put(index, item))
135+
.log();
136+
}
137+
138+
fn boxer_array_at(array_box: *mut ValueBox<ArrayBox<T>>, index: usize, default: T) -> T
139+
where
140+
T: Clone,
141+
{
142+
array_box
143+
.with_ref_ok(|array| array.at(index))
144+
.or_log(default)
145+
}
146+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use array_box::ArrayBox;
2+
use value_box::ValueBox;
3+
4+
use crate::array::ArrayBoxFFI;
5+
6+
pub type BoxerArrayF32 = ArrayBox<f32>;
7+
8+
#[no_mangle]
9+
pub extern "C" fn boxer_array_f32_create() -> *mut ValueBox<BoxerArrayF32> {
10+
BoxerArrayF32::boxer_array_create()
11+
}
12+
13+
#[no_mangle]
14+
pub extern "C" fn boxer_array_f32_create_with(
15+
element: f32,
16+
amount: usize,
17+
) -> *mut ValueBox<BoxerArrayF32> {
18+
BoxerArrayF32::boxer_array_create_with(element, amount)
19+
}
20+
21+
#[no_mangle]
22+
pub extern "C" fn boxer_array_f32_create_from_data(
23+
_data: *mut f32,
24+
amount: usize,
25+
) -> *mut ValueBox<BoxerArrayF32> {
26+
BoxerArrayF32::boxer_array_create_from_data(_data, amount)
27+
}
28+
29+
#[no_mangle]
30+
pub extern "C" fn boxer_array_f32_get_length(_ptr: *mut ValueBox<BoxerArrayF32>) -> usize {
31+
BoxerArrayF32::boxer_array_get_length(_ptr)
32+
}
33+
34+
#[no_mangle]
35+
pub extern "C" fn boxer_array_f32_get_capacity(_ptr: *mut ValueBox<BoxerArrayF32>) -> usize {
36+
BoxerArrayF32::boxer_array_get_capacity(_ptr)
37+
}
38+
39+
#[no_mangle]
40+
pub extern "C" fn boxer_array_f32_get_data(_ptr: *mut ValueBox<BoxerArrayF32>) -> *mut f32 {
41+
BoxerArrayF32::boxer_array_get_data(_ptr)
42+
}
43+
44+
#[no_mangle]
45+
pub extern "C" fn boxer_array_f32_at_put(
46+
_ptr: *mut ValueBox<BoxerArrayF32>,
47+
index: usize,
48+
item: f32,
49+
) {
50+
BoxerArrayF32::boxer_array_at_put(_ptr, index, item);
51+
}
52+
53+
#[no_mangle]
54+
pub extern "C" fn boxer_array_f32_drop(ptr: *mut ValueBox<BoxerArrayF32>) {
55+
BoxerArrayF32::boxer_array_drop(ptr);
56+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use core::ffi::c_int;
2+
3+
use array_box::ArrayBox;
4+
use value_box::ValueBox;
5+
6+
use crate::array::ArrayBoxFFI;
7+
8+
pub type BoxerArrayInt = ArrayBox<c_int>;
9+
10+
#[no_mangle]
11+
pub extern "C" fn boxer_array_int_create() -> *mut ValueBox<BoxerArrayInt> {
12+
BoxerArrayInt::boxer_array_create()
13+
}
14+
15+
#[no_mangle]
16+
pub extern "C" fn boxer_array_int_create_with(
17+
element: c_int,
18+
amount: usize,
19+
) -> *mut ValueBox<BoxerArrayInt> {
20+
BoxerArrayInt::boxer_array_create_with(element, amount)
21+
}
22+
23+
#[no_mangle]
24+
pub extern "C" fn boxer_array_int_create_from_data(
25+
_data: *mut c_int,
26+
amount: usize,
27+
) -> *mut ValueBox<BoxerArrayInt> {
28+
BoxerArrayInt::boxer_array_create_from_data(_data, amount)
29+
}
30+
31+
#[no_mangle]
32+
pub extern "C" fn boxer_array_int_get_length(_ptr: *mut ValueBox<BoxerArrayInt>) -> usize {
33+
BoxerArrayInt::boxer_array_get_length(_ptr)
34+
}
35+
36+
#[no_mangle]
37+
pub extern "C" fn boxer_array_int_get_capacity(_ptr: *mut ValueBox<BoxerArrayInt>) -> usize {
38+
BoxerArrayInt::boxer_array_get_capacity(_ptr)
39+
}
40+
41+
#[no_mangle]
42+
pub extern "C" fn boxer_array_int_get_data(_ptr: *mut ValueBox<BoxerArrayInt>) -> *mut c_int {
43+
BoxerArrayInt::boxer_array_get_data(_ptr)
44+
}
45+
46+
#[no_mangle]
47+
pub extern "C" fn boxer_array_int_at_put(
48+
_ptr: *mut ValueBox<BoxerArrayInt>,
49+
index: usize,
50+
item: c_int,
51+
) {
52+
BoxerArrayInt::boxer_array_at_put(_ptr, index, item);
53+
}
54+
55+
#[no_mangle]
56+
pub extern "C" fn boxer_array_int_at(_ptr: *mut ValueBox<BoxerArrayInt>, index: usize) -> c_int {
57+
BoxerArrayInt::boxer_array_at(_ptr, index, 0)
58+
}
59+
60+
#[no_mangle]
61+
pub extern "C" fn boxer_array_int_drop(ptr: *mut ValueBox<BoxerArrayInt>) {
62+
BoxerArrayInt::boxer_array_drop(ptr);
63+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use array_box::ArrayBox;
2+
use geometry_box::PointBox;
3+
4+
use value_box::{ReturnBoxerResult, ValueBox, ValueBoxPointer};
5+
6+
use crate::array::ArrayBoxFFI;
7+
8+
pub type BoxerPointF32 = PointBox<f32>;
9+
pub type BoxerArrayPointF32 = ArrayBox<BoxerPointF32>;
10+
11+
#[no_mangle]
12+
pub extern "C" fn boxer_array_point_f32_create() -> *mut ValueBox<BoxerArrayPointF32> {
13+
BoxerArrayPointF32::boxer_array_create()
14+
}
15+
16+
#[no_mangle]
17+
pub extern "C" fn boxer_array_point_f32_create_with(
18+
point: *mut ValueBox<BoxerPointF32>,
19+
amount: usize,
20+
) -> *mut ValueBox<BoxerArrayPointF32> {
21+
point
22+
.with_clone_ok(|point| BoxerArrayPointF32::boxer_array_create_with(point, amount))
23+
.or_log(std::ptr::null_mut())
24+
}
25+
26+
#[no_mangle]
27+
pub extern "C" fn boxer_array_point_f32_create_from_data(
28+
data: *mut BoxerPointF32,
29+
amount: usize,
30+
) -> *mut ValueBox<BoxerArrayPointF32> {
31+
BoxerArrayPointF32::boxer_array_create_from_data(data, amount)
32+
}
33+
34+
#[no_mangle]
35+
pub extern "C" fn boxer_array_point_f32_drop(array: *mut ValueBox<BoxerArrayPointF32>) {
36+
BoxerArrayPointF32::boxer_array_drop(array);
37+
}
38+
39+
#[no_mangle]
40+
pub extern "C" fn boxer_array_point_f32_get_length(
41+
array: *mut ValueBox<BoxerArrayPointF32>,
42+
) -> usize {
43+
BoxerArrayPointF32::boxer_array_get_length(array)
44+
}
45+
46+
#[no_mangle]
47+
pub extern "C" fn boxer_array_point_f32_get_capacity(
48+
array: *mut ValueBox<BoxerArrayPointF32>,
49+
) -> usize {
50+
BoxerArrayPointF32::boxer_array_get_capacity(array)
51+
}
52+
53+
#[no_mangle]
54+
pub extern "C" fn boxer_array_point_f32_get_data(
55+
array: *mut ValueBox<BoxerArrayPointF32>,
56+
) -> *mut BoxerPointF32 {
57+
BoxerArrayPointF32::boxer_array_get_data(array)
58+
}
59+
60+
#[cfg(test)]
61+
mod test {
62+
use crate::array_point_f32::{
63+
boxer_array_point_f32_create_with, boxer_array_point_f32_drop,
64+
boxer_array_point_f32_get_length,
65+
};
66+
use crate::point_f32::{
67+
boxer_point_f32_default, boxer_point_f32_drop, boxer_point_f32_get_x, boxer_point_f32_get_y,
68+
};
69+
70+
#[test]
71+
fn create_with_point() {
72+
let point = boxer_point_f32_default();
73+
assert_eq!(boxer_point_f32_get_x(point), 0.0);
74+
assert_eq!(boxer_point_f32_get_y(point), 0.0);
75+
76+
let array = boxer_array_point_f32_create_with(point, 10);
77+
// array is created with a clone of the point, we can drop the point
78+
boxer_point_f32_drop(point);
79+
80+
assert_eq!(boxer_array_point_f32_get_length(array), 10);
81+
boxer_array_point_f32_drop(array);
82+
}
83+
}

0 commit comments

Comments
 (0)