5 unstable releases
Uses old Rust 2015
0.3.1 | Nov 11, 2022 |
---|---|
0.3.0 | Aug 3, 2018 |
0.2.1 | May 31, 2018 |
0.1.1 | May 30, 2018 |
0.1.0 | May 30, 2018 |
#928 in Procedural macros
30KB
486 lines
Translator
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:
- Python
- C++
- C#
This macro isn't a magic bullet, but it will (hopefully) greatly reduce the cost of creating Rust libraries for use with other languages
Use
Input
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:
c++
typedef struct SomeStructTag {
int32_t foo;
Baz bar;
uint8_t foobar[5];
} SomeStruct;
typedef struct BazTag {
float bob;
} Baz;
Python
class SomeStruct(Structure):
_fields_ = [
("foo", c_int),
("bar", Baz),
("foobar", c_ubyte * 5),
]
class Baz(Structure):
_fields_ = [
("bob", c_float),
]
C#
[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;
}
Dependencies
~2MB
~48K SLoC