Skip to content

Conversation

@userzhy
Copy link
Contributor

@userzhy userzhy commented Dec 20, 2025

Why?

This PR implements support for UNION type ID in Rust tagged enum serialization as part of the xlang type system enhancement (issue #3027). Previously, Rust tagged enums used ENUM or NAMED_ENUM type IDs in xlang mode, but according to the xlang specification, tagged union types (like Rust enums, C++ std::variant, Go Union) should use the UNION type ID (38).

This change aligns Rust's tagged enum serialization with C++ variant serialization (#3025) and ensures cross-language compatibility.

What does this PR do?

  1. Add UNION and NONE type IDs (rust/fory-core/src/types.rs):

    • Added UNION = 38 - Tagged union type (one of several alternatives)
    • Added NONE = 39 - Empty/unit type (no data)
    • Added corresponding constants
  2. Modify enum serializer (rust/fory-core/src/serializer/enum_.rs):

    • Modified write_type_info to write UNION type ID when in xlang mode
    • Modified read_type_info to expect UNION type ID when in xlang mode
  3. Add tests (rust/tests/tests/test_enum.rs):

    • xlang_tagged_enum_uses_union_type_id: Verifies that UNION type ID (38) is used in xlang mode
    • xlang_complex_tagged_enum_roundtrip: Verifies roundtrip serialization works correctly

Serialization Format Change in xlang Mode

Before:
| ref flag | ENUM/NAMED_ENUM type id | variant index |

After:
| ref flag | UNION type id (38)      | variant index |

Related issues

Does this PR introduce any user-facing change?

  • Does this PR introduce any public API change?
  • Does this PR introduce any binary protocol compatibility change?

Note: This change affects the binary protocol in xlang mode only. In xlang mode, tagged enums now use the UNION type ID (38) instead of ENUM (13) or NAMED_ENUM (14). This is a breaking change for xlang serialization but aligns with the xlang specification and other language implementations (C++, Go).

Benchmark

This change does not introduce any performance impact as it only changes the type ID value written during serialization.

…mode

- Add UNION (38) and NONE (39) type IDs to TypeId enum in types.rs
- Modify write_type_info to use UNION type ID in xlang mode
- Modify read_type_info to expect UNION type ID in xlang mode
- Add tests for UNION type ID in xlang mode

Closes apache#3028
Simple enums (all unit variants) should use ENUM type ID.
Tagged enums (some variants have data) should use UNION type ID.

This fixes the compatibility with Java where simple enums are
serialized with ENUM type ID.

- Add is_simple_enum() helper to check if all variants are unit types
- Pass is_tagged parameter to write_type_info/read_type_info
- Update tests to verify correct type ID for both enum types
@chaokunyang
Copy link
Collaborator

@userzhy I will take a look soon, thanks for contributing this feature

@userzhy
Copy link
Contributor Author

userzhy commented Dec 24, 2025

@chaokunyang Thanks for your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use union type id for tagged enum serialization in rust

2 participants