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

add/sub for tick types

This commit is contained in:
Robin Appelman 2022-10-22 17:51:08 +02:00
commit 0c91b4b274

View file

@ -4,6 +4,7 @@ use bitbuffer::{BitRead, BitReadStream, BitWrite, BitWriteStream, Endianness};
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt::{Debug, Display, Formatter}; use std::fmt::{Debug, Display, Formatter};
use std::ops::{Add, Range, RangeInclusive, Sub};
pub use userinfo::UserInfo; pub use userinfo::UserInfo;
@ -147,6 +148,12 @@ impl schemars::JsonSchema for MaybeUtf8String {
)] )]
pub struct ServerTick(u32); pub struct ServerTick(u32);
impl ServerTick {
pub fn range_inclusive(&self, till: Self) -> impl Iterator<Item = Self> {
(self.0..=till.0).into_iter().map(Self::from)
}
}
impl PartialEq<u32> for ServerTick { impl PartialEq<u32> for ServerTick {
fn eq(&self, other: &u32) -> bool { fn eq(&self, other: &u32) -> bool {
*other == self.0 *other == self.0
@ -171,6 +178,38 @@ impl From<ServerTick> for u32 {
} }
} }
impl Add<u32> for ServerTick {
type Output = ServerTick;
fn add(self, rhs: u32) -> Self::Output {
ServerTick(self.0 + rhs)
}
}
impl Add<ServerTick> for ServerTick {
type Output = ServerTick;
fn add(self, rhs: ServerTick) -> Self::Output {
ServerTick(self.0 + rhs.0)
}
}
impl Sub<u32> for ServerTick {
type Output = ServerTick;
fn sub(self, rhs: u32) -> Self::Output {
ServerTick(self.0 - rhs)
}
}
impl Sub<ServerTick> for ServerTick {
type Output = ServerTick;
fn sub(self, rhs: ServerTick) -> Self::Output {
ServerTick(self.0 - rhs.0)
}
}
/// Tick relative to the start of the demo /// Tick relative to the start of the demo
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive( #[derive(
@ -189,6 +228,12 @@ impl From<ServerTick> for u32 {
)] )]
pub struct DemoTick(u32); pub struct DemoTick(u32);
impl DemoTick {
pub fn range_inclusive(&self, till: Self) -> impl Iterator<Item = Self> {
(self.0..=till.0).into_iter().map(Self::from)
}
}
impl PartialEq<u32> for DemoTick { impl PartialEq<u32> for DemoTick {
fn eq(&self, other: &u32) -> bool { fn eq(&self, other: &u32) -> bool {
*other == self.0 *other == self.0
@ -212,3 +257,35 @@ impl From<DemoTick> for u32 {
tick.0 tick.0
} }
} }
impl Add<u32> for DemoTick {
type Output = DemoTick;
fn add(self, rhs: u32) -> Self::Output {
DemoTick(self.0 + rhs)
}
}
impl Add<DemoTick> for DemoTick {
type Output = DemoTick;
fn add(self, rhs: DemoTick) -> Self::Output {
DemoTick(self.0 + rhs.0)
}
}
impl Sub<u32> for DemoTick {
type Output = DemoTick;
fn sub(self, rhs: u32) -> Self::Output {
DemoTick(self.0 - rhs)
}
}
impl Sub<DemoTick> for DemoTick {
type Output = DemoTick;
fn sub(self, rhs: DemoTick) -> Self::Output {
DemoTick(self.0 - rhs.0)
}
}