Skip to content

Commit

Permalink
Simplify TlPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Nov 28, 2024
1 parent cbe5a50 commit 7d98717
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
6 changes: 3 additions & 3 deletions proc/src/tl_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,10 @@ where

if field.attrs.signature {
quote! {
if <P_ as _tl_proto::TlPacket>::TARGET == _tl_proto::TlTarget::Packet {
#write_to
} else {
if _tl_proto::__internal::unlikely(<P_ as _tl_proto::TlPacket>::ignore_signature(__packet)) {
<&[u8] as _tl_proto::TlWrite>::write_to::<P_>(&[].as_ref(), __packet);
} else {
#write_to
}
}
} else {
Expand Down
10 changes: 8 additions & 2 deletions src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ impl<T> 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) {
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,8 @@ where
{
hash(data.into_boxed())
}

#[doc(hidden)]
pub mod __internal {
pub use crate::util::unlikely;
}
28 changes: 14 additions & 14 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -148,7 +148,10 @@ pub trait TlPacket {
}

impl TlPacket for Vec<u8> {
const TARGET: TlTarget = TlTarget::Packet;
#[inline(always)]
fn ignore_signature(&self) -> bool {
false
}

#[inline(always)]
fn write_u32(&mut self, data: u32) {
Expand Down Expand Up @@ -178,7 +181,10 @@ impl TlPacket for Vec<u8> {

#[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) {
Expand Down Expand Up @@ -241,7 +247,10 @@ impl<W> IoWriter<W> {
}

impl<W: std::io::Write> TlPacket for IoWriter<W> {
const TARGET: TlTarget = TlTarget::Packet;
#[inline(always)]
fn ignore_signature(&self) -> bool {
false
}

#[inline(always)]
fn write_u32(&mut self, data: u32) {
Expand Down Expand Up @@ -275,15 +284,6 @@ impl<W: std::io::Write> TlPacket for IoWriter<W> {
}
}

/// 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<T> = Result<T, TlError>;

Expand Down

0 comments on commit 7d98717

Please sign in to comment.