Skip to content

Commit

Permalink
smol readablity changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bmisiak committed Jul 6, 2024
1 parent 2f9bbd2 commit dc1e82b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
lto = true
1 change: 1 addition & 0 deletions src/amx_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32, AmxError> {
self.amx.exec(self.callback_idx)
}
Expand Down
24 changes: 12 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -34,13 +34,13 @@ impl PreciseTimers {
#[samp::native(raw, name = "SetPreciseTimer")]
pub fn create(&self, amx: &Amx, mut args: samp::args::Args) -> AmxResult<i32> {
// Get the basic, mandatory timer parameters
let callback_name = args.next::<AmxString>().ok_or(AmxError::Params)?;
let callback_name: AmxString = args.next().ok_or(AmxError::Params)?;
let interval = args
.next::<i32>()
.and_then(|ms| u64::try_from(ms).ok())
.ok_or(AmxError::Params)
.map(Duration::from_millis)?;
let repeat = args.next::<bool>().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 {
Expand All @@ -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
Expand Down Expand Up @@ -91,12 +91,12 @@ impl PreciseTimers {
) -> AmxResult<i32> {
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) {
Expand All @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions src/scheduling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,14 @@ pub(crate) fn reschedule_next_due_and_then<T>(
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};

Expand All @@ -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,
}
}
Expand All @@ -177,18 +178,18 @@ 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);
let fourth = insert_and_schedule_timer(empty_timer(), dont_repeat);
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);
}
}
6 changes: 3 additions & 3 deletions src/timer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::amx_arguments::{StackedCallback, VariadicAmxArguments};

use samp::{
amx::{Amx, AmxIdent},
amx::{self, Amx, AmxIdent},
consts::AmxExecIdx,
error::AmxError,
};
Expand All @@ -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<StackedCallback<'amx>, 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)
}
Expand Down

2 comments on commit dc1e82b

@nekjutsu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, but could you fix the bug with incorrect parameters?
Because of this the plugin is not possible to use at all

@bmisiak
Copy link
Owner Author

@bmisiak bmisiak commented on dc1e82b Jul 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my apologies, the issue escaped me. Let me look into it.

Please sign in to comment.