Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Rust programming language #3894

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*.suo
*.keystore
**/bin/**
!rust/src/bin/**
**/gen/**
**/libs/**
**/obj/**
Expand Down Expand Up @@ -44,6 +45,7 @@ grpctest
grpctest.exe
snapshot.sh
tests/go_gen
tests/rust_gen
tests/monsterdata_java_wire.mon
tests/monsterdata_go_wire.mon
tests/monsterdata_javascript_wire.mon
Expand Down Expand Up @@ -79,3 +81,4 @@ android/build/
samples/android/.externalNativeBuild/
samples/android/.gradle/
samples/android/build/
.editorconfig
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ set(FlatBuffers_Compiler_SRCS
src/idl_gen_fbs.cpp
src/idl_gen_grpc.cpp
src/idl_gen_json_schema.cpp
src/idl_gen_rust.cpp
src/flatc.cpp
src/flatc_main.cpp
grpc/src/compiler/schema_interface.h
Expand Down
54 changes: 33 additions & 21 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,24 @@ namespace flatbuffers {
// Additionally, Parser::ParseType assumes bool..string is a contiguous range
// of type tokens.
#define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
TD(NONE, "", "", uint8_t, byte, byte, byte, uint8) \
TD(UTYPE, "", "", uint8_t, byte, byte, byte, uint8) /* begin scalar/int */ \
TD(BOOL, "bool", "", uint8_t, boolean,byte, bool, bool) \
TD(CHAR, "byte", "int8", int8_t, byte, int8, sbyte, int8) \
TD(UCHAR, "ubyte", "uint8", uint8_t, byte, byte, byte, uint8) \
TD(SHORT, "short", "int16", int16_t, short, int16, short, int16) \
TD(USHORT, "ushort", "uint16", uint16_t, short, uint16, ushort, uint16) \
TD(INT, "int", "int32", int32_t, int, int32, int, int32) \
TD(UINT, "uint", "uint32", uint32_t, int, uint32, uint, uint32) \
TD(LONG, "long", "int64", int64_t, long, int64, long, int64) \
TD(ULONG, "ulong", "uint64", uint64_t, long, uint64, ulong, uint64) /* end int */ \
TD(FLOAT, "float", "float32", float, float, float32, float, float32) /* begin float */ \
TD(DOUBLE, "double", "float64", double, double, float64, double, float64) /* end float/scalar */
TD(NONE, "", "", uint8_t, byte, byte, byte, uint8, u8) \
TD(UTYPE, "", "", uint8_t, byte, byte, byte, uint8, u8) /* begin scalar/int */ \
TD(BOOL, "bool", "", uint8_t, boolean,byte, bool, bool, bool) \
TD(CHAR, "byte", "int8", int8_t, byte, int8, sbyte, int8, i8) \
TD(UCHAR, "ubyte", "uint8", uint8_t, byte, byte, byte, uint8, u8) \
TD(SHORT, "short", "int16", int16_t, short, int16, short, int16, i16) \
TD(USHORT, "ushort", "uint16", uint16_t, short, uint16, ushort, uint16, u16) \
TD(INT, "int", "int32", int32_t, int, int32, int, int32, i32) \
TD(UINT, "uint", "uint32", uint32_t, int, uint32, uint, uint32, u32) \
TD(LONG, "long", "int64", int64_t, long, int64, long, int64, i64) \
TD(ULONG, "ulong", "uint64", uint64_t, long, uint64, ulong, uint64, u64) /* end int */ \
TD(FLOAT, "float", "float32", float, float, float32, float, float32, f32) /* begin float */ \
TD(DOUBLE, "double", "float64", double, double, float64, double, float64, f64) /* end float/scalar */
#define FLATBUFFERS_GEN_TYPES_POINTER(TD) \
TD(STRING, "string", "", Offset<void>, int, int, StringOffset, int) \
TD(VECTOR, "", "", Offset<void>, int, int, VectorOffset, int) \
TD(STRUCT, "", "", Offset<void>, int, int, int, int) \
TD(UNION, "", "", Offset<void>, int, int, int, int)
TD(STRING, "string", "", Offset<void>, int, int, StringOffset, int, i32) \
TD(VECTOR, "", "", Offset<void>, int, int, VectorOffset, int, i32) \
TD(STRUCT, "", "", Offset<void>, int, int, int, int, i32) \
TD(UNION, "", "", Offset<void>, int, int, int, int, i32)

