aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoey Sacchini <joey@sacchini.net>2020-12-07 18:08:36 -0500
committerJoey Sacchini <joey@sacchini.net>2020-12-07 18:08:36 -0500
commit1db5a578e238ffe9c57b7a9c3be97dbf62f4f36e (patch)
tree1e0935f6434e05f22da086b8586276bb788d3ece
parent2ce7b7b52b3ec2dd8a4b7aa08c9b341f31fbf745 (diff)
downloadmcproto-rs-1db5a578e238ffe9c57b7a9c3be97dbf62f4f36e.tar.gz
mcproto-rs-1db5a578e238ffe9c57b7a9c3be97dbf62f4f36e.tar.bz2
mcproto-rs-1db5a578e238ffe9c57b7a9c3be97dbf62f4f36e.zip
optionally use GAT with PacketKind
-rw-r--r--Cargo.toml1
-rw-r--r--src/lib.rs1
-rw-r--r--src/protocol.rs32
3 files changed, 30 insertions, 4 deletions
diff --git a/Cargo.toml b/Cargo.toml
index d8aae35..aa3ddd8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,6 +24,7 @@ default = [ "std", "bench", "v1_15_2", "v1_16_3" ]
std = [ "rand" ]
bench = []
+gat = []
v1_15_2 = []
v1_16_3 = [] \ No newline at end of file
diff --git a/src/lib.rs b/src/lib.rs
index fd742f3..851e52a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,5 @@
#![cfg_attr(feature = "bench", feature(test))]
+#![cfg_attr(feature = "gat", feature(generic_associated_types))]
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
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),* });)*
};
}