From 9a8d3572eb30c875199efe6bd7c22ac31e781dd9 Mon Sep 17 00:00:00 2001 From: Ben Reeves Date: Mon, 17 Jan 2022 22:59:25 -0600 Subject: [PATCH] Derive Clone, PartialEq, Eq, and Hash where appropriate. This is really useful for anyone who wishes to use the steven_protocol crate and write tests that compare packets to expected values. --- protocol/src/format.rs | 8 ++--- protocol/src/item.rs | 2 +- protocol/src/nbt/mod.rs | 4 +-- protocol/src/protocol/forge.rs | 14 ++++----- protocol/src/protocol/mod.rs | 29 ++++++++++-------- protocol/src/protocol/mojang.rs | 2 +- protocol/src/protocol/packet.rs | 54 ++++++++++++++++----------------- protocol/src/types/bit/set.rs | 2 +- protocol/src/types/metadata.rs | 10 +++--- protocol/src/types/mod.rs | 2 +- src/chunk_builder.rs | 2 +- 11 files changed, 67 insertions(+), 62 deletions(-) diff --git a/protocol/src/format.rs b/protocol/src/format.rs index 6f433c1..c3cc9d0 100644 --- a/protocol/src/format.rs +++ b/protocol/src/format.rs @@ -15,7 +15,7 @@ use std::fmt; use std::mem; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Component { Text(TextComponent), } @@ -115,7 +115,7 @@ impl Default for Component { } } -#[derive(Debug, Default, Clone)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct Modifier { pub extra: Option>, pub bold: Option, @@ -159,7 +159,7 @@ impl Modifier { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct TextComponent { pub text: String, pub modifier: Modifier, @@ -199,7 +199,7 @@ impl fmt::Display for TextComponent { } } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Color { Black, DarkBlue, diff --git a/protocol/src/item.rs b/protocol/src/item.rs index 8851dd5..f48d6c1 100644 --- a/protocol/src/item.rs +++ b/protocol/src/item.rs @@ -17,7 +17,7 @@ use crate::protocol::{self, Serializable}; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use std::io; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct Stack { pub id: isize, pub count: isize, diff --git a/protocol/src/nbt/mod.rs b/protocol/src/nbt/mod.rs index 7ddd5d5..7d2d638 100644 --- a/protocol/src/nbt/mod.rs +++ b/protocol/src/nbt/mod.rs @@ -20,7 +20,7 @@ use super::protocol; use super::protocol::Serializable; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum Tag { End, Byte(i8), @@ -37,7 +37,7 @@ pub enum Tag { LongArray(Vec), } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct NamedTag(pub String, pub Tag); impl Tag { diff --git a/protocol/src/protocol/forge.rs b/protocol/src/protocol/forge.rs index 254a45c..2409ad4 100644 --- a/protocol/src/protocol/forge.rs +++ b/protocol/src/protocol/forge.rs @@ -5,7 +5,7 @@ use std::io; use super::{Error, LenPrefixed, Serializable, VarInt}; -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Phase { // Client handshake states (written) Start, @@ -44,7 +44,7 @@ impl Serializable for Phase { } } -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct ForgeMod { pub modid: String, pub version: String, @@ -64,7 +64,7 @@ impl Serializable for ForgeMod { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct ModIdMapping { pub name: String, pub id: VarInt, @@ -87,7 +87,7 @@ impl Serializable for ModIdMapping { pub static BLOCK_NAMESPACE: &str = "\u{1}"; pub static ITEM_NAMESPACE: &str = "\u{2}"; -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum FmlHs { ServerHello { fml_protocol_version: i8, @@ -196,7 +196,7 @@ pub mod fml2 { // https://wiki.vg/Minecraft_Forge_Handshake#FML2_protocol_.281.13_-_Current.29 use super::*; - #[derive(Clone, Default, Debug)] + #[derive(Clone, Default, Debug, PartialEq, Eq)] pub struct Channel { pub name: String, pub version: String, @@ -216,7 +216,7 @@ pub mod fml2 { } } - #[derive(Clone, Default, Debug)] + #[derive(Clone, Default, Debug, PartialEq, Eq)] pub struct Registry { pub name: String, pub marker: String, @@ -236,7 +236,7 @@ pub mod fml2 { } } - #[derive(Debug)] + #[derive(Debug, Clone, PartialEq, Eq)] pub enum FmlHandshake { ModList { mod_names: LenPrefixed, diff --git a/protocol/src/protocol/mod.rs b/protocol/src/protocol/mod.rs index dfb4266..1c8ad36 100644 --- a/protocol/src/protocol/mod.rs +++ b/protocol/src/protocol/mod.rs @@ -75,7 +75,7 @@ macro_rules! state_packets { use crate::protocol::*; use std::io; - #[derive(Debug)] + #[derive(Debug, Clone, PartialEq)] pub enum Packet { $( $( @@ -107,7 +107,7 @@ macro_rules! state_packets { } $( - #[derive(Default, Debug)] + #[derive(Default, Debug, Clone, PartialEq)] $(#[$attr])* pub struct $name { $($(#[$fattr])* pub $field: $field_type),+, } @@ -418,7 +418,7 @@ impl Serializable for f64 { #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub struct UUID(u64, u64); -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct UUIDParseError; impl std::error::Error for UUIDParseError {} @@ -469,6 +469,7 @@ impl Serializable for UUID { } } +#[derive(Clone, PartialEq, Eq)] pub struct Biomes3D { pub data: [i32; 1024], } @@ -511,6 +512,7 @@ pub trait Lengthable: Serializable + Copy + Default { fn from_len(_: usize) -> Self; } +#[derive(Clone, PartialEq, Eq)] pub struct LenPrefixed { len: L, pub data: Vec, @@ -566,6 +568,7 @@ impl fmt::Debug for LenPrefixed { } // Optimization +#[derive(Clone, PartialEq, Eq)] pub struct LenPrefixedBytes { len: L, pub data: Vec, @@ -662,7 +665,7 @@ impl Lengthable for i32 { use num_traits::cast::{cast, NumCast}; /// `FixedPoint5` has the 5 least-significant bits for the fractional /// part, upper for integer part: https://wiki.vg/Data_types#Fixed-point_numbers -#[derive(Clone, Copy)] +#[derive(Clone, Copy, PartialEq, Eq)] pub struct FixedPoint5(T); impl Serializable for FixedPoint5 { @@ -708,7 +711,7 @@ where } /// `FixedPoint12` is like `FixedPoint5` but the fractional part is 12-bit -#[derive(Clone, Copy)] +#[derive(Clone, Copy, PartialEq, Eq)] pub struct FixedPoint12(T); impl Serializable for FixedPoint12 { @@ -755,7 +758,7 @@ where /// `VarInt` have a variable size (between 1 and 5 bytes) when encoded based /// on the size of the number -#[derive(Clone, Copy)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct VarInt(pub i32); impl Lengthable for VarInt { @@ -818,7 +821,7 @@ impl fmt::Debug for VarInt { /// `VarShort` have a variable size (2 or 3 bytes) and are backwards-compatible /// with vanilla shorts, used for Forge custom payloads -#[derive(Clone, Copy)] +#[derive(Clone, Copy, PartialEq, Eq)] pub struct VarShort(pub i32); impl Lengthable for VarShort { @@ -881,7 +884,7 @@ impl fmt::Debug for VarShort { /// `VarLong` have a variable size (between 1 and 10 bytes) when encoded based /// on the size of the number -#[derive(Clone, Copy)] +#[derive(Clone, Copy, PartialEq, Eq)] pub struct VarLong(pub i64); impl Lengthable for VarLong { @@ -963,7 +966,7 @@ impl Serializable for Position { /// Direction is used to define whether packets are going to the /// server or the client. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Direction { Serverbound, Clientbound, @@ -1436,7 +1439,7 @@ pub fn try_parse_packet(ibuf: Vec, protocol_version: i32) { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Status { pub version: StatusVersion, pub players: StatusPlayers, @@ -1446,20 +1449,20 @@ pub struct Status { pub fml_network_version: Option, } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct StatusVersion { pub name: String, pub protocol: i32, } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct StatusPlayers { pub max: i32, pub online: i32, pub sample: Vec, } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct StatusPlayer { name: String, id: String, diff --git a/protocol/src/protocol/mojang.rs b/protocol/src/protocol/mojang.rs index 8b7e50a..0c852e6 100644 --- a/protocol/src/protocol/mojang.rs +++ b/protocol/src/protocol/mojang.rs @@ -16,7 +16,7 @@ use serde_json::json; use sha1::{self, Digest}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct Profile { pub username: String, pub id: String, diff --git a/protocol/src/protocol/packet.rs b/protocol/src/protocol/packet.rs index 31383d6..baee55f 100644 --- a/protocol/src/protocol/packet.rs +++ b/protocol/src/protocol/packet.rs @@ -2415,7 +2415,7 @@ state_packets!( } ); -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct SpawnProperty { pub name: String, pub value: String, @@ -2438,7 +2438,7 @@ impl Serializable for SpawnProperty { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct Statistic { pub name: String, pub value: VarInt, @@ -2458,7 +2458,7 @@ impl Serializable for Statistic { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct BlockChangeRecord { pub xz: u8, pub y: u8, @@ -2481,7 +2481,7 @@ impl Serializable for BlockChangeRecord { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct ChunkMeta { pub x: i32, pub z: i32, @@ -2504,7 +2504,7 @@ impl Serializable for ChunkMeta { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct ExplosionRecord { pub x: i8, pub y: i8, @@ -2527,7 +2527,7 @@ impl Serializable for ExplosionRecord { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct MapIcon { pub direction_type: i8, pub x: i8, @@ -2560,7 +2560,7 @@ impl Default for MapIcon { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct Advancement { pub id: String, pub parent_id: Option, @@ -2613,7 +2613,7 @@ impl Serializable for Advancement { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct AdvancementDisplay { pub title: String, pub description: String, @@ -2666,7 +2666,7 @@ impl Serializable for AdvancementDisplay { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct AdvancementProgress { pub id: String, pub criteria: LenPrefixed, @@ -2686,7 +2686,7 @@ impl Serializable for AdvancementProgress { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct CriterionProgress { pub id: String, pub date_of_achieving: Option, @@ -2714,7 +2714,7 @@ impl Serializable for CriterionProgress { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct EntityEquipment { pub slot: u8, pub item: Option, @@ -2735,7 +2735,7 @@ impl Serializable for EntityEquipment { } // Top-bit terminated array of EntityEquipment -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct EntityEquipments { pub equipments: Vec, } @@ -2765,7 +2765,7 @@ impl Serializable for EntityEquipments { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct EntityProperty { pub key: String, pub value: f64, @@ -2788,7 +2788,7 @@ impl Serializable for EntityProperty { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct EntityProperty_i16 { pub key: String, pub value: f64, @@ -2811,7 +2811,7 @@ impl Serializable for EntityProperty_i16 { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct PropertyModifier { pub uuid: UUID, pub amount: f64, @@ -2834,7 +2834,7 @@ impl Serializable for PropertyModifier { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub struct PlayerInfoData { pub action: VarInt, pub players: Vec, @@ -2920,7 +2920,7 @@ impl Default for PlayerInfoData { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum PlayerDetail { Add { uuid: UUID, @@ -2947,7 +2947,7 @@ pub enum PlayerDetail { }, } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct PlayerProperty { pub name: String, pub value: String, @@ -2957,7 +2957,7 @@ pub struct PlayerProperty { use crate::item; type RecipeIngredient = LenPrefixed>; -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub enum RecipeData { Shapeless { group: String, @@ -3031,7 +3031,7 @@ impl Default for RecipeData { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct Recipe { pub id: String, pub ty: String, @@ -3153,7 +3153,7 @@ impl Serializable for Recipe { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct Tags { pub tag_name: String, pub entries: LenPrefixed, @@ -3172,7 +3172,7 @@ impl Serializable for Tags { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct TagsGroup { pub tag_type: String, pub tags: LenPrefixed, @@ -3191,7 +3191,7 @@ impl Serializable for TagsGroup { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct Trade { pub input_item_1: Option, pub output_item: Option, @@ -3234,7 +3234,7 @@ impl Serializable for Trade { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct CommandNode { pub flags: u8, pub children: LenPrefixed, @@ -3245,14 +3245,14 @@ pub struct CommandNode { pub suggestions_type: Option, } -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] enum CommandNodeType { Root, Literal, Argument, } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub enum CommandProperty { Bool, Double { @@ -3484,7 +3484,7 @@ impl Serializable for CommandNode { } } -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default, PartialEq)] pub struct NumberedSlot { pub slot_number: i16, pub slot_data: Option, diff --git a/protocol/src/types/bit/set.rs b/protocol/src/types/bit/set.rs index 14cf4fc..c5bb724 100644 --- a/protocol/src/types/bit/set.rs +++ b/protocol/src/types/bit/set.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct Set { data: Vec, } diff --git a/protocol/src/types/metadata.rs b/protocol/src/types/metadata.rs index 1c8cec9..b760189 100644 --- a/protocol/src/types/metadata.rs +++ b/protocol/src/types/metadata.rs @@ -24,6 +24,7 @@ use std::fmt; use std::io; use std::marker::PhantomData; +#[derive(Clone, Copy, PartialEq, Eq)] pub struct MetadataKey { index: i32, ty: PhantomData, @@ -39,6 +40,7 @@ impl MetadataKey { } } +#[derive(Clone, PartialEq)] pub struct Metadata { map: HashMap, } @@ -491,7 +493,7 @@ impl Default for Metadata { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub enum Value { Byte(i8), Short(i16), @@ -516,7 +518,7 @@ pub enum Value { Pose(PoseData), } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] pub enum ParticleData { AmbientEntityEffect, AngryVillager, @@ -655,7 +657,7 @@ impl Serializable for ParticleData { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] #[allow(dead_code)] pub struct VillagerData { villager_type: protocol::VarInt, @@ -680,7 +682,7 @@ impl Serializable for VillagerData { } } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum PoseData { Standing, FallFlying, diff --git a/protocol/src/types/mod.rs b/protocol/src/types/mod.rs index 4eb41fd..b646648 100644 --- a/protocol/src/types/mod.rs +++ b/protocol/src/types/mod.rs @@ -19,7 +19,7 @@ pub mod bit; pub mod hash; pub mod nibble; -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Gamemode { Survival = 0, Creative = 1, diff --git a/src/chunk_builder.rs b/src/chunk_builder.rs index 2f4dccd..cd17afe 100644 --- a/src/chunk_builder.rs +++ b/src/chunk_builder.rs @@ -393,7 +393,7 @@ fn flood_fill(snapshot: &world::Snapshot, visited: &mut Set, x: i32, y: i32, z: touched } -#[derive(Clone, Copy, Default)] +#[derive(Clone, Copy, Default, PartialEq, Eq)] pub struct CullInfo(u64); impl CullInfo {