c3ne-types provides a way to interface with certain messy types from C3 without the need of implementing them yourself.
At the time of writing, all String types have been implemented, even if roughly.
- C3String:
Corresponds to
String. You can create a variable of this type using theC3String::into_c3_stringfunction:It can also be converted to a string slice by using thelet my_c3string = C3String::into_c3_string("Hello there!");
str_from_c3_stringfunction:let string_slice = my_c3string.str_from_c3_string().unwrap();
- C3ZString:
Corresponds to
ZString. You can create a variable of this type using theC3String::into_c3_zstringfunction:It can also be converted to a string slice by using thelet my_c3_zstring = C3String::into_c3_zstring("Hello there!");
C3String::str_from_c3_zstringfunction:let string_slice = CString::str_from_c3_zstring(my_c3_zstring)).unwrap();
- C3WString:
Corresponds to
WString. You can create a variable of this type using theC3String::into_c3_wstringfunction:It can also be converted to a string slice by using thelet my_c3_wstring = C3String::into_c3_wstring("Hello there!");
C3String::str_from_c3_wstringfunction:let string_slice = CString::str_from_c3_wstring(my_c3_wstring)).unwrap();
- C3DString:
Corresponds to
DString. You can create a variable of this type using theC3String::into_c3_dstringfunction:It can also be converted to a string slice by using thelet my_c3_dstring = C3String::into_c3_dstring("Hello there!");
C3String::str_from_c3_dstringfunction:let string_slice = CString::str_from_c3_dstring(my_c3_dstring)).unwrap();
While it is not very difficult to use them, you can follow these steps to verify they work:
- Install the crate:
[dependencies]
c3ne-types = "0.1.0"
# ...- Assuming you already know how to include and work with a C3 source file from Rust (check c3ne's documentation for that if not!), create a
person.c3in your directory of choosing with the following code:
module person @export;
import std::io;
struct Person
{
int age;
String name;
}
fn Person newPerson(int age, String name) @extern("newPerson")
{
return { age, name };
}
fn void printInfo(Person *p) @extern("printInfo")
{
io::printfn("Age: %d\nName: %s", p.age, p.name);
}- Then the following in your
src/main.rs:
use c3ne_types::C3String;
use std::ffi::c_int;
#[repr(C)]
struct Person {
age: c_int,
name: C3String,
}
unsafe extern "system" {
fn newPerson(age: c_int, name: C3String) -> Person;
fn printInfo(p: *const Person);
}
fn main() {
unsafe {
let p: Person = newPerson(22, C3String::into_c3_string("John Doe"));
printInfo(&p);
println!("{}", &p.name.str_from_c3_string().unwrap());
}
}- Compile and check the results, then test each type, and do not forget to cast
p.nametoZStringin your C3 code once you get toWStringandDString!