Skip to content

Commit

Permalink
Derive Clone, PartialEq, Eq, and Hash where appropriate.
Browse files Browse the repository at this point in the history
This is really useful for anyone who wishes to use the steven_protocol
crate and write tests that compare packets to expected values.
  • Loading branch information
BGR360 authored and iceiix committed Feb 5, 2022
1 parent 00e7a3d commit 9a8d357
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 62 deletions.
8 changes: 4 additions & 4 deletions protocol/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::fmt;
use std::mem;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Component {
Text(TextComponent),
}
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Default for Component {
}
}

#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Modifier {
pub extra: Option<Vec<Component>>,
pub bold: Option<bool>,
Expand Down Expand Up @@ -159,7 +159,7 @@ impl Modifier {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextComponent {
pub text: String,
pub modifier: Modifier,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/nbt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -37,7 +37,7 @@ pub enum Tag {
LongArray(Vec<i64>),
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct NamedTag(pub String, pub Tag);

impl Tag {
Expand Down
14 changes: 7 additions & 7 deletions protocol/src/protocol/forge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -64,7 +64,7 @@ impl Serializable for ForgeMod {
}
}

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModIdMapping {
pub name: String,
pub id: VarInt,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -236,7 +236,7 @@ pub mod fml2 {
}
}

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FmlHandshake {
ModList {
mod_names: LenPrefixed<VarInt, String>,
Expand Down
29 changes: 16 additions & 13 deletions protocol/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ macro_rules! state_packets {
use crate::protocol::*;
use std::io;

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
pub enum Packet {
$(
$(
Expand Down Expand Up @@ -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),+,
}
Expand Down Expand Up @@ -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 {}

Expand Down Expand Up @@ -469,6 +469,7 @@ impl Serializable for UUID {
}
}

#[derive(Clone, PartialEq, Eq)]
pub struct Biomes3D {
pub data: [i32; 1024],
}
Expand Down Expand Up @@ -511,6 +512,7 @@ pub trait Lengthable: Serializable + Copy + Default {
fn from_len(_: usize) -> Self;
}

#[derive(Clone, PartialEq, Eq)]
pub struct LenPrefixed<L: Lengthable, V> {
len: L,
pub data: Vec<V>,
Expand Down Expand Up @@ -566,6 +568,7 @@ impl<L: Lengthable, V: fmt::Debug> fmt::Debug for LenPrefixed<L, V> {
}

// Optimization
#[derive(Clone, PartialEq, Eq)]
pub struct LenPrefixedBytes<L: Lengthable> {
len: L,
pub data: Vec<u8>,
Expand Down Expand Up @@ -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>(T);

impl<T: Serializable> Serializable for FixedPoint5<T> {
Expand Down Expand Up @@ -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>(T);

impl<T: Serializable> Serializable for FixedPoint12<T> {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1436,7 +1439,7 @@ pub fn try_parse_packet(ibuf: Vec<u8>, protocol_version: i32) {
}
}

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Status {
pub version: StatusVersion,
pub players: StatusPlayers,
Expand All @@ -1446,20 +1449,20 @@ pub struct Status {
pub fml_network_version: Option<i64>,
}

#[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<StatusPlayer>,
}

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatusPlayer {
name: String,
id: String,
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/protocol/mojang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 9a8d357

Please sign in to comment.