Skip to content

Commit

Permalink
cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
bmisiak committed Jul 22, 2024
1 parent 54db64f commit 3db20fc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
58 changes: 27 additions & 31 deletions src/amx_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ pub enum PassedArgument {
pub(crate) struct StackedCallback {
pub amx: Amx,
#[borrows(amx)]
#[covariant]
#[not_covariant]
pub allocator: Allocator<'this>,
#[borrows(amx)]
pub callback_idx: AmxExecIdx,
}
/*

impl StackedCallback {
/// ### SAFETY:
/// The `amx.exec()` here might call one of our natives
Expand All @@ -36,12 +37,12 @@ impl StackedCallback {
/// 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]
#[must_use]
pub unsafe fn execute(self) -> Result<i32, AmxError> {
self.amx.exec(self.callback_idx)
/// This is currently enforced by using thread_local
#[inline(always)]
pub fn execute(self) -> Result<i32, AmxError> {
self.with(|cb| cb.amx.exec(*cb.callback_idx))
}
}*/
}

#[derive(Debug, Clone)]
pub(crate) struct VariadicAmxArguments {
Expand Down Expand Up @@ -124,36 +125,31 @@ impl VariadicAmxArguments {
}

/// Push the arguments onto the AMX stack, in first-in-last-out order, i.e. reversed
pub fn push_onto_amx_stack<'cb, 'amx: 'cb>(
pub fn push_onto_amx_stack(
&self,
amx: Amx,
callback_idx: AmxExecIdx,
) -> Result<StackedCallback, AmxError> {

Ok(StackedCallbackBuilder {
amx: amx.clone(),
callback_idx,
allocator_builder: |amx| {
let allocator: Allocator = amx.allocator();
for param in self.inner.iter().rev() {
match param {
PassedArgument::PrimitiveCell(cell_value) => {
amx.push(cell_value).unwrap();
}
PassedArgument::Str(bytes) => {
let buffer = allocator.allot_buffer(bytes.len() + 1).unwrap();
let amx_str = unsafe { AmxString::new(buffer, bytes) };
amx.push(amx_str).unwrap();
}
PassedArgument::Array(array_cells) => {
let amx_buffer = allocator.allot_array(array_cells.as_slice()).unwrap();
amx.push(array_cells.len()).unwrap();
amx.push(amx_buffer).unwrap();
}
StackedCallback::try_new(amx.clone(), |amx| {
let allocator: Allocator = amx.allocator();
for param in self.inner.iter().rev() {
match param {
PassedArgument::PrimitiveCell(cell_value) => {
amx.push(cell_value)?;
}
PassedArgument::Str(bytes) => {
let buffer = allocator.allot_buffer(bytes.len() + 1)?;
let amx_str = unsafe { AmxString::new(buffer, bytes) };
amx.push(amx_str)?;
}
PassedArgument::Array(array_cells) => {
let amx_buffer = allocator.allot_array(array_cells.as_slice())?;
amx.push(array_cells.len())?;
amx.push(amx_buffer)?;
}
}
allocator
}
}.build())
Ok(allocator)
}, |_amx| Ok(callback_idx))
}
}
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ impl SampPlugin for PreciseTimers {
match callback {
Ok(stacked_callback) => {
// SAFETY: We are not holding any references to scheduling stores.
if let Err(exec_err) = stacked_callback
.with_amx(|amx| amx.exec(*stacked_callback.borrow_callback_idx()))
{
if let Err(exec_err) = stacked_callback.execute() {
error!("Error while executing timer: {exec_err}");
}
}
Expand Down

0 comments on commit 3db20fc

Please sign in to comment.