feat!: Update bincode 2 & allow any AsRef<[u8]> to be decoded

This commit is contained in:
ferris
2025-05-17 08:40:29 +02:00
committed by Vincent Herlemont
parent cf3a633539
commit cf68ecfb19
4 changed files with 9 additions and 9 deletions
+1 -1
View File
@@ -22,7 +22,7 @@ native_model_macro = { version = "0.6.1", path = "native_model_macro" }
serde = { version = "1.0.200", features = ["derive"], optional = true }
bincode_1_3 = { package = "bincode", version = "1.3.3", optional = true }
bincode_2_rc = { package = "bincode", version = "2.0.0-rc.3", features = ["serde"], optional = true }
bincode_2 = { package = "bincode", version = "2.0", features = ["serde"], optional = true }
postcard_1_0 = { package = "postcard", version = "1.0.8", features = ["alloc"], optional = true }
rmp_serde_1_3 = { package = "rmp-serde", version = "1.3", optional = true }
doc-comment = "0.3.3"
+2 -2
View File
@@ -3,8 +3,8 @@
#[cfg(any(all(feature = "serde", feature = "bincode_1_3"), doc))]
pub mod bincode_1_3;
#[cfg(any(all(feature = "serde", feature = "bincode_2_rc"), doc))]
pub mod bincode_2_rc;
#[cfg(any(all(feature = "serde", feature = "bincode_2"), doc))]
pub mod bincode_2;
#[cfg(any(all(feature = "serde", feature = "postcard_1_0"), doc))]
pub mod postcard_1_0;
#[cfg(any(all(feature = "serde", feature = "rmp_serde_1_3"), doc))]
+6 -6
View File
@@ -27,7 +27,7 @@ doc_comment! {
#[cfg(any(
feature = "serde",
feature = "bincode_1_3",
feature = "bincode_2_rc",
feature = "bincode_2",
feature = "postcard_1_0",
feature = "rmp_serde_1_3",
doc
@@ -37,7 +37,7 @@ mod codec;
#[cfg(any(
feature = "serde",
feature = "bincode_1_3",
feature = "bincode_2_rc",
feature = "bincode_2",
feature = "postcard_1_0",
feature = "rmp_serde_1_3",
doc
@@ -127,7 +127,7 @@ pub struct DowngradeError {
/// # Errors
///
/// The errors returned from this function depend on the [`Encode`] trait
/// implementor (the serializer), i.e. `bincode_2_rc`.
/// implementor (the serializer), i.e. `bincode_2`.
pub fn encode<T: crate::Model>(model: &T) -> Result<Vec<u8>> {
T::native_model_encode(model)
}
@@ -148,7 +148,7 @@ pub fn encode_downgrade<T: crate::Model>(model: T, version: u32) -> Result<Vec<u
/// # Errors
///
/// The errors returned from this function depend on the [`Decode`] trait
/// implementor (the deserializer), i.e. `bincode_2_rc`.
/// implementor (the deserializer), i.e. `bincode_2`.
pub fn decode<T: crate::Model>(data: Vec<u8>) -> Result<(T, u32)> {
T::native_model_decode(data)
}
@@ -164,8 +164,8 @@ pub trait Model: Sized {
fn native_model_decode_upgrade_body(data: Vec<u8>, id: u32, version: u32) -> Result<Self>;
fn native_model_decode(data: Vec<u8>) -> Result<(Self, u32)> {
let native_model = crate::Wrapper::deserialize(&data[..]).unwrap();
fn native_model_decode(data: impl AsRef<[u8]>) -> Result<(Self, u32)> {
let native_model = crate::Wrapper::deserialize(data.as_ref()).unwrap();
let source_id = native_model.get_id();
let source_version = native_model.get_version();
let result = Self::native_model_decode_upgrade_body(