use crate::amx::Amx;
use crate::error::{AmxError, AmxResult};
pub trait AmxCell<'amx>
where
Self: Sized,
{
fn from_raw(_amx: &'amx Amx, _cell: i32) -> AmxResult<Self>
where
Self: 'amx,
{
Err(AmxError::General)
}
fn as_cell(&self) -> i32;
}
pub unsafe trait AmxPrimitive
where
Self: Sized,
{
}
impl<'a, T: AmxCell<'a>> AmxCell<'a> for &'a T {
fn as_cell(&self) -> i32 {
(**self).as_cell()
}
}
impl<'a, T: AmxCell<'a>> AmxCell<'a> for &'a mut T {
fn as_cell(&self) -> i32 {
(**self).as_cell()
}
}
macro_rules! impl_for_primitive {
($type:ty) => {
impl AmxCell<'_> for $type {
fn from_raw(_amx: &Amx, cell: i32) -> AmxResult<Self> {
Ok(cell as Self)
}
fn as_cell(&self) -> i32 {
*self as i32
}
}
unsafe impl AmxPrimitive for $type {}
};
}
impl_for_primitive!(i8);
impl_for_primitive!(u8);
impl_for_primitive!(i16);
impl_for_primitive!(u16);
impl_for_primitive!(i32);
impl_for_primitive!(u32);
impl_for_primitive!(usize);
impl_for_primitive!(isize);
impl AmxCell<'_> for f32 {
fn from_raw(_amx: &Amx, cell: i32) -> AmxResult<f32> {
Ok(f32::from_bits(cell as u32))
}
fn as_cell(&self) -> i32 {
unsafe { std::mem::transmute(*self) }
}
}
impl AmxCell<'_> for bool {
fn from_raw(_amx: &Amx, cell: i32) -> AmxResult<bool> {
Ok(cell != 0)
}
fn as_cell(&self) -> i32 {
i32::from(*self)
}
}
unsafe impl AmxPrimitive for f32 {}
unsafe impl AmxPrimitive for bool {}