Skip to content

Commit 60d6e9e

Browse files
committed
[Rust] Allow reflection verifier to start with custom root
1 parent c1cb450 commit 60d6e9e

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

rust/reflection/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub use vector_of_any::VectorOfAny;
2222
mod r#struct;
2323
pub use crate::r#struct::Struct;
2424
pub use crate::reflection_generated::reflection;
25+
pub use crate::reflection_verifier::verify_with_options;
2526
pub use crate::safe_buffer::SafeBuffer;
2627

2728
use flatbuffers::{

rust/reflection/src/reflection_verifier.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@ pub fn verify_with_options(
2828
schema: &Schema,
2929
opts: &VerifierOptions,
3030
buf_loc_to_obj_idx: &mut HashMap<usize, i32>,
31+
root_table_name: Option<&str>,
3132
) -> FlatbufferResult<()> {
3233
let mut verifier = Verifier::new(opts, buffer);
33-
if let Some(table_object) = schema.root_table() {
34-
if let core::result::Result::Ok(table_pos) = verifier.get_uoffset(0) {
34+
let root_table = match root_table_name {
35+
Some(name) => schema
36+
.objects()
37+
.lookup_by_key(name, |o, k| o.key_compare_with_value(k)),
38+
None => schema.root_table(),
39+
};
40+
if let Some(table_object) = root_table {
41+
if let Ok(table_pos) = verifier.get_uoffset(0) {
3542
// Inserts -1 as object index for root table
3643
buf_loc_to_obj_idx.insert(table_pos.try_into()?, -1);
3744
let mut verified = vec![false; buffer.len()];

rust/reflection/src/safe_buffer.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'a> SafeBuffer<'a> {
4949
opts: &VerifierOptions,
5050
) -> FlatbufferResult<Self> {
5151
let mut buf_loc_to_obj_idx = HashMap::new();
52-
verify_with_options(&buf, schema, opts, &mut buf_loc_to_obj_idx)?;
52+
verify_with_options(&buf, schema, opts, &mut buf_loc_to_obj_idx, None)?;
5353
Ok(SafeBuffer {
5454
buf,
5555
schema,
@@ -141,7 +141,9 @@ impl<'a> SafeTable<'a> {
141141
pub fn get_field_string(&self, field_name: &str) -> FlatbufferResult<Option<&str>> {
142142
if let Some(field) = self.safe_buf.find_field_by_name(self.loc, field_name)? {
143143
// SAFETY: the buffer was verified during construction.
144-
Ok(Some(unsafe { get_field_string(&Table::new(&self.safe_buf.buf, self.loc), &field) }))
144+
Ok(Some(unsafe {
145+
get_field_string(&Table::new(&self.safe_buf.buf, self.loc), &field)
146+
}))
145147
} else {
146148
Err(FlatbufferError::FieldNotFound)
147149
}

0 commit comments

Comments
 (0)