Skip to content

Commit 5b9de8b

Browse files
authored
Fixes Rust code generation for single file output when using namespaces (#8877)
* Adds tests for Rust single file mode All existing tests only compile Rust code using --rust-module-root-file. * Adds standalone include tests for Rust The imports for these tests have been moved to their own file, as the existing intergration_test.rs file hides compilation issues from code generation due to symbols brought into scope outside of the generated code (e.g. `extern crate alloc`). * Declare alloc crate in every Rust namespace When performing code generation within a single file, extern crate alloc needs to be delcared to bring alloc into scope within every inner namespace. * Regenerates generated schemas
1 parent ea0a73d commit 5b9de8b

File tree

9 files changed

+994
-26
lines changed

9 files changed

+994
-26
lines changed

goldens/rust/basic_generated.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// automatically generated by the FlatBuffers compiler, do not modify
22
// @generated
3-
extern crate alloc;
43

4+
extern crate alloc;
55

66
#[allow(unused_imports, dead_code)]
77
pub mod flatbuffers {
88

9+
extern crate alloc;
910
#[allow(unused_imports, dead_code)]
1011
pub mod goldens {
1112

13+
extern crate alloc;
1214

1315
pub enum GalaxyOffset {}
1416
#[derive(Copy, Clone, PartialEq)]

scripts/generate_code.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ def glob(path, pattern):
8585
"--gen-name-strings",
8686
"--rust-module-root-file",
8787
]
88+
RUST_STANDALONE_OPTS = BASE_OPTS + [
89+
"--rust",
90+
"--gen-all",
91+
"--gen-name-strings",
92+
]
8893
RUST_SERIALIZE_OPTS = BASE_OPTS + [
8994
"--rust",
9095
"--gen-all",
@@ -288,13 +293,25 @@ def glob(path, pattern):
288293
schema="include_test/include_test1.fbs",
289294
)
290295

296+
flatc(
297+
RUST_STANDALONE_OPTS,
298+
include="include_test",
299+
schema="include_test/include_test1.fbs",
300+
)
301+
291302
flatc(
292303
RUST_OPTS,
293304
prefix="include_test2",
294305
include="include_test",
295306
schema="include_test/sub/include_test2.fbs",
296307
)
297308

309+
flatc(
310+
RUST_STANDALONE_OPTS,
311+
include="include_test",
312+
schema="include_test/sub/include_test2.fbs",
313+
)
314+
298315
flatc(
299316
BINARY_OPTS + ["--bfbs-filenames", str(tests_path)],
300317
include="include_test",

src/idl_gen_rust.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,6 @@ class RustGenerator : public BaseGenerator {
424424
code_.Clear();
425425
code_ += "// " + std::string(FlatBuffersGeneratedWarning());
426426
code_ += "// @generated";
427-
code_ += "extern crate alloc;";
428427

429428
assert(!cur_name_space_);
430429

@@ -3006,6 +3005,7 @@ class RustGenerator : public BaseGenerator {
30063005
"use self::serde::ser::{Serialize, Serializer, SerializeStruct};";
30073006
code_ += "";
30083007
}
3008+
code_ += "extern crate alloc;";
30093009
}
30103010

30113011
// Set up the correct namespace. This opens a namespace if the current

0 commit comments

Comments
 (0)