From cf68ecfb199ce2e8c5549794c6f3a8e45c9635df Mon Sep 17 00:00:00 2001 From: ferris Date: Sat, 17 May 2025 08:40:29 +0200 Subject: [PATCH] feat!: Update bincode 2 & allow any AsRef<[u8]> to be decoded --- libraries/native_model/Cargo.toml | 2 +- .../src/codec/{bincode_2_rc.rs => bincode_2.rs} | 0 libraries/native_model/src/codec/mod.rs | 4 ++-- libraries/native_model/src/lib.rs | 12 ++++++------ 4 files changed, 9 insertions(+), 9 deletions(-) rename libraries/native_model/src/codec/{bincode_2_rc.rs => bincode_2.rs} (100%) diff --git a/libraries/native_model/Cargo.toml b/libraries/native_model/Cargo.toml index f891a3ae..7d3eccbc 100644 --- a/libraries/native_model/Cargo.toml +++ b/libraries/native_model/Cargo.toml @@ -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" diff --git a/libraries/native_model/src/codec/bincode_2_rc.rs b/libraries/native_model/src/codec/bincode_2.rs similarity index 100% rename from libraries/native_model/src/codec/bincode_2_rc.rs rename to libraries/native_model/src/codec/bincode_2.rs diff --git a/libraries/native_model/src/codec/mod.rs b/libraries/native_model/src/codec/mod.rs index 4aa57086..ba732c2e 100644 --- a/libraries/native_model/src/codec/mod.rs +++ b/libraries/native_model/src/codec/mod.rs @@ -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))] diff --git a/libraries/native_model/src/lib.rs b/libraries/native_model/src/lib.rs index bcddfc57..443935f0 100644 --- a/libraries/native_model/src/lib.rs +++ b/libraries/native_model/src/lib.rs @@ -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(model: &T) -> Result> { T::native_model_encode(model) } @@ -148,7 +148,7 @@ pub fn encode_downgrade(model: T, version: u32) -> Result(data: Vec) -> Result<(T, u32)> { T::native_model_decode(data) } @@ -164,8 +164,8 @@ pub trait Model: Sized { fn native_model_decode_upgrade_body(data: Vec, id: u32, version: u32) -> Result; - fn native_model_decode(data: Vec) -> 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(