Skip to content

Commit

Permalink
Add Wait type.
Browse files Browse the repository at this point in the history
  • Loading branch information
azriel91 committed Aug 7, 2020
1 parent 7d8f1bc commit eaf87c2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use crate::{
object_data_parser::{ObjectDataParser, Rule},
pic::Pic,
sprite_file::SpriteFile,
wait::Wait,
};

mod error;
Expand All @@ -19,3 +20,4 @@ mod object_data;
mod object_data_parser;
mod pic;
mod sprite_file;
mod wait;
48 changes: 48 additions & 0 deletions src/wait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::{
fmt::{self, Display},
num::{NonZeroU32, ParseIntError},
ops::{Deref, DerefMut},
str::FromStr,
};

/// Default wait value of 1.
pub const WAIT_DEFAULT: Wait = Wait(unsafe { NonZeroU32::new_unchecked(1) });

/// Represents the frame number.
#[derive(Clone, Copy, Debug)]
pub struct Wait(pub NonZeroU32);

impl Default for Wait {
fn default() -> Self {
WAIT_DEFAULT
}
}

impl Deref for Wait {
type Target = NonZeroU32;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Wait {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl Display for Wait {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl FromStr for Wait {
type Err = ParseIntError;

fn from_str(s: &str) -> Result<Wait, ParseIntError> {
s.parse::<u32>()
.map(|wait| NonZeroU32::new(wait).map(Self).unwrap_or(WAIT_DEFAULT))
}
}

0 comments on commit eaf87c2

Please sign in to comment.