diff --git a/Cargo.lock b/Cargo.lock index 3d715c0..67e4730 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,6 +246,7 @@ name = "lolicron" version = "0.1.0" dependencies = [ "futures", + "lazy_static", "notify", "sexp", "tokio", diff --git a/Cargo.toml b/Cargo.toml index 821a5ad..09344e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,4 @@ tokio = {version = "0.2", features=["time", "macros", "io-driver", "sync", "rt-c notify = "4.0" futures= "0.3" sexp = "1.1" +lazy_static="*" \ No newline at end of file diff --git a/etc/other/included-file b/etc/other/included-file new file mode 100644 index 0000000..e69de29 diff --git a/src/interval.rs b/src/interval.rs index 324c8b5..e52564e 100644 --- a/src/interval.rs +++ b/src/interval.rs @@ -8,6 +8,7 @@ use std::{ iter::FromIterator, }; + /// Interval for job, in miliseconds. #[derive(Debug,Clone,PartialEq,Eq,Hash,Ord,PartialOrd)] pub enum Unit @@ -26,6 +27,7 @@ impl Unit { /// Multiplier to get to miliseconds #[inline] + #[cfg(nightly)] const fn multiplier(&self) -> u64 { use Unit::*; match self { @@ -38,6 +40,27 @@ impl Unit Aeon => Year.multiplier() * 1000000, } } + #[cfg(not(nightly))] + fn multiplier(&self) -> u64 { + use lazy_static::lazy_static; + use std::collections::HashMap; + + use Unit::*; + lazy_static! { + static ref MUL_TABLE: HashMap = { + let mut map = HashMap::with_capacity(8); + map.insert(Second, 1000); + map.insert(Minute, Second.multiplier() * 60); + map.insert(Hour, Minute.multiplier() * 60); + map.insert(Day, Hour.multiplier() * 24); + map.insert(Week, Day.multiplier() * 7); + map.insert(Year, Week.multiplier() * 52); + map.insert(Aeon, Year.multiplier() * 1000000); + map + }; + } + *MUL_TABLE.get(&self).unwrap() + } } /// A time object parsed from the definition file. @@ -68,7 +91,10 @@ impl Time { fn into_abs(mut self) -> Result { - self.absolute = Some(NonZeroU64::try_from(self.unit.multiplier().checked_mul(u64::from(self.value)).ok_or(ParseError::InvalidNumber(None))?)?); + self.absolute = Some(NonZeroU64::new(self.unit.multiplier() + .checked_mul(u64::from(self.value)) + .ok_or(ParseError::nz_err())?) + .ok_or(ParseError::nz_err())?); Ok(self) } @@ -92,7 +118,7 @@ impl Time /// If `value` is less than 1 second (1000ms) pub fn from_ms(value: NonZeroU64) -> Self { - let value_sec = NonZeroU64::try_from(u64::from(value) / 1000).expect("absolute `value` is too small for `Time`"); + let value_sec = NonZeroU64::new(u64::from(value) / 1000).expect("absolute `value` is too small for `Time`"); Self{ unit: Unit::Second, value: value_sec, @@ -101,9 +127,9 @@ impl Time } /// Attempt to create a new `Time` from miliseconds. - pub fn try_from_ms(value: NonZeroU64) -> Result + pub fn try_from_ms(value: NonZeroU64) -> Result { - let value_sec = NonZeroU64::try_from(u64::from(value) / 1000)?; + let value_sec = NonZeroU64::new(u64::from(value) / 1000).ok_or(ParseError::nz_err())?; Ok(Self{ unit: Unit::Second, value: value_sec, @@ -301,6 +327,15 @@ impl From for ParseError } } +impl ParseError +{ + /// Non-zero conversion error + const fn nz_err() -> Self + { + Self::InvalidNumber(None) + } +} + impl From for ParseError { #[allow(unused_variables)] diff --git a/src/main.rs b/src/main.rs index ce68936..6458f35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,8 +31,8 @@ async fn do_thing_every() -> Result<(oneshot::Sender<()>, task::JoinHandle<()>), // Do the things println!("yes"); } - _ = &mut rx => { - // We got cancel + command = &mut rx => { + // We got interrupt, interpret `command` here println!("no"); break; }