Skip to content

Commit

Permalink
cleanup sendprop array/vector comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Apr 7, 2024
1 parent c7a7ee0 commit 43deaac
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/demo/sendprop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,27 +604,17 @@ impl PartialEq for SendPropValue {
(SendPropValue::VectorXY(value1), SendPropValue::Vector(value2)) => {
value1.x == value2.x && value1.y == value2.y && value2.z == 0.0
}
(SendPropValue::Vector(value1), SendPropValue::Array(value2)) if value2.len() == 3 => {
SendPropValue::Float(value1.x) == value2[0]
&& SendPropValue::Float(value1.y) == value2[1]
&& SendPropValue::Float(value1.z) == value2[2]
(SendPropValue::Vector(value1), SendPropValue::Array(value2)) => {
value1 == value2.as_slice()
}
(SendPropValue::Array(value1), SendPropValue::Vector(value2)) if value1.len() == 3 => {
SendPropValue::Float(value2.x) == value1[0]
&& SendPropValue::Float(value2.y) == value1[1]
&& SendPropValue::Float(value2.z) == value1[2]
(SendPropValue::Array(value1), SendPropValue::Vector(value2)) => {
value2 == value1.as_slice()
}
(SendPropValue::VectorXY(value1), SendPropValue::Array(value2))
if value2.len() == 2 =>
{
SendPropValue::Float(value1.x) == value2[0]
&& SendPropValue::Float(value1.y) == value2[1]
(SendPropValue::VectorXY(value1), SendPropValue::Array(value2)) => {
value1 == value2.as_slice()
}
(SendPropValue::Array(value1), SendPropValue::VectorXY(value2))
if value1.len() == 2 =>
{
SendPropValue::Float(value2.x) == value1[0]
&& SendPropValue::Float(value2.y) == value1[1]
(SendPropValue::Array(value1), SendPropValue::VectorXY(value2)) => {
value2 == value1.as_slice()
}
_ => false,
}
Expand Down
56 changes: 56 additions & 0 deletions src/demo/vector.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::demo::sendprop::SendPropValue;
use bitbuffer::{BitRead, BitWrite};
use parse_display::Display;
use serde::{Deserialize, Serialize};
Expand All @@ -24,6 +25,39 @@ impl PartialEq for Vector {
}
}

impl PartialEq<[SendPropValue]> for Vector {
fn eq(&self, other: &[SendPropValue]) -> bool {
match other {
&[SendPropValue::Float(x), SendPropValue::Float(y), SendPropValue::Float(z)] => {
self.x == x && self.y == y && self.z == z
}
_ => false,
}
}
}

#[test]
fn test_vec_array_eq() {
assert!(!Vector {
x: 1.0,
y: 2.0,
z: 3.0,
}
.eq([SendPropValue::Float(1.0), SendPropValue::Float(2.0)].as_slice()));

assert!(Vector {
x: 1.0,
y: 2.0,
z: 3.0,
}
.eq([
SendPropValue::Float(1.0),
SendPropValue::Float(2.0),
SendPropValue::Float(3.0),
]
.as_slice()));
}

impl Add for Vector {
type Output = Vector;

Expand Down Expand Up @@ -62,6 +96,28 @@ impl PartialEq for VectorXY {
}
}

impl PartialEq<[SendPropValue]> for VectorXY {
fn eq(&self, other: &[SendPropValue]) -> bool {
match other {
&[SendPropValue::Float(x), SendPropValue::Float(y)] => self.x == x && self.y == y,
_ => false,
}
}
}

#[test]
fn test_vec_xy_array_eq() {
assert!(VectorXY { x: 1.0, y: 2.0 }
.eq([SendPropValue::Float(1.0), SendPropValue::Float(2.0)].as_slice()));

assert!(!VectorXY { x: 1.0, y: 2.0 }.eq([
SendPropValue::Float(1.0),
SendPropValue::Float(2.0),
SendPropValue::Float(3.0)
]
.as_slice()));
}

impl From<Vector> for VectorXY {
fn from(vec: Vector) -> Self {
VectorXY { x: vec.x, y: vec.y }
Expand Down

0 comments on commit 43deaac

Please sign in to comment.