forked from BigfootDev/flatbuffers
Rust full reflection (#8102)
* #Rust Create a crate for reflection * #Rust Add a crate for reflection tests and helper to access schema * #Rust Get root table of a buffer and access field with schema * #Rust Add 'Struct' struct and corresponding getter * #Rust Add functions of getting any table/struct field value as integer/float/string * #Rust Add setters for scalar fields * #Rust Add setter for string fields * #Rust Add getter for Table/Vector fields * #Rust Add buffer verification * Add a 'SafeBuffer' struct which provides safe methods for reflection It verifies buffer against schema during construction and provides all the unsafe getters in lib.rs in a safe way --------- Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use core::cmp::Ordering;
|
||||
use core::fmt::{Debug, Formatter, Result};
|
||||
use core::iter::{DoubleEndedIterator, ExactSizeIterator, FusedIterator};
|
||||
use core::marker::PhantomData;
|
||||
@@ -102,6 +103,37 @@ impl<'a, T: Follow<'a> + 'a> Vector<'a, T> {
|
||||
unsafe { T::follow(self.0, self.1 as usize + SIZE_UOFFSET + sz * idx) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn lookup_by_key<K: Ord>(
|
||||
&self,
|
||||
key: K,
|
||||
f: fn(&<T as Follow<'a>>::Inner, &K) -> Ordering,
|
||||
) -> Option<T::Inner> {
|
||||
if self.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut left: usize = 0;
|
||||
let mut right = self.len() - 1;
|
||||
|
||||
while left <= right {
|
||||
let mid = (left + right) / 2;
|
||||
let value = self.get(mid);
|
||||
match f(&value, &key) {
|
||||
Ordering::Equal => return Some(value),
|
||||
Ordering::Less => left = mid + 1,
|
||||
Ordering::Greater => {
|
||||
if mid == 0 {
|
||||
return None;
|
||||
}
|
||||
right = mid - 1;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn iter(&self) -> VectorIter<'a, T> {
|
||||
VectorIter::from_vector(*self)
|
||||
|
||||
Reference in New Issue
Block a user