@@ -75,6 +75,7 @@ pub trait DType:
75
75
+ Rem < Output = Self >
76
76
+ Sub < Output = Self >
77
77
+ PartialOrd
78
+ + Debug
78
79
+ Display
79
80
+ FromF32
80
81
{
@@ -558,6 +559,22 @@ impl TryFrom<&str> for Space {
558
559
}
559
560
}
560
561
562
+ impl TryFrom < * const c_char > for Space {
563
+ type Error = ( ) ;
564
+ fn try_from ( value : * const c_char ) -> Result < Self , ( ) > {
565
+ if value. is_null ( ) {
566
+ Err ( ( ) )
567
+ } else {
568
+ unsafe { CStr :: from_ptr ( value) }
569
+ . to_str ( )
570
+ . ok ( )
571
+ . map ( |s| Self :: try_from ( s) . ok ( ) )
572
+ . flatten ( )
573
+ . ok_or ( ( ) )
574
+ }
575
+ }
576
+ }
577
+
561
578
impl Display for Space {
562
579
fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
563
580
core:: fmt:: write (
@@ -755,38 +772,8 @@ pub fn convert_space_ffi<T: DType, const N: usize>(
755
772
where
756
773
Channels < N > : ValidChannels ,
757
774
{
758
- let from = unsafe {
759
- if from. is_null ( ) {
760
- return 1 ;
761
- } else {
762
- if let Some ( s) = CStr :: from_ptr ( from)
763
- . to_str ( )
764
- . ok ( )
765
- . map ( |s| Space :: try_from ( s) . ok ( ) )
766
- . flatten ( )
767
- {
768
- s
769
- } else {
770
- return 1 ;
771
- }
772
- }
773
- } ;
774
- let to = unsafe {
775
- if to. is_null ( ) {
776
- return 2 ;
777
- } else {
778
- if let Some ( s) = CStr :: from_ptr ( to)
779
- . to_str ( )
780
- . ok ( )
781
- . map ( |s| Space :: try_from ( s) . ok ( ) )
782
- . flatten ( )
783
- {
784
- s
785
- } else {
786
- return 2 ;
787
- }
788
- }
789
- } ;
775
+ let Ok ( from) = Space :: try_from ( from) else { return 1 } ;
776
+ let Ok ( to) = Space :: try_from ( to) else { return 2 } ;
790
777
let pixels = unsafe {
791
778
if pixels. is_null ( ) {
792
779
return 3 ;
@@ -905,6 +892,25 @@ where
905
892
col
906
893
} )
907
894
}
895
+
896
+ /// Same as `str2space` but with FFI types
897
+ ///
898
+ /// Returns an N-length pointer to T on success or null on failure
899
+ pub fn str2space_ffi < T : DType , const N : usize > ( s : * const c_char , to : * const c_char ) -> * const T
900
+ where
901
+ Channels < N > : ValidChannels ,
902
+ {
903
+ if s. is_null ( ) {
904
+ return core:: ptr:: null ( ) ;
905
+ } ;
906
+ let Some ( s) = unsafe { CStr :: from_ptr ( s) } . to_str ( ) . ok ( ) else {
907
+ return core:: ptr:: null ( ) ;
908
+ } ;
909
+ let Ok ( to) = Space :: try_from ( to) else {
910
+ return core:: ptr:: null ( ) ;
911
+ } ;
912
+ str2space :: < T , N > ( s, to) . map_or ( core:: ptr:: null ( ) , |b| Box :: into_raw ( Box :: new ( b) ) . cast ( ) )
913
+ }
908
914
// ### Str2Col ### }}}
909
915
910
916
// ### FORWARD ### {{{
@@ -1316,22 +1322,39 @@ where
1316
1322
// ### MONOTYPED EXTERNAL FUNCTIONS ### {{{
1317
1323
1318
1324
#[ no_mangle]
1319
- extern "C" fn convert_space_ffi_3f32 ( from : * const c_char , to : * const c_char , pixels : * mut f32 , len : usize ) -> i32 {
1325
+ extern "C" fn convert_space_3f32 ( from : * const c_char , to : * const c_char , pixels : * mut f32 , len : usize ) -> i32 {
1320
1326
convert_space_ffi :: < _ , 3 > ( from, to, pixels, len)
1321
1327
}
1322
1328
#[ no_mangle]
1323
- extern "C" fn convert_space_ffi_4f32 ( from : * const c_char , to : * const c_char , pixels : * mut f32 , len : usize ) -> i32 {
1329
+ extern "C" fn convert_space_4f32 ( from : * const c_char , to : * const c_char , pixels : * mut f32 , len : usize ) -> i32 {
1324
1330
convert_space_ffi :: < _ , 4 > ( from, to, pixels, len)
1325
1331
}
1326
1332
#[ no_mangle]
1327
- extern "C" fn convert_space_ffi_3f64 ( from : * const c_char , to : * const c_char , pixels : * mut f64 , len : usize ) -> i32 {
1333
+ extern "C" fn convert_space_3f64 ( from : * const c_char , to : * const c_char , pixels : * mut f64 , len : usize ) -> i32 {
1328
1334
convert_space_ffi :: < _ , 3 > ( from, to, pixels, len)
1329
1335
}
1330
1336
#[ no_mangle]
1331
- extern "C" fn convert_space_ffi_4f64 ( from : * const c_char , to : * const c_char , pixels : * mut f64 , len : usize ) -> i32 {
1337
+ extern "C" fn convert_space_4f64 ( from : * const c_char , to : * const c_char , pixels : * mut f64 , len : usize ) -> i32 {
1332
1338
convert_space_ffi :: < _ , 4 > ( from, to, pixels, len)
1333
1339
}
1334
1340
1341
+ #[ no_mangle]
1342
+ extern "C" fn str2space_3f32 ( s : * const c_char , to : * const c_char ) -> * const f32 {
1343
+ str2space_ffi :: < f32 , 3 > ( s, to)
1344
+ }
1345
+ #[ no_mangle]
1346
+ extern "C" fn str2space_4f32 ( s : * const c_char , to : * const c_char ) -> * const f32 {
1347
+ str2space_ffi :: < f32 , 4 > ( s, to)
1348
+ }
1349
+ #[ no_mangle]
1350
+ extern "C" fn str2space_3f64 ( s : * const c_char , to : * const c_char ) -> * const f64 {
1351
+ str2space_ffi :: < f64 , 3 > ( s, to)
1352
+ }
1353
+ #[ no_mangle]
1354
+ extern "C" fn str2space_4f64 ( s : * const c_char , to : * const c_char ) -> * const f64 {
1355
+ str2space_ffi :: < f64 , 4 > ( s, to)
1356
+ }
1357
+
1335
1358
macro_rules! cdef1 {
1336
1359
( $base: ident, $f32: ident, $f64: ident) => {
1337
1360
#[ no_mangle]
0 commit comments