From 4371653264cbe904a79ce403e517d7896411a709 Mon Sep 17 00:00:00 2001 From: printfn Date: Sat, 16 Mar 2024 09:32:14 +0000 Subject: [PATCH] Make usize byte serialisation platform-independent --- core/src/lib.rs | 3 +-- core/src/serialize.rs | 20 +++++++++++++++++++- windows-wix/main.wxs | 7 ------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index cbd36c32..4a9089bc 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -294,8 +294,7 @@ impl Context { /// Serializes all variables defined in this context to a stream of bytes. /// Note that the specific format is NOT stable, and can change with any - /// minor update. It is also not compatible between 32-bit and - /// 64-bit architectures. + /// minor update. /// /// # Errors /// This function returns an error if the input cannot be serialized. diff --git a/core/src/serialize.rs b/core/src/serialize.rs index 7fa6d23b..470107c1 100644 --- a/core/src/serialize.rs +++ b/core/src/serialize.rs @@ -34,7 +34,25 @@ macro_rules! impl_serde { }; } -impl_serde!(u8 i32 u64 usize); +impl_serde!(u8 i32 u64); + +impl Serialize for usize { + fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { + (*self as u64).serialize(write) + } +} + +impl Deserialize for usize { + #[allow(clippy::use_self, clippy::cast_possible_truncation)] + fn deserialize(read: &mut impl io::Read) -> FResult { + let val = u64::deserialize(read)?; + if usize::BITS < 64 && val > usize::MAX as u64 { + Err(FendError::DeserializationError) + } else { + Ok(val as usize) + } + } +} impl Serialize for &str { fn serialize(&self, write: &mut impl io::Write) -> FResult<()> { diff --git a/windows-wix/main.wxs b/windows-wix/main.wxs index 2f03d6b1..72212984 100644 --- a/windows-wix/main.wxs +++ b/windows-wix/main.wxs @@ -16,13 +16,6 @@ limitations under the License. --> - -