Skip to content

Comments

[Rust] Improve Object API codegen: idiomatic types, Eq/Hash derives, String shadowing fix#8931

Open
pittengermdp wants to merge 3 commits intogoogle:masterfrom
pittengermdp:rust-object-api-improvements
Open

[Rust] Improve Object API codegen: idiomatic types, Eq/Hash derives, String shadowing fix#8931
pittengermdp wants to merge 3 commits intogoogle:masterfrom
pittengermdp:rust-object-api-improvements

Conversation

@pittengermdp
Copy link

Summary

  • Use idiomatic short type names (String, Vec, Box) instead of fully-qualified alloc:: paths in generated Rust Object API code, with use alloc::* imports added for no_std compatibility
  • Derive Eq and Hash on Object API types (tables, structs, unions, bitflags) when no floating-point fields are present in the type hierarchy
  • Fix String type shadowing: when a schema defines a table named String, generated code now emits std::string::String for stdlib string fields (wires up existing unused NamespaceShadowsString/ObjectStringType helpers)
  • Add #[allow(unused_imports)] to suppress warnings on generated imports
  • Fix serde Deserialize impl for enums to use fully-qualified std::string::String

Test plan

  • C++ tests pass (flattests)
  • Rust tests pass (usage, no_std, serde, reflection, outdir, clippy)
  • Go tests pass (TestAll, TestTextParsing, benchmarks)
  • Python tests pass (multi-file, one-file, flexbuffers)

…String shadowing fix

- Use short type names (String, Vec, Box) instead of fully-qualified
  alloc:: paths in generated Object API code. Add corresponding
  `use alloc::{vec::Vec, string::String, boxed::Box, string::ToString}`
  imports to maintain no_std compatibility.

- Derive Eq and Hash on Object API types (tables, structs, unions,
  bitflags) when the type hierarchy contains no floating-point fields.
  Types with f32/f64 fields continue to derive only PartialEq.

- Fix String type shadowing: when a schema defines a table named
  "String" in the same namespace, generated code now emits
  std::string::String for standard library string fields, preventing
  compilation failures. Wires up the existing but unused
  NamespaceShadowsString/ObjectStringType helpers.

- Add #[allow(unused_imports)] to generated use/crate imports to
  suppress warnings when not all imported names are used.

- Fix serde Deserialize impl for enums to use fully-qualified
  std::string::String to avoid shadowing conflicts.

Regenerated all test files via scripts/generate_code.py.
@google-cla
Copy link

google-cla bot commented Feb 19, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added python c++ rust codegen Involving generating code from schema labels Feb 19, 2026
… is enabled

The ObjectDerives() function was adding Eq/Hash but missing
serde::Serialize/Deserialize when the --rust-serialize flag was active.
This caused regenerated code to lose serde derives on *T types.
@csmulhern
Copy link
Contributor

The use of fully qualified names for Vec, String, etc, was done intentionally in #8564. The point is to deal with the situation you cover in bullet 3. If you were to name a struct / table Vec or String without using fully-qualified path names in the generated code when referencing the stdlib types, then your code generation would be broken. The use of fully-qualified paths for the stdlib types is the most robust and precise approach.

This can't be done for alloc because of how alloc works, but all current uses should be using alloc for no_std compatibility.

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 python rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants