-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
[Rust]: enum as Option<T> #5614
Conversation
Nice work. Did you add a test with a Also it think you should mark your PR with |
e35602e
to
f345fe9
Compare
@rw Now I'm thinking that the flatbuffers test suite is missing some backward & forward dependency tests that could have could detected this issue in the first place. |
@jean-airoldie Can you say more? cc @aardappel |
Essentially the tests are compiled and ran at the same time, so the generated code is always up date with the binary format. Maybe it would be worthwhile to check whether new generated code works well against a old schema serialized file. In the case of rust, this would have detected the undefined behavior with the enum evolution. |
I added a test with enum evolution { name: "FromFuture",
// Race=-127 is unknown Race, not defined in the monster_test.fbs schema.
signed_enum: -127,
} Assume that -127 is a value from a future version of Race enum. |
Is there a way to remove the dependency on |
I see two potential ways to remove the
|
@jean-airoldie AFAICT the |
Yes. Its kinda trivial really. #[repr(u8)]
struct Enum {
A = 1,
B = 2,
}
# We need to generate this
fn from_u8(int: u8) -> Option<Enum> {
match int {
x if x == A as u8 => Some(Enum::A),
x if x == B as u8 => Some(Enum::B),
_ => None,
}
}
fn to_u8(e: Enum) -> u8 {
e as u8
}
Yes, however I'm not familiar with the code generator. But i know this is trivial to implement as a procedural rust macro. |
f381fd7
to
31f8799
Compare
Any update on this? I’m currently working around this at the moment. Still waiting on the issue with num derive? Or is the thought to use the flatbuffers code generator? I’d also like to see something similar for unions as per @jean-airoldie they exhibit similar undefined behavior. I can open a separate issue for that as I assume this fix is just for enums |
3c24b46
to
6e9c40c
Compare
@jcrevier |
6e9c40c
to
712a5f4
Compare
@jean-airoldie, @rw |
@vglavnyy Ah, yes, I can see the generated code works for unions as well. Having those |
Nice, the generated code looks good. As an aside this makes me realize a flatbuffer enum that would previously be expressed as:
Can now be simply be expressed as:
which removes the need for special logic previously required by the |
Thanks! To help my review, can you please explain this PR?
Adding code comments along these lines would be very helpful, as well. |
Important History Potential UB could happen if an old code receives a message generated on an updated schema with new discriminants added to a schema. thread 'binary_data_from_future_source' has overflowed its stack
error: test failed, to rerun pass '--test integration_test'
Caused by:
process didn't exit successfully: `E:\github\flatbuffers\tests\rust_usage_test\target\debug\deps\integration_test-6e2446f87a4c78ed.exe --quiet` (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION)
./RustTest.sh: line 25: 16004 Segmentation fault cargo test $TARGET_FLAG -- --quiet The breaking changes to the user-facing API
The changes in the code generator
Default semantic of flatbuffers values The changes in the base code All unit-tests touched by PR were updated. P.S. The PR was rebased to the master to pass the tests. |
- Add `tests/monsterdata_unknown.mon` into repo - Add test of Option<None> enum using a future monster on the wire.
9a5d198
to
042a938
Compare
In Rust generated code the While union enumerators have explicit 'NONE' value to indicate an empty variant. For example Monster's
|
I mean't that you don't need an explicit value of the default (or unset) in an enum anymore.
I don't think we should distinguish these cases and I think it would make sense to remove NONE. This way the behavior of the union and the enum would be consistent. |
@jean-airoldie I will remove generation of NONE after review. |
f116b80
to
f12cca8
Compare
Hello, are we still working on this? I noticed bit_flag support wasn't implemented while working on #6070 and would like these change to get in first so I can work off of them. Suggestion: Instead of returning |
No, I'm not working on it. |
This PR resolves #5467 and #5581 issues.
Current status: ready for review and testing.
This PR isn't finished yet due to an issue with the
num-derive
crate (rust-num/num-derive#35).As soon as the issue is solved, the final commit will be prepared.
Thanks @jean-airoldie. He did most of the work.