// The fields are:
// - enum
Expand All @@ -68,12 +68,13 @@ namespace flatbuffers {
// - Go type.
// - C# / .Net type.
// - Python type.
// - Rust type.

// using these macros, we can now write code dealing with types just once, e.g.

/*
switch (type) {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
#define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
case BASE_TYPE_ ## ENUM: \
// do something specific to CTYPE here
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
Expand All @@ -90,13 +91,13 @@ switch (type) {
__extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
#endif
enum BaseType {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
#define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
BASE_TYPE_ ## ENUM,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
};

#define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
#define FLATBUFFERS_TD(ENUM, IDLTYPE, ALIASTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
static_assert(sizeof(CTYPE) <= sizeof(largest_scalar_t), \
"define largest_scalar_t as " #CTYPE);
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
Expand Down Expand Up @@ -207,7 +208,8 @@ struct Namespace {
// With max_components you can request less than the number of components
// the current namespace has.
std::string GetFullyQualifiedName(const std::string &name,
size_t max_components = 1000) const;
size_t max_components = 1000,
const std::string &sep = ".") const;
};

// Base class for all definition types (fields, structs_, enums_).
Expand Down Expand Up @@ -375,6 +377,7 @@ struct IDLOptions {
std::string go_namespace;
bool reexport_ts_modules;
bool protobuf_ascii_alike;
bool strict_rust;

// Possible options for the more general generator below.
enum Language {
Expand All @@ -389,6 +392,7 @@ struct IDLOptions {
kBinary = 1 << 8,
kTs = 1 << 9,
kJsonSchema = 1 << 10,
kRust = 1 << 11,
kMAX
};

Expand Down Expand Up @@ -422,6 +426,7 @@ struct IDLOptions {
skip_flatbuffers_import(false),
reexport_ts_modules(true),
protobuf_ascii_alike(false),
strict_rust(false),
lang(IDLOptions::kJava),
lang_to_generate(0) {}
};
Expand Down Expand Up @@ -765,6 +770,13 @@ extern bool GenerateGeneral(const Parser &parser,
const std::string &path,
const std::string &file_name);

// Generate Rust files from the definitions in the Parser object.
// See idl_gen_rust.cpp.
extern bool GenerateRust(const Parser &parser,
const std::string &path,
const std::string &file_name);


// Generate a schema file from the internal representation, useful after
// parsing a .proto schema.
extern std::string GenerateFBS(const Parser &parser,
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ unpacking/parsing it first, while still having great forwards/backwards compatib
* JavaScript
* PHP
* Python

* Rust
*and many more in progress...*

## Contribution
Expand Down
2 changes: 2 additions & 0 deletions rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
Cargo.lock
12 changes: 12 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "flatbuffers"
version = "0.3.0"
authors = ["Joseph Dunne <jd@lambda.tech>"]
description = "Memory Efficient Serialization Library. Flatbuffers runtime for Rust."
keywords = ["flatbuffers", "serialization", "deserialization"]
readme = "../readme.md"
license = "Apache-2.0"
homepage = "http://google.github.io/flatbuffers/index.html"
repository = "https://github.com/josephDunne/flatbuffers"

[lib]
name = "flatbuffers"

[[bin]]
name = "test"

[dependencies]
byteorder = "0.5.*"

[features]
default = []
# Test the idl_gen_rust cpp code.
test_idl_gen = []

22 changes: 22 additions & 0 deletions rust/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "flatbuffers_macros"
version = "0.1.0"
authors = ["Joseph Dunne <jd@lambda.tech>"]
license = "Apache-2.0"
description = "Macros for generating flatbuffer objects"
keywords = ["flatbuffers"]


[lib]
plugin = true

[dependencies]
regex = "*"

[dev-dependencies]
compiletest_rs = "*"

[dev-dependencies.flatbuffers]
path = "../"
version = ">=0.0"

Loading