diff options
author | Joey Sacchini <joey@sacchini.net> | 2020-12-07 18:08:36 -0500 |
---|---|---|
committer | Joey Sacchini <joey@sacchini.net> | 2020-12-07 18:08:36 -0500 |
commit | 1db5a578e238ffe9c57b7a9c3be97dbf62f4f36e (patch) | |
tree | 1e0935f6434e05f22da086b8586276bb788d3ece /src/protocol.rs | |
parent | 2ce7b7b52b3ec2dd8a4b7aa08c9b341f31fbf745 (diff) | |
download | mcproto-rs-1db5a578e238ffe9c57b7a9c3be97dbf62f4f36e.tar.gz mcproto-rs-1db5a578e238ffe9c57b7a9c3be97dbf62f4f36e.tar.bz2 mcproto-rs-1db5a578e238ffe9c57b7a9c3be97dbf62f4f36e.zip |
optionally use GAT with PacketKind
Diffstat (limited to 'src/protocol.rs')
-rw-r--r-- | src/protocol.rs | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/src/protocol.rs b/src/protocol.rs index 8f155fc..f8873a1 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -94,6 +94,14 @@ pub trait HasPacketKind { } pub trait PacketKind: HasPacketId + Clone + Copy + PartialEq + Eq { + + #[cfg(feature = "gat")] + type RawPacket<'a>: RawPacket<'a>; + + fn from_id(id: Id) -> Option<Self>; + + #[cfg(feature = "gat")] + fn with_body_data<'a>(self, body: &'a [u8]) -> Self::RawPacket<'a>; } pub trait HasPacketId { @@ -351,6 +359,7 @@ macro_rules! define_protocol { type Packet = $packett; fn create(id: crate::protocol::Id, data: &'a[u8]) -> Result<Self, crate::protocol::PacketErr> { + use crate::protocol::PacketKind; if let Some(kind) = $kindt::from_id(id) { Ok(kind.with_body_data(data)) } else { @@ -419,15 +428,32 @@ macro_rules! define_protocol { } } - impl $kindt { - pub fn from_id(id: crate::protocol::Id) -> Option<Self> { + impl crate::protocol::PacketKind for $kindt { + #[cfg(feature = "gat")] + type RawPacket<'a> = $rawpackett<'a>; + + fn from_id(id: crate::protocol::Id) -> Option<Self> { match (id.id, id.state, id.direction) { $(($id, crate::protocol::State::$state, crate::protocol::PacketDirection::$direction) => Some($kindt::$nam)),*, _ => None } } + #[cfg(feature = "gat")] + fn with_body_data<'a>(self, data: &'a [u8]) -> Self::RawPacket<'a> { + self.with_body_data_inner(data) + } + } + + #[cfg(not(feature = "gat"))] + impl $kindt { pub fn with_body_data<'a>(self, data: &'a [u8]) -> $rawpackett<'a> { + self.with_body_data_inner(data) + } + } + + impl $kindt { + fn with_body_data_inner<'a>(self, data: &'a [u8]) -> $rawpackett<'a> { match self { $($kindt::$nam => $rawpackett::$nam($rawdt{ data, @@ -437,8 +463,6 @@ macro_rules! define_protocol { } } - impl crate::protocol::PacketKind for $kindt {} - $($crate::proto_struct!($body { $($fnam: $ftyp),* });)* }; } |