From 9b04250ec3051d262ebaf7b7e262507996118a67 Mon Sep 17 00:00:00 2001 From: Ivan Kalinin Date: Thu, 28 Nov 2024 17:01:12 +0100 Subject: [PATCH] Simplify `TlPacket` --- proc/src/tl_write.rs | 6 +++--- src/hasher.rs | 10 ++++++++-- src/lib.rs | 5 +++++ src/traits.rs | 28 ++++++++++++++-------------- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/proc/src/tl_write.rs b/proc/src/tl_write.rs index 48c3df6..419b2a6 100644 --- a/proc/src/tl_write.rs +++ b/proc/src/tl_write.rs @@ -342,10 +342,10 @@ where if field.attrs.signature { quote! { - if ::TARGET == _tl_proto::TlTarget::Packet { - #write_to - } else { + if _tl_proto::__internal::unlikely(::ignore_signature(__packet)) { <&[u8] as _tl_proto::TlWrite>::write_to::(&[].as_ref(), __packet); + } else { + #write_to } } } else { diff --git a/src/hasher.rs b/src/hasher.rs index 1d2d890..d398ec1 100644 --- a/src/hasher.rs +++ b/src/hasher.rs @@ -29,7 +29,10 @@ impl TlPacket for DigestWriter<'_, T> where T: digest::Update, { - const TARGET: TlTarget = TlTarget::Hasher; + #[inline(always)] + fn ignore_signature(&self) -> bool { + true + } #[inline(always)] fn write_u32(&mut self, data: u32) { @@ -69,7 +72,10 @@ where struct HashWriter<'a>(pub &'a mut dyn Hasher); impl<'a> TlPacket for HashWriter<'a> { - const TARGET: TlTarget = TlTarget::Hasher; + #[inline(always)] + fn ignore_signature(&self) -> bool { + true + } #[inline(always)] fn write_u32(&mut self, data: u32) { diff --git a/src/lib.rs b/src/lib.rs index a0b2697..1d00bb8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -132,3 +132,8 @@ where { hash(data.into_boxed()) } + +#[doc(hidden)] +pub mod __internal { + pub use crate::util::unlikely; +} diff --git a/src/traits.rs b/src/traits.rs index b5a6682..6608988 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -132,8 +132,8 @@ where /// TL packet interface. pub trait TlPacket { - /// TL packet type. - const TARGET: TlTarget; + /// Whether this packet type excludes signatures. + fn ignore_signature(&self) -> bool; /// Writes `u32` to the packet. fn write_u32(&mut self, data: u32); @@ -148,7 +148,10 @@ pub trait TlPacket { } impl TlPacket for Vec { - const TARGET: TlTarget = TlTarget::Packet; + #[inline(always)] + fn ignore_signature(&self) -> bool { + false + } #[inline(always)] fn write_u32(&mut self, data: u32) { @@ -178,7 +181,10 @@ impl TlPacket for Vec { #[cfg(feature = "bytes")] impl TlPacket for bytes::BytesMut { - const TARGET: TlTarget = TlTarget::Packet; + #[inline(always)] + fn ignore_signature(&self) -> bool { + false + } #[inline(always)] fn write_u32(&mut self, data: u32) { @@ -241,7 +247,10 @@ impl IoWriter { } impl TlPacket for IoWriter { - const TARGET: TlTarget = TlTarget::Packet; + #[inline(always)] + fn ignore_signature(&self) -> bool { + false + } #[inline(always)] fn write_u32(&mut self, data: u32) { @@ -275,15 +284,6 @@ impl TlPacket for IoWriter { } } -/// TL packet type. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub enum TlTarget { - /// Ordinary packet (bytes). - Packet, - /// Hasher packet (to compute TL hash without allocations). - Hasher, -} - /// TL result wrapper. pub type TlResult = Result;