fix(rust/reflection): support vectors of tables, strings, and structs in "get_field_vector" #8960
fix(rust/reflection): support vectors of tables, strings, and structs in "get_field_vector" #8960Desel72 wants to merge 2 commits intogoogle:masterfrom
Conversation
… in get_field_vector
|
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. |
|
are new api functions necessary to implement this functionality? is it not possible to fix the current API to be functional? I have not yet dived into the code to get full context. |
Hi @jtdavis777 The existing 1. Trait bound:
2. For tables and strings this isn't an issue (offsets are always 4 bytes), but for structs, the element size is schema-defined and only known at runtime. There is no Rust type with the right compile-time size to represent "a struct whose byte size I'll only know after reading the schema". This is why Because each element category (offsets-to-tables, offsets-to-strings, inline-structs) requires a different return type and a different stride, a single generic fix to the existing function is not possible without modifying the core |
|
Hi @jtdavis777, sorry for the ping. Do you have any feedback on my PR? If there are any issues, please feel free to let me know and I’ll address them. |
|
Hey @Desel72 no worries thank you for the ping. I'm more than happy to look this over and provide feedback, just a heads up I am a 100% volunteer donating my time to help maintain this project so it sometimes takes me a bit to carve out enough time to do a proper review! I try to be fairly responsive but know you're on my list even if it takes me a bit to make progress! |
|
Thank you @jtdavis777. Once this is approved, I’m planning to open another PR. Looking forward to it! |
|
Hi @jtdavis777 , I know you’re busy and donating your time, so I really appreciate any time you can give to review this PR. It would be great to get it reviewed soon so I can proceed with the next steps. |
Closes #8550
The existing
get_field_vector()reflection API only works for vectors of primitive/scalar types (integers, floats, bools). It fails for vectors of tables, strings, and structs becauseVector<T>::get()usessize_of::<T>()to step through elements, which only produces correct offsets when the Rust type's memory size matches the buffer element size. For non-primitive types this invariant doesn't hold:UOffsetToffsets, butTable<'a>is 24 bytes in Rust&stris 16 bytes in RustStruct<'a>wrapper is 16 bytes in RustSolution
Added three new dedicated functions alongside the existing
get_field_vector:get_field_vector_of_tablesVector<ForwardsUOffset<Table>>ForwardsUOffsetis 4 bytes, matching buffer layout. RequiresSchemato reject struct vectors.get_field_vector_of_stringsVector<ForwardsUOffset<&str>>ForwardsUOffsetis 4 bytes, matching buffer layout.get_field_vector_of_structsStructVector(new type)Schema.Both
get_field_vector_of_tablesandget_field_vector_of_structsuse theSchemato look up the object'sis_struct()flag, preventing misuse where a struct vector is read as table offsets (or vice versa), which would cause silent data corruption.StructVectoris a new type providinglen(),is_empty(), andget(idx) -> Structwith correct element stride from the schema.Safe buffer wrappers (
SafeTable::get_field_vector_of_tables, etc.) are also added.Changes
rust/reflection/src/lib.rs: 3 new public functions +StructVectortype + updated docs onget_field_vectorpointing to the new functionsrust/reflection/src/safe_buffer.rs: 3 correspondingSafeTablewrapper methodstests/rust_reflection_test/src/lib.rs: 8 new tests (success cases, wrong-type rejection, and cross-validation between table/struct vectors)Test plan
rust_usage_testunaffected (no dependency on reflection crate)get_field_vector_of_tablesget_field_vector_of_stringsget_field_vector_of_structs