From dc1e82b0b794272aa68b5a7aa65a22d0622dabb1 Mon Sep 17 00:00:00 2001 From: Brian Misiak Date: Fri, 5 Jul 2024 16:18:42 -0700 Subject: [PATCH] smol readablity changes --- Cargo.lock | 7 +++++++ Cargo.toml | 3 ++- src/amx_arguments.rs | 1 + src/lib.rs | 24 ++++++++++++------------ src/scheduling.rs | 15 ++++++++------- src/timer.rs | 6 +++--- 6 files changed, 33 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7478474..003f8ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,12 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" +[[package]] +name = "durr" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6150896f7f231d85fb8b13d5ef6974bbfae83cd857a0f5bd5ec5c856da32f5b" + [[package]] name = "fern" version = "0.6.2" @@ -104,6 +110,7 @@ dependencies = [ name = "samp-precise-timers" version = "1.0.0" dependencies = [ + "durr", "fern", "fnv", "log", diff --git a/Cargo.toml b/Cargo.toml index 280a8e5..3ee6d7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ log = "0.4.6" fern = { version = "0.6", features = [] } fnv = "1.0.7" snafu = "0.8.0" +durr = "2" [profile.release] -lto = true \ No newline at end of file +lto = true diff --git a/src/amx_arguments.rs b/src/amx_arguments.rs index f1686f1..726dccc 100644 --- a/src/amx_arguments.rs +++ b/src/amx_arguments.rs @@ -29,6 +29,7 @@ impl<'amx> StackedCallback<'amx> { /// the scheduling store(s) e.g. `TIMERS` and `QUEUE`. /// To avoid aliasing, there MUST NOT be any /// active references to them when this is called. + #[inline] pub unsafe fn execute(self) -> Result { self.amx.exec(self.callback_idx) } diff --git a/src/lib.rs b/src/lib.rs index 429ae32..269f327 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,8 @@ #![warn(clippy::pedantic)] use amx_arguments::VariadicAmxArguments; +use durr::{now, Durr}; use log::{error, info}; - use samp::amx::Amx; use samp::cell::AmxString; use samp::error::{AmxError, AmxResult}; @@ -34,13 +34,13 @@ impl PreciseTimers { #[samp::native(raw, name = "SetPreciseTimer")] pub fn create(&self, amx: &Amx, mut args: samp::args::Args) -> AmxResult { // Get the basic, mandatory timer parameters - let callback_name = args.next::().ok_or(AmxError::Params)?; + let callback_name: AmxString = args.next().ok_or(AmxError::Params)?; let interval = args - .next::() - .and_then(|ms| u64::try_from(ms).ok()) - .ok_or(AmxError::Params) - .map(Duration::from_millis)?; - let repeat = args.next::().ok_or(AmxError::Params)?; + .next() + .and_then(|ms: i32| u64::try_from(ms).ok()) + .ok_or(AmxError::Params)? + .milliseconds(); + let repeat: bool = args.next().ok_or(AmxError::Params)?; let passed_arguments = VariadicAmxArguments::from_amx_args::<3>(args)?; let timer = Timer { @@ -50,7 +50,7 @@ impl PreciseTimers { }; let key = insert_and_schedule_timer(timer, |key| Schedule { key, - next_trigger: Instant::now() + interval, + next_trigger: now() + interval, repeat: if repeat { Every(interval) } else { DontRepeat }, }); // The timer's slot in Slab<> incresed by 1, so that 0 signifies an invalid timer in PAWN @@ -91,12 +91,12 @@ impl PreciseTimers { ) -> AmxResult { let key = timer_number - 1; let interval = u64::try_from(interval) - .map_err(|_| AmxError::Params) - .map(Duration::from_millis)?; + .map_err(|_| AmxError::Params)? + .milliseconds(); let schedule = Schedule { key, - next_trigger: Instant::now() + interval, + next_trigger: now() + interval, repeat: if repeat { Every(interval) } else { DontRepeat }, }; if let Err(error) = reschedule_timer(key, schedule) { @@ -119,7 +119,7 @@ impl SampPlugin for PreciseTimers { #[allow(clippy::inline_always)] #[inline(always)] fn process_tick(&self) { - let now = Instant::now(); + let now = now(); while let Some(callback) = reschedule_next_due_and_then(now, Timer::stack_callback_on_amx) { match callback { diff --git a/src/scheduling.rs b/src/scheduling.rs index e83bf85..c6195b8 100644 --- a/src/scheduling.rs +++ b/src/scheduling.rs @@ -133,13 +133,14 @@ pub(crate) fn reschedule_next_due_and_then( mod test { use std::ptr::null_mut; + use durr::{now, Durr}; use samp::raw::types::AMX; use crate::schedule::Repeat::{DontRepeat, Every}; use crate::scheduling::{State, STATE}; use crate::Timer; use crate::{amx_arguments::VariadicAmxArguments, scheduling::reschedule_next_due_and_then}; - use std::time::{Duration, Instant}; + use std::time::Instant; use super::{insert_and_schedule_timer, Schedule}; @@ -157,15 +158,15 @@ mod test { fn every_1s(key: usize) -> Schedule { Schedule { key, - next_trigger: Instant::now() + Duration::from_secs(key as u64), - repeat: Every(Duration::from_secs(1)), + next_trigger: now() + (key as u64).seconds(), + repeat: Every(1.seconds()), } } fn dont_repeat(key: usize) -> Schedule { Schedule { key, - next_trigger: Instant::now() + Duration::from_secs(key as u64), + next_trigger: now() + (key as u64).seconds(), repeat: DontRepeat, } } @@ -177,7 +178,7 @@ mod test { #[test] fn hello() { - assert_eq!(reschedule_next_due_and_then(Instant::now(), noop), None); + assert_eq!(reschedule_next_due_and_then(now(), noop), None); let first = insert_and_schedule_timer(empty_timer(), every_1s); let second = insert_and_schedule_timer(empty_timer(), every_1s); let third = insert_and_schedule_timer(empty_timer(), every_1s); @@ -185,10 +186,10 @@ mod test { STATE.with_borrow_mut(|&mut State { ref mut queue, .. }| { assert_eq!(timer_keys(queue), [fourth, third, second, first]); }); - assert!(reschedule_next_due_and_then(Instant::now(), noop).is_some()); + assert!(reschedule_next_due_and_then(now(), noop).is_some()); STATE.with_borrow_mut(|&mut State { ref mut queue, .. }| { assert_eq!(timer_keys(queue), [fourth, third, first, second]); }); - assert_eq!(reschedule_next_due_and_then(Instant::now(), noop), None); + assert_eq!(reschedule_next_due_and_then(now(), noop), None); } } diff --git a/src/timer.rs b/src/timer.rs index 87260ae..a0eab86 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -1,7 +1,7 @@ use crate::amx_arguments::{StackedCallback, VariadicAmxArguments}; use samp::{ - amx::{Amx, AmxIdent}, + amx::{self, Amx, AmxIdent}, consts::AmxExecIdx, error::AmxError, }; @@ -15,13 +15,13 @@ pub(crate) struct Timer { } impl Timer { - pub fn was_scheduled_by_amx(&self, amx: &samp::amx::Amx) -> bool { + pub fn was_scheduled_by_amx(&self, amx: &amx::Amx) -> bool { self.amx_identifier == AmxIdent::from(amx.amx().as_ptr()) } pub fn stack_callback_on_amx<'amx>(&self) -> Result, AmxError> { // amx::get returns an invalid amx lifetime - let amx: &'amx Amx = samp::amx::get(self.amx_identifier).ok_or(AmxError::NotFound)?; + let amx: &'amx Amx = amx::get(self.amx_identifier).ok_or(AmxError::NotFound)?; self.passed_arguments .push_onto_amx_stack(amx, self.amx_callback_index) }