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
95
96
97
98
99
100
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use crate::amx::Amx;
use crate::error::AmxResult;
pub mod buffer;
pub mod repr;
pub mod string;
pub use buffer::{Buffer, UnsizedBuffer};
pub use repr::{AmxCell, AmxPrimitive};
pub use string::AmxString;
pub struct Ref<'amx, T: Sized + AmxPrimitive> {
amx_addr: i32,
phys_addr: *mut T,
marker: PhantomData<&'amx Amx>,
}
impl<'amx, T: Sized + AmxPrimitive> Ref<'amx, T> {
pub unsafe fn new(amx_addr: i32, phys_addr: *mut T) -> Ref<'amx, T> {
Ref {
amx_addr,
phys_addr,
marker: PhantomData,
}
}
#[inline]
pub fn address(&self) -> i32 {
self.amx_addr
}
#[inline]
pub fn as_ptr(&self) -> *const T {
self.phys_addr
}
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut T {
self.phys_addr
}
}
impl<T: Sized + AmxPrimitive> Deref for Ref<'_, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.phys_addr }
}
}
impl<T: Sized + AmxPrimitive> DerefMut for Ref<'_, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.phys_addr }
}
}
impl<'amx, T: Sized + AmxPrimitive> AmxCell<'amx> for Ref<'amx, T> {
fn from_raw(amx: &'amx Amx, cell: i32) -> AmxResult<Ref<'amx, T>> {
amx.get_ref(cell)
}
fn as_cell(&self) -> i32 {
self.address()
}
}