translator

Crates.iotranslator
lib.rstranslator
version0.3.1
sourcesrc
created_at2018-05-30 14:50:22.553276
updated_at2022-11-11 23:51:26.651034
descriptionA procedural macro which translates your repr-C structs into C++, Python and C# at compile time for helping write FFI libraries
homepage
repositoryhttps://github.com/LivingInSyn/Translator
max_upload_size
id67790
size29,455
Jeremy Mill (LivingInSyn)

documentation

README

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;
}

Mastodon

Commit count: 24

cargo fmt