-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use dedicated resource for server tick
- Loading branch information
Showing
6 changed files
with
80 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::cmp::Ordering; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
/// Wraps a value that represents an index of server update. | ||
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)] | ||
pub struct RepliconTick(pub(crate) u32); | ||
|
||
impl RepliconTick { | ||
/// Gets the value of this tick. | ||
#[inline] | ||
pub fn get(self) -> u32 { | ||
self.0 | ||
} | ||
|
||
/// Increments current tick by the specified `value` and takes wrapping into account. | ||
#[inline] | ||
pub fn increment_by(&mut self, value: u32) { | ||
self.0 = self.0.wrapping_add(value); | ||
} | ||
|
||
/// Same as [`Self::increment_by`], but increments only by 1. | ||
#[inline] | ||
pub fn increment(&mut self) { | ||
self.increment_by(1) | ||
} | ||
} | ||
|
||
impl PartialOrd for RepliconTick { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
let difference = self.0.wrapping_sub(other.0); | ||
if difference == 0 { | ||
Some(Ordering::Equal) | ||
} else if difference > u32::MAX / 2 { | ||
Some(Ordering::Less) | ||
} else { | ||
Some(Ordering::Greater) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn tick_comparsion() { | ||
assert_eq!(RepliconTick(0), RepliconTick(0)); | ||
assert!(RepliconTick(0) < RepliconTick(1)); | ||
assert!(RepliconTick(0) > RepliconTick(u32::MAX)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters