c3ne-types

Crates.ioc3ne-types
lib.rsc3ne-types
version0.1.0
created_at2025-10-13 02:30:15.626262+00
updated_at2025-10-13 02:30:15.626262+00
descriptionc3ne-types is a helper crate for users of the main c3ne crate that provides an implementation of the messier C3 types for use in Rust.
homepage
repositoryhttps://github.com/TheDreamer123/c3ne-types
max_upload_size
id1879929
size9,062
(TheDreamer123)

documentation

README

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 the C3String::into_c3_string function:
    let my_c3string = C3String::into_c3_string("Hello there!");
    
    It can also be converted to a string slice by using the str_from_c3_string function:
    let string_slice = my_c3string.str_from_c3_string().unwrap();
    
  • C3ZString: Corresponds to ZString. You can create a variable of this type using the C3String::into_c3_zstring function:
    let my_c3_zstring = C3String::into_c3_zstring("Hello there!");
    
    It can also be converted to a string slice by using the C3String::str_from_c3_zstring function:
    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 the C3String::into_c3_wstring function:
    let my_c3_wstring = C3String::into_c3_wstring("Hello there!");
    
    It can also be converted to a string slice by using the C3String::str_from_c3_wstring function:
    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 the C3String::into_c3_dstring function:
    let my_c3_dstring = C3String::into_c3_dstring("Hello there!");
    
    It can also be converted to a string slice by using the C3String::str_from_c3_dstring function:
    let string_slice = CString::str_from_c3_dstring(my_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:

  1. Install the crate:
[dependencies]
c3ne-types = "0.1.0"
# ...
  1. 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.c3 in 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);
}
  1. 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());
    }
}
  1. Compile and check the results, then test each type, and do not forget to cast p.name to ZString in your C3 code once you get to WString and DString!
Commit count: 0

cargo fmt