Skip to content

c3ne-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.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

TheDreamer123/c3ne-types

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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!

About

c3ne-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.

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages