feat: implement mismatched model id + improvement

This commit is contained in:
Vincent Herlemont
2023-09-04 19:35:43 +02:00
parent e3ed493a23
commit 64301ca301
13 changed files with 188 additions and 131 deletions
+12 -26
View File
@@ -23,7 +23,11 @@ impl<T: ByteSlice> Wrapper<T> {
}
pub fn get_type_id(&self) -> u32 {
self.header.type_id.get()
self.header.id.get()
}
pub fn get_id(&self) -> u32 {
self.header.id.get()
}
pub fn get_version(&self) -> u32 {
@@ -33,7 +37,7 @@ impl<T: ByteSlice> Wrapper<T> {
impl<T: ByteSliceMut> Wrapper<T> {
pub fn set_type_id(&mut self, type_id: u32) {
self.header.type_id = U32::new(type_id);
self.header.id = U32::new(type_id);
}
pub fn set_version(&mut self, version: u32) {
@@ -41,32 +45,14 @@ impl<T: ByteSliceMut> Wrapper<T> {
}
}
pub fn native_model_encode(value: &mut Vec<u8>, type_id: u32, version: u32) {
pub fn native_model_encode(data: &mut Vec<u8>, type_id: u32, version: u32) -> Vec<u8> {
let header = Header {
type_id: U32::new(type_id),
id: U32::new(type_id),
version: U32::new(version),
};
let header = header.as_bytes();
value.reserve(header.len());
value.splice(..0, header.iter().cloned());
// Try to do with unsafe code to improve performance but benchmark shows that it's the same
//
// // Add header to the beginning of the vector
// unsafe {
// // get the raw pointer to the vector's buffer
// let ptr = value.as_mut_ptr();
//
// // move the existing elements to the right
// ptr.offset(header.len() as isize)
// .copy_from_nonoverlapping(ptr, value.len());
//
// // copy the elements from the header to the beginning of the vector
// ptr.copy_from_nonoverlapping(header.as_ptr(), header.len());
//
// // update the length of the vector
// value.set_len(value.len() + header.len());
// }
let mut header = header.as_bytes().to_vec();
header.append(data);
header
}
#[cfg(test)]
@@ -76,7 +62,7 @@ mod tests {
#[test]
fn native_model_deserialize_with_body() {
let mut data = vec![0u8; 8];
native_model_encode(&mut data, 200000, 100000);
let data = native_model_encode(&mut data, 200000, 100000);
assert_eq!(data.len(), 16);
let model = Wrapper::deserialize(&data[..]).unwrap();
assert_eq!(model.get_type_id(), 200000);