1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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 {}