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

Make flatc generate Rust files not requiring std #7273

Merged
merged 4 commits into from
Apr 27, 2022

Conversation

fjarri
Copy link
Contributor

@fjarri fjarri commented Apr 25, 2022

flatc generates Rust accessors for flatbuffers structures using std imports, which prevents it from being used in Rust's no_std environment. This PR replaces std imports with the equivalent core ones, which do work in no_std.

For reviewers: this is mostly a search-and-replace PR, I did not get into the architecture of the compiler, so I might have missed some subtle appearances of std (e.g. if a string is assembled out of several variables or something) - please check.

@github-actions github-actions bot added c++ codegen Involving generating code from schema rust labels Apr 25, 2022
@fjarri fjarri force-pushed the flatc-rust-gen-no-std branch 3 times, most recently from 43c2d21 to 6b1c094 Compare April 25, 2022 23:01
@CasperN
Copy link
Collaborator

CasperN commented Apr 26, 2022

Can you

  1. add a test binary in tests/rust_usage_test/ (or maybe make another test crate tests/rust_no_std_tests/) that;
  2. uses ![no_std],
  3. imports the generated code, and
  4. call those tests in tests/RustTest.sh?

This way, CI will confirm that we can at least build with no_std.

@fjarri
Copy link
Contributor Author

fjarri commented Apr 26, 2022

So, it turned out RustTest.sh already had a cargo test --features no_std, but it didn't have any effect, since the only thing that the no_std feature changed was the allocator in integration_tests.rs. So I added an explicit !#[no_std], and gated std usage on the lack of no_std feature. Had to also gate a few tests that used the filesystem, but the majority is still working.

It is still a mystery how that test worked before #7268, since for me it immediately fails with a compilation error.

@CasperN CasperN merged commit 750dde7 into google:master Apr 27, 2022
@CasperN
Copy link
Collaborator

CasperN commented Apr 27, 2022

It is still a mystery how that test worked before #7268, since for me it immediately fails with a compilation error.

Yeah, that's a little sus. Also I found the out_dir test was broken (sigh), I forgot it was disabled on CI

@fjarri
Copy link
Contributor Author

fjarri commented Apr 27, 2022

Great, would you be able to publish a new release of flatc in https://github.com/google/flatbuffers/releases/? (this will also close #7081)

I think it should be a patch version bump; it will break the users somehow still on rust_edition=2015, but there should be a negligible quantity of those. Edit: actually, scratch that, since flatbuffers crate uses 2018 edition, I don't think it is possible to use it in a crate with 2015.

@CasperN CasperN mentioned this pull request Apr 27, 2022
@fjarri fjarri mentioned this pull request May 16, 2022
sssooonnnggg pushed a commit to sssooonnnggg/flatbuffers that referenced this pull request Aug 8, 2022
* Set an explicit 2018 edition for Rust tests

* Replace all `std` usage with `core` and `alloc` in Rust code generator

* Update the generated files

* Make Rust tests actually use no_std when the corresponding feature is enabled
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ codegen Involving generating code from schema rust
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants