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
+1 -1
View File
@@ -4,6 +4,6 @@ use zerocopy::{AsBytes, FromBytes, FromZeroes};
#[derive(FromZeroes, FromBytes, AsBytes, Debug)]
#[repr(C)]
pub struct Header {
pub(crate) type_id: U32,
pub(crate) id: U32,
pub(crate) version: U32,
}
+9 -4
View File
@@ -56,10 +56,15 @@ pub type DecodeResult<T> = std::result::Result<T, DecodeBodyError>;
#[derive(Error, Debug)]
#[error("Decode body error: {msg}")]
pub struct DecodeBodyError {
pub msg: String,
#[source]
pub source: anyhow::Error,
pub enum DecodeBodyError {
#[error("Mismatched model id")]
MismatchedModelId,
#[error("Decode error: {msg}")]
DecodeError {
msg: String,
#[source]
source: anyhow::Error,
},
}
pub type EncodeResult<T> = std::result::Result<T, EncodeBodyError>;
+10 -6
View File
@@ -6,11 +6,11 @@ pub trait Model: Sized {
// --------------- Decode ---------------
fn native_model_decode_body(data: Vec<u8>) -> DecodeResult<Self>
fn native_model_decode_body(data: Vec<u8>, id: u32) -> DecodeResult<Self>
where
Self: Sized;
fn native_model_decode_upgrade_body(data: Vec<u8>, version: u32) -> Result<Self>
fn native_model_decode_upgrade_body(data: Vec<u8>, id: u32, version: u32) -> Result<Self>
where
Self: Sized;
@@ -19,9 +19,13 @@ pub trait Model: Sized {
Self: Sized,
{
let native_model = crate::Wrapper::deserialize(&data[..]).unwrap();
let source_id = native_model.get_id();
let source_version = native_model.get_version();
let result =
Self::native_model_decode_upgrade_body(native_model.value().to_vec(), source_version)?;
let result = Self::native_model_decode_upgrade_body(
native_model.value().to_vec(),
source_id,
source_version,
)?;
Ok((result, source_version))
}
@@ -40,7 +44,7 @@ pub trait Model: Sized {
Self: Sized,
{
let mut data = self.native_model_encode_body()?;
crate::native_model_encode(
let data = crate::native_model_encode(
&mut data,
Self::native_model_id(),
Self::native_model_version(),
@@ -54,7 +58,7 @@ pub trait Model: Sized {
{
let version = version.clone();
let mut data = self.native_model_encode_downgrade_body(version)?;
crate::native_model_encode(&mut data, Self::native_model_id(), version);
let data = crate::native_model_encode(&mut data, Self::native_model_id(), version);
Ok(data)
}
}
+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);