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,
{
}
pub trait CellConvert: Sized {
fn from_cell(raw: i32) -> Self;
fn into_cell(self) -> i32;
}
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) => {
#[allow(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::cast_lossless
)]
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
}
}
#[allow(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::cast_lossless
)]
impl CellConvert for $type {
#[inline]
fn from_cell(raw: i32) -> Self {
raw as Self
}
#[inline]
fn into_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> {
#[allow(clippy::cast_sign_loss)]
let bits = cell as u32;
Ok(f32::from_bits(bits))
}
fn as_cell(&self) -> i32 {
f32::to_bits(*self).cast_signed()
}
}
impl CellConvert for f32 {
#[inline]
fn from_cell(raw: i32) -> Self {
#[allow(clippy::cast_sign_loss)]
let bits = raw as u32;
f32::from_bits(bits)
}
#[inline]
fn into_cell(self) -> i32 {
f32::to_bits(self).cast_signed()
}
}
impl AmxCell<'_> for bool {
fn from_raw(_amx: &Amx, cell: i32) -> AmxResult<bool> {
Ok(cell != 0)
}
fn as_cell(&self) -> i32 {
i32::from(*self)
}
}
impl CellConvert for bool {
#[inline]
fn from_cell(raw: i32) -> Self {
raw != 0
}
#[inline]
fn into_cell(self) -> i32 {
i32::from(self)
}
}
unsafe impl AmxPrimitive for f32 {}
unsafe impl AmxPrimitive for bool {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn i32_as_cell_identity() {
for v in [0, 1, -1, 42, i32::MAX, i32::MIN] {
assert_eq!(v.as_cell(), v);
}
}
#[test]
fn f32_as_cell_preserves_bits() {
for v in [0.0f32, 1.0, -1.0, 42.5, f32::MAX, f32::MIN, f32::EPSILON] {
let cell = v.as_cell();
let recovered = f32::from_bits(cell.cast_unsigned());
assert_eq!(
v.to_bits(),
recovered.to_bits(),
"f32 {v} did not preserve bits"
);
}
}
#[test]
fn bool_as_cell() {
assert_eq!(true.as_cell(), 1);
assert_eq!(false.as_cell(), 0);
}
#[test]
fn u8_as_cell() {
assert_eq!(0u8.as_cell(), 0);
assert_eq!(255u8.as_cell(), 255);
}
#[test]
fn i8_as_cell() {
assert_eq!(0i8.as_cell(), 0);
assert_eq!((-1i8).as_cell(), -1);
assert_eq!(127i8.as_cell(), 127);
}
#[test]
fn u16_as_cell() {
assert_eq!(0u16.as_cell(), 0);
assert_eq!(65535u16.as_cell(), 65535);
}
#[test]
fn i16_as_cell() {
assert_eq!(0i16.as_cell(), 0);
assert_eq!((-1i16).as_cell(), -1);
}
#[test]
fn ref_delegates_to_inner() {
let val = 42i32;
let r = &val;
assert_eq!(r.as_cell(), 42);
}
#[test]
fn mut_ref_delegates_to_inner() {
let mut val = 42i32;
let r = &mut val;
assert_eq!(r.as_cell(), 42);
}
}