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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
macro_rules! make_thin_wrapper {
($name:ident, $t:ty, $dropfunc:expr) => {
make_thin_wrapper!($name, $t, $dropfunc, true);
};
($name:ident, $t:ty, $dropfunc:expr, false) => {
#[repr(transparent)]
#[derive(Debug)]
pub struct $name(pub(crate) $t);
impl_wrapper!($name, $t, $dropfunc, 0);
gen_from_raw_wrapper!($name, $t, $dropfunc, 0);
};
($name:ident, $t:ty, $dropfunc:expr, true) => {
#[repr(transparent)]
#[derive(Debug)]
pub struct $name(pub(crate) $t);
impl_wrapper!($name, $t, $dropfunc, 0);
deref_impl_wrapper!($name, $t, $dropfunc, 0);
gen_from_raw_wrapper!($name, $t, $dropfunc, 0);
};
}
macro_rules! make_thin_wrapper_lifetime {
($name:ident, $t1:ty, $t2:ty, $dropfunc:expr) => {
make_thin_wrapper_lifetime!($name, $t1, $t2, $dropfunc, true);
};
($name:ident, $t1:ty, $t2:ty,$dropfunc:expr, false) => {
#[derive(Debug)]
pub struct $name<'a>(pub(crate) $t1, &'a $t2);
impl_wrapper!($name, $t1, $dropfunc, 0);
};
($name:ident, $t1:ty, $t2:ty, $dropfunc:expr, true) => {
#[derive(Debug)]
pub struct $name<'a>(pub(crate) $t1, &'a $t2);
impl_wrapper!($name<'a>, $t1, $dropfunc, 0);
deref_impl_wrapper!($name<'a>, $t1, $dropfunc, 0);
};
}
macro_rules! impl_wrapper {
($name:ident$(<$lifetime:tt>)?, $t:ty, $dropfunc:expr, $rawfield:tt) => {
impl$(<$lifetime>)? $name$(<$lifetime>)? {
/// Take the raw ffi type. Must manually free memory by calling the proper unload function
///
/// # Safety
///
/// The returned raw value is no longer managed by Rust. The caller is responsible for
/// freeing it via the appropriate raylib unload function; otherwise memory will leak.
pub unsafe fn unwrap(self) -> $t {
let inner = self.$rawfield;
std::mem::forget(self);
inner
}
}
impl$(<$lifetime>)? Drop for $name$(<$lifetime>)? {
#[allow(unused_unsafe)]
fn drop(&mut self) {
unsafe {
($dropfunc)(self.$rawfield);
}
}
}
};
}
macro_rules! gen_from_raw_wrapper {
($name:ident$(<$lifetime:tt>)?, $t:ty, $dropfunc:expr, $rawfield:tt) => {
impl$(<$lifetime>)? $name$(<$lifetime>)? {
/// returns the unwrapped raylib-sys object
pub fn to_raw(self) -> $t {
let raw = self.$rawfield;
std::mem::forget(self);
raw
}
/// converts raylib-sys object to a "safe"
/// version. Make sure to call this function
/// from the thread the resource was created.
///
/// # Safety
///
/// The caller must ensure `raw` is a valid value produced by raylib and that Rust now
/// has exclusive ownership of it; otherwise double-free or use-after-free can occur.
pub unsafe fn from_raw(raw: $t) -> Self {
Self(raw)
}
}
};
}
macro_rules! deref_impl_wrapper {
($name:ident$(<$lifetime:tt>)?, $t:ty, $dropfunc:expr, $rawfield:tt) => {
impl$(<$lifetime>)? std::convert::AsRef<$t> for $name$(<$lifetime>)? {
fn as_ref(&self) -> &$t {
&self.$rawfield
}
}
impl$(<$lifetime>)? std::convert::AsMut<$t> for $name$(<$lifetime>)? {
fn as_mut(&mut self) -> &mut $t {
&mut self.$rawfield
}
}
impl$(<$lifetime>)? std::ops::Deref for $name$(<$lifetime>)? {
type Target = $t;
#[inline]
fn deref(&self) -> &Self::Target {
&self.$rawfield
}
}
impl$(<$lifetime>)? std::ops::DerefMut for $name$(<$lifetime>)? {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.$rawfield
}
}
};
}
// Why `ManuallyDrop<Box<[T]>>` instead of copying into a `Vec`:
// raylib returns buffers allocated by its own allocator. Earlier versions of
// these bindings copied into a `Vec` and then freed the raw buffer with
// `libc::free`, but that breaks if the user has a custom allocator or links
// libc differently — the free goes to the wrong allocator. Wrapping as
// `ManuallyDrop<Box<[T]>>` lets us hand callers a slice with the usual
// iterator/indexing ergonomics without an extra allocation, and on drop we
// use `Box::leak` + `ManuallyDrop::take` to recover the raw pointer and hand
// it to raylib's matching `UnloadX` (or `MemFree`) so it's freed with the
// same allocator that allocated it.
macro_rules! make_rslice {
($name:ident, $t:ty, $dropfunc:expr) => {
#[repr(transparent)]
#[derive(Debug)]
pub struct $name(pub(crate) std::mem::ManuallyDrop<std::boxed::Box<[$t]>>);
impl_rslice!($name, std::boxed::Box<[$t]>, $dropfunc, 0);
};
}
macro_rules! impl_rslice {
($name:ident, $t:ty, $dropfunc:expr, $rawfield:tt) => {
impl Drop for $name {
#[allow(unused_unsafe)]
fn drop(&mut self) {
unsafe {
let inner = std::mem::ManuallyDrop::take(&mut self.0);
($dropfunc)(std::boxed::Box::leak(inner).as_mut_ptr() as *mut _);
}
}
}
impl std::convert::AsRef<$t> for $name {
fn as_ref(&self) -> &$t {
&self.$rawfield
}
}
impl std::convert::AsMut<$t> for $name {
fn as_mut(&mut self) -> &mut $t {
&mut self.$rawfield
}
}
impl std::ops::Deref for $name {
type Target = $t;
#[inline]
fn deref(&self) -> &Self::Target {
&self.$rawfield
}
}
impl std::ops::DerefMut for $name {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.$rawfield
}
}
};
}