File tree 4 files changed +32
-1
lines changed
4 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,14 @@ hello <- function() {
10
10
.Call(hello_wrapper )
11
11
}
12
12
13
+ # ' @export
14
+ # ' @rdname hellorust
15
+ # ' @examples hello()
16
+ # ' @useDynLib hellorust hello_wrapper2
17
+ hello2 <- function (name ) {
18
+ .Call(hello_wrapper2 , name )
19
+ }
20
+
13
21
# ' @export
14
22
# ' @rdname hellorust
15
23
# ' @examples random()
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ extern "C" {
5
5
#endif
6
6
7
7
char * string_from_rust ();
8
+ char * string_from_rust2 (const char * );
8
9
int32_t random_number ();
9
10
void run_threads ();
10
11
Original file line number Diff line number Diff line change 1
1
use std;
2
- use std:: ffi:: CString ;
2
+ use std:: ffi:: { CStr , CString } ;
3
3
use std:: os:: raw:: c_char;
4
4
5
5
#[ no_mangle]
@@ -9,3 +9,18 @@ pub extern fn string_from_rust() -> *const c_char {
9
9
std:: mem:: forget ( s) ;
10
10
p
11
11
}
12
+
13
+ // Utility function to convert c_char to string
14
+ fn c_char_to_string ( c : * const c_char ) -> String {
15
+ unsafe { CStr :: from_ptr ( c) . to_string_lossy ( ) . into_owned ( ) }
16
+ }
17
+
18
+ #[ no_mangle]
19
+ pub extern fn string_from_rust2 ( c_name : * const c_char ) -> * const c_char {
20
+ let name = c_char_to_string ( c_name) ;
21
+
22
+ let s = CString :: new ( format ! ( "Hello {} !" , name) ) . unwrap ( ) ;
23
+ let p = s. as_ptr ( ) ;
24
+ std:: mem:: forget ( s) ;
25
+ p
26
+ }
Original file line number Diff line number Diff line change @@ -10,6 +10,12 @@ SEXP hello_wrapper(){
10
10
return Rf_ScalarString (Rf_mkCharCE (string_from_rust (), CE_UTF8 ));
11
11
}
12
12
13
+ SEXP hello_wrapper2 (SEXP name ){
14
+ char * res = string_from_rust2 (Rf_translateCharUTF8 (STRING_ELT (name , 0 )));
15
+ return Rf_ScalarString (Rf_mkCharCE (res , CE_UTF8 ));
16
+ }
17
+
18
+
13
19
SEXP random_wrapper (){
14
20
return Rf_ScalarInteger (random_number ());
15
21
}
@@ -22,6 +28,7 @@ SEXP threads_wapper(){
22
28
// Standard R package stuff
23
29
static const R_CallMethodDef CallEntries [] = {
24
30
{"hello_wrapper" , (DL_FUNC ) & hello_wrapper , 0 },
31
+ {"hello_wrapper2" , (DL_FUNC ) & hello_wrapper2 , 0 },
25
32
{"random_wrapper" , (DL_FUNC ) & random_wrapper , 0 },
26
33
{"threads_wapper" , (DL_FUNC ) & threads_wapper , 0 },
27
34
{NULL , NULL , 0 }
You can’t perform that action at this time.
0 commit comments