2 个不稳定版本
| 0.2.0 | 2019年1月12日 |
|---|---|
| 0.1.0 | 2019年1月11日 |
#8 in #rtps
在 2 crates 中使用
80KB
1.5K SLoC
RTPS IDL 到 Rust 代码生成库
一个读取 IDL 输入并生成相应 Rust 数据类型的库
预期功能
- 将 IDL 类型转换为 Rust 类型
- 将 IDL 类型转换为 TokenStreams (hygiene proc-macros 还不稳定)
添加依赖
在您的 Cargo.toml 中添加以下内容
## Cargo.toml file
[dependencies]
rtps-idl = "0.1"
RTPS-IDL 到 Rust 映射
IDL 类型按照以下方式映射到 Rust。如果尚未决定类型映射,则用 'NA' 标记。
由于 RTPS 是一个以数据为中心的框架,与原始的面向对象背景相反,因此重点放在数据结构上,至今尚未考虑接口和结构。
| IDL 类型 | Rust 类型 |
|---|---|
| module | module |
| boolean | bool |
| char/wchar | char |
| octet | u8 |
| string/wstring | std::string::String |
| short | i16 |
| long | i32 |
| long long | i64 |
| unsigned short | u16 |
| unsigned long | u32 |
| unsigned long long | u64 |
| float | f32 |
| double | f64 |
| fixed | NA |
| enum | enum |
| union | enum |
| struct | struct |
| sequence | std::vec::Vec |
| array, eg. 'T a[N]' | native array '[T;N]' |
| interface (non abstract) | NA |
| interface (abstract) | NA |
| constant (not within interface) | const |
| constant (within an interface) | NA |
| exception | std::result::Result |
| Any | NA |
| 嵌套在接口中的类型声明 | NA |
| typedef | type |
| pseudo objects | NA |
| readonly attribute | NA |
| readwrite attribute | NA |
| operation | NA |
通过示例映射
模板
| IDL | Rust |
|---|---|
sequence<octet> |
std::vec::Vec<u8> |
Typedef
| IDL | Rust |
|---|---|
| typedef long Foo; | pub type Foo = i32; |
| typedef short Foo[2]; | pub type Foo = [i16;2] |
| typedef short Foo[2][3]; | pub type Foo = [[i16; 2]; 3] |
| typedef sequence Foo; | pub type Foo = std::vec::Vec |
Struct
| IDL | Rust |
|---|---|
| struct Foo { long l; short s; } |
pub struct Foo { l: i32, s: i16; } |
Union Switch
| IDL | Rust |
|---|---|
| union Foo switch (long) { case LABEL0: long l; case LABEL1 case LABEL2: short s; default: octet o[8]; } |
pub enum Foo { LABEL0{l: i32}, LABEL2{s: i16}, LABEL1{s: i16}, default{o: [u8; 8]}, } |
Credits
使用的底层解析生成器是 PEST
原始 IDL-v4 语法来自 kpansky,并已针对本项目需求进行了调整。
CDR Serde 实现将是 github 上的 cdr-rs 项目。
依赖项
~3.5MB
~71K SLoC