[rust] Genericize flexbuffer reader (#6450)

* feature/rust-tokio-bytes added feature name for tokio-bytes

* Added flexbuffer implementation, TODO: typecast to avoid recurse

* Converted codebase to utilize FlexBuffer implementation, need to resolve deserialization issues

* Added todo for lifetime issue, may use &'de [u8] for deserializer instead of current method

* Added proper &[u8] implementation

* Removed unused struct

* Added experimental fix to get_slice

* Added experimental fix to get_slice

* Avoided lifetime issues via ref structs, need to check if this hurts peformance

* Updated deserializer implementation to allow for borrowed data from Reader struct

* Fixed bug with str

* Removed unnecessary generic parameter

* Added unsafe to avoid lifetime complaints, current tests pass, need to review alternatives to unsafe

* Opinionated: Removed bytes crate as this implementation could be done in a separate crate

* Cleaned up flatbuffer

* Fixed sample / example

* Resolved PR feedback, need to resolve issues with tests

* Cleaned up FlexBuffer trait to be an auto impl

* Removed TODO

* Reverted Deserializer to only support &'de [u8]

* Cleaned up / renamed function for clarification

* Renamed FlexBuffer -> InternalBuffer for clarification on it's purpose

* Fixed issue with key bytes

* resolved issues with broken tests, confirming this is a breaking change

* Removed FIXME that's solved by splitting String and Key variants

* Implemented associated types approach

* Fixed backward slice logic

* Fixed MapReader compile error

* Added from_buffer for deserialization, removed  function since it's only needed for deserialization

* Removed dead code

* Cleaned up buffer, removed AsRef in favor of Deref

* Renamed Buffer::as_str -> Buffer::buffer_str

* Minor cleanup

* Updated documentation, need to fix tests

* Removed unnecessary &

* Removed unused lifetime

* removed unnecessary as_ref

* Minor optimization wrap-up

* resolved issue with Clone

* Added test to verify no deep-copy

* Added  for optimization

* Updated to use empty fn instead of default

* Updated comments / test name - plus the 0.3.0 version bump

* comment
This commit is contained in:
Colin
2021-02-16 08:04:48 -05:00
committed by GitHub
parent a20f606c29
commit 4174c10e7a
14 changed files with 385 additions and 128 deletions

View File

@@ -20,7 +20,7 @@ use quickcheck::QuickCheck;
#[cfg(not(miri))] // slow.
fn qc_reader_no_crash() {
fn no_crash(xs: Vec<u8>) -> bool {
let r = Reader::get_root(&xs);
let r = Reader::get_root(xs.as_ref());
r.is_err() || r.is_ok()
}
QuickCheck::new()
@@ -133,7 +133,7 @@ fn string_as_num() {
}
#[test]
fn null_reader() {
let n = Reader::default();
let n = Reader::<&[u8]>::default();
assert_eq!(n.as_i8(), 0);
assert_eq!(n.as_i16(), 0);
assert_eq!(n.as_i32(), 0);
@@ -159,7 +159,7 @@ fn get_root_deref_oob() {
(FlexBufferType::Vector as u8) << 2 | BitWidth::W8 as u8,
1,
];
assert!(Reader::get_root(s).is_err());
assert!(Reader::get_root(s.as_ref()).is_err());
}
#[test]
fn get_root_deref_u64() {
@@ -170,7 +170,24 @@ fn get_root_deref_u64() {
1,
];
// The risk of crashing is reading 8 bytes from index 0.
assert_eq!(Reader::get_root(s).unwrap().as_u64(), 0);
assert_eq!(Reader::get_root(s.as_ref()).unwrap().as_u64(), 0);
}
/// Verifies that the clone operation is shallow / zero copy.
#[test]
fn clone_is_shallow() {
let mut fxb = Builder::default();
let mut m = fxb.start_map();
m.push("a", &[-1i8, -2, -3, -4]);
m.push("b", 250i64);
m.push("c", 5000u16);
m.end_map();
let r = Reader::get_root(fxb.view()).unwrap();
let r2 = r.clone();
assert_eq!(r.buffer().as_ptr(), r2.buffer().as_ptr());
}
#[test]