builds on stable

master
Avril 4 years ago
parent c6f3e75ccf
commit 003b454ea1
Signed by: flanchan
GPG Key ID: 284488987C31F630

1
Cargo.lock generated

@ -246,6 +246,7 @@ name = "lolicron"
version = "0.1.0"
dependencies = [
"futures",
"lazy_static",
"notify",
"sexp",
"tokio",

@ -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="*"

@ -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<Unit, u64> = {
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, ParseError>
{
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<Self, num::TryFromIntError>
pub fn try_from_ms(value: NonZeroU64) -> Result<Self, ParseError>
{
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<num::TryFromIntError> for ParseError
}
}
impl ParseError
{
/// Non-zero conversion error
const fn nz_err() -> Self
{
Self::InvalidNumber(None)
}
}
impl From<num::ParseIntError> for ParseError
{
#[allow(unused_variables)]

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

Loading…
Cancel
Save