From bb861cb828dedaae880d1f0cea759d79020f6c90 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Wed, 26 Feb 2025 17:20:15 +0100 Subject: add MsgId and BufferId to objects where needed some objects where still handling BufferId or MsgId as their raw types which lead to errors now that the Types are properly parsed in the varinats --- src/primitive/bufferid.rs | 16 +++++++++++++++- src/primitive/msgid.rs | 27 ++++++++++++++++++++++++++- src/primitive/variant.rs | 4 ++-- 3 files changed, 43 insertions(+), 4 deletions(-) (limited to 'src/primitive') diff --git a/src/primitive/bufferid.rs b/src/primitive/bufferid.rs index 25cc029..4be867d 100644 --- a/src/primitive/bufferid.rs +++ b/src/primitive/bufferid.rs @@ -1,4 +1,4 @@ -#[derive(Copy, Clone, Debug, std::cmp::PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] pub struct BufferId(pub i32); @@ -17,6 +17,20 @@ impl Deserialize for BufferId { } } +impl From for BufferId { + fn from(value: i32) -> Self { + BufferId(value) + } +} + +impl std::ops::Deref for BufferId { + type Target = i32; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/primitive/msgid.rs b/src/primitive/msgid.rs index fb9b6af..27608fe 100644 --- a/src/primitive/msgid.rs +++ b/src/primitive/msgid.rs @@ -1,4 +1,4 @@ -#[derive(Copy, Clone, Debug, std::cmp::PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] #[repr(transparent)] pub struct MsgId( #[cfg(not(feature = "long-message-id"))] pub i32, @@ -24,6 +24,31 @@ impl Deserialize for MsgId { } } +#[cfg(not(feature = "long-message-id"))] +impl From for MsgId { + fn from(value: i32) -> Self { + Self(value) + } +} + +#[cfg(feature = "long-message-id")] +impl From for MsgId { + fn from(value: i64) -> Self { + Self(value) + } +} + +impl std::ops::Deref for MsgId { + #[cfg(not(feature = "long-message-id"))] + type Target = i32; + #[cfg(feature = "long-message-id")] + type Target = i64; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/primitive/variant.rs b/src/primitive/variant.rs index 340d908..a7ff8ca 100644 --- a/src/primitive/variant.rs +++ b/src/primitive/variant.rs @@ -288,7 +288,7 @@ impl Serialize for Variant { impl Deserialize for Variant { fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { - trace!("trying to parse variant with bytes: {:?}", b); + trace!("trying to parse variant with bytes: {:x?}", b); let (_, qtype) = i32::parse(&b[0..4])?; let qtype = qtype as u32; @@ -805,7 +805,7 @@ mod tests { #[test] fn bufferid_deserialize() { let test_bytes = vec![ - 0, 0, 0, 127, 0, 0, 0, 0, 8, 66, 117, 102, 102, 101, 114, 73, 100, 0, 0, 0, 1 + 0, 0, 0, 127, 0, 0, 0, 0, 8, 66, 117, 102, 102, 101, 114, 73, 100, 0, 0, 0, 1, ]; assert_eq!( (test_bytes.len(), Variant::BufferId(BufferId(1))), -- cgit v1.2.3