Skip to content

Commit 6391475

Browse files
committed
Change convert_space_ffi to be generic
Includes the usual 4 monotyped functions but without a macro because it's only used once.
1 parent 3778a67 commit 6391475

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

scripts/test_ctypes.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
colcon = ctypes.CDLL(f"./target/release/{LIBRARY}")
1616

17-
colcon.convert_space_ffi.argtypes = [
17+
colcon.convert_space_ffi_3f32.argtypes = [
1818
ctypes.c_char_p,
1919
ctypes.c_char_p,
2020
cpixels,
2121
ctypes.c_uint,
2222
]
23-
colcon.convert_space_ffi.restype = ctypes.c_int32
23+
colcon.convert_space_ffi_3f32.restype = ctypes.c_int32
2424

2525
# up
2626
colcon.srgb_to_hsv_3f32.argtypes = [cpixel]
@@ -141,6 +141,6 @@ def pixcmp(a, b):
141141
pixcmp(list(pix), HSV)
142142

143143
pix = (ctypes.c_float * len(SRGB))(*SRGB)
144-
if colcon.convert_space_ffi("srgb".encode(), "lch".encode(), pix, len(pix)) != 0:
144+
if colcon.convert_space_ffi_3f32("srgb".encode(), "lch".encode(), pix, len(pix)) != 0:
145145
print("CONVERT SPACE FAIL")
146146
pixcmp(list(pix), LCH)

src/lib.rs

+29-3
Original file line numberDiff line numberDiff line change
@@ -744,8 +744,17 @@ where
744744
/// Same as `convert_space_sliced` but with FFI types.
745745
///
746746
/// Returns 0 on success, 1 on invalid `from`, 2 on invalid `to`, 3 on invalid `pixels`
747-
#[no_mangle]
748-
pub extern "C" fn convert_space_ffi(from: *const c_char, to: *const c_char, pixels: *mut f32, len: usize) -> i32 {
747+
///
748+
/// `len` is in elements rather than bytes
749+
pub fn convert_space_ffi<T: DType, const N: usize>(
750+
from: *const c_char,
751+
to: *const c_char,
752+
pixels: *mut T,
753+
len: usize,
754+
) -> i32
755+
where
756+
Channels<N>: ValidChannels,
757+
{
749758
let from = unsafe {
750759
if from.is_null() {
751760
return 1;
@@ -785,7 +794,7 @@ pub extern "C" fn convert_space_ffi(from: *const c_char, to: *const c_char, pixe
785794
core::slice::from_raw_parts_mut(pixels, len)
786795
}
787796
};
788-
convert_space_sliced::<_, 3>(from, to, pixels);
797+
convert_space_sliced::<T, N>(from, to, pixels);
789798
0
790799
}
791800

@@ -1306,6 +1315,23 @@ where
13061315

13071316
// ### MONOTYPED EXTERNAL FUNCTIONS ### {{{
13081317

1318+
#[no_mangle]
1319+
extern "C" fn convert_space_ffi_3f32(from: *const c_char, to: *const c_char, pixels: *mut f32, len: usize) -> i32 {
1320+
convert_space_ffi::<_, 3>(from, to, pixels, len)
1321+
}
1322+
#[no_mangle]
1323+
extern "C" fn convert_space_ffi_4f32(from: *const c_char, to: *const c_char, pixels: *mut f32, len: usize) -> i32 {
1324+
convert_space_ffi::<_, 4>(from, to, pixels, len)
1325+
}
1326+
#[no_mangle]
1327+
extern "C" fn convert_space_ffi_3f64(from: *const c_char, to: *const c_char, pixels: *mut f64, len: usize) -> i32 {
1328+
convert_space_ffi::<_, 3>(from, to, pixels, len)
1329+
}
1330+
#[no_mangle]
1331+
extern "C" fn convert_space_ffi_4f64(from: *const c_char, to: *const c_char, pixels: *mut f64, len: usize) -> i32 {
1332+
convert_space_ffi::<_, 4>(from, to, pixels, len)
1333+
}
1334+
13091335
macro_rules! cdef1 {
13101336
($base:ident, $f32:ident, $f64:ident) => {
13111337
#[no_mangle]

0 commit comments

Comments
 (0)