1

I can call my test Rust program with integers as input and handle these fine, even without reference to ctypes. However I cannot seem to get a string without segfaulting in Rust.

Here is my test Rust code:

use std::env;

#[no_mangle]
pub extern fn helloworld(names: &str ) {
  println!("{}", names);
  println!("helloworld...");
}

#[no_mangle]
pub extern fn ihelloworld(names: i32 ) {
  println!("{}", names);
  println!("ihelloworld...");
}

ihelloworld works just fine. But I cannot find a way to get a string from python into Rust even if I use ctypes.

Here is the calling Python code:

import sys, ctypes, os
from ctypes import cdll
from ctypes import c_char_p
from ctypes import *


if __name__ == "__main__":
    directory = os.path.dirname(os.path.abspath(__file__))
    lib = cdll.LoadLibrary(os.path.join(directory, "target/release/libembeded.so"))

    lib.ihelloworld(1)
    lib.helloworld.argtypes = [c_char_p]
    #lib.helloworld(str("test user"))
    #lib.helloworld(u'test user')
    lib.helloworld(c_char_p("test user"))

    print("finished running!")

The output is:

1
ihelloworld...
Segmentation fault (core dumped)

The ihellowworld Rust function works just fine, but I cannot seem to get helloworld working.

2
  • 2
    See also The Rust FFI Omnibus and the numerous existing questions about Rust + Python. Commented Jan 4, 2017 at 16:26
  • @Shepmaster Thanks, I had a look, I presume Omnibus is yours - its good for covering these little points which I cannot seem to find in the Rust documentation. Commented Jan 4, 2017 at 16:50

2 Answers 2

1

The string sent from Python should be represented in Rust as a CString instead.

Sign up to request clarification or add additional context in comments.

Comments

1

I used the Rust FFI Omnibus and now my code seems to work just fine.

use std::env;
use std::ffi::{CString, CStr};
use std::os::raw::c_char;

#[no_mangle]
pub extern "C" fn helloworld(names: *const c_char) {

    unsafe {
        let c_str = CStr::from_ptr(names).to_str().unwrap();
        println!("{:?}", c_str);

    }
    println!("helloworld...");

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.