c3ne-types provides a way to interface with certain messy types from C3 without the need of implementing them yourself.
What types are provided
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 = into_c3_string;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 = into_c3_zstring;C3String::str_from_c3_zstringfunction:let string_slice = str_from_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 = into_c3_wstring;C3String::str_from_c3_wstringfunction:let string_slice = str_from_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 = into_c3_dstring;C3String::str_from_c3_dstringfunction:let string_slice = str_from_c3_dstring).unwrap;
How to use them
While it is not very difficult to use them, you can follow these steps to verify they work:
- Install the crate:
[]
= "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 C3String;
use c_int;
unsafe extern "system"
- Compile and check the results, then test each type, and do not forget to cast
p.nametoZStringin your C3 code once you get toWStringandDString!