1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 18:24:05 +02:00

cleanup sendprop array/vector comparisons

This commit is contained in:
Robin Appelman 2024-04-07 16:43:45 +02:00
commit dfd0b8ae90
2 changed files with 64 additions and 18 deletions

View file

@ -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,
}

View file

@ -1,3 +1,4 @@
use crate::demo::sendprop::SendPropValue;
use bitbuffer::{BitRead, BitWrite};
use parse_display::Display;
use serde::{Deserialize, Serialize};
@ -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;
@ -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 }