Crates.io | translator |
lib.rs | translator |
version | 0.3.1 |
source | src |
created_at | 2018-05-30 14:50:22.553276 |
updated_at | 2022-11-11 23:51:26.651034 |
description | A procedural macro which translates your repr-C structs into C++, Python and C# at compile time for helping write FFI libraries |
homepage | |
repository | https://github.com/LivingInSyn/Translator |
max_upload_size | |
id | 67790 |
size | 29,455 |
Translator is a rust procedural macro which translates rust structs into other languages for exposure over a rust FFI (or however else you want to use them). Structs that are not [repr(C)]
are ignored. The structs are translated into:
This macro isn't a magic bullet, but it will (hopefully) greatly reduce the cost of creating Rust libraries for use with other languages
Say you want to translate the following structs:
#[repr(C)]
#[derive(Clone, Copy)]
pub struct SomeStruct {
//pub raw_message: [i16;5],
pub foo: i32,
pub bar: Baz,
pub foobar: [u8;5]
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct Baz {
pub bob: f32
}
You would have to add the Translator macro, and the Translator
to the derivations. You would also have to add the 'magic struct' to the end of the struct declarations. It would look like this:
#[macro_use]
extern crate translator;
#[repr(C)]
#[derive(Clone, Copy, Translate)]
pub struct SomeStruct {
//pub raw_message: [i16;5],
pub foo: i32,
pub bar: Baz,
pub foobar: [u8;5]
}
#[repr(C)]
#[derive(Clone, Copy, Translate)]
pub struct Baz {
pub bob: f32
}
#[derive(Translate)]
struct __FinalizeTranslatorStruct__{}
When you compile, in the 'target' folder a new folder will be created named 'TranslateOutput' with 3 files (one for each language) with the following contents:
typedef struct SomeStructTag {
int32_t foo;
Baz bar;
uint8_t foobar[5];
} SomeStruct;
typedef struct BazTag {
float bob;
} Baz;
class SomeStruct(Structure):
_fields_ = [
("foo", c_int),
("bar", Baz),
("foobar", c_ubyte * 5),
]
class Baz(Structure):
_fields_ = [
("bob", c_float),
]
[StructLayout(LayoutKind.Sequential)]
public struct SomeStruct
{
public int foo;
public Baz bar;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
public byte[] foobar;
}
[StructLayout(LayoutKind.Sequential)]
public struct Baz
{
public float bob;
}