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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use std::fmt;
use std::marker::PhantomData;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::os::raw::*;
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::ptr::NonNull;
use std::slice;
use crate::error::WebPSimpleError;
pub struct WebpBox<T: ?Sized> {
ptr: NonNull<T>,
_marker: PhantomData<T>,
}
unsafe impl<T: ?Sized + Send> Send for WebpBox<T> {}
unsafe impl<T: ?Sized + Sync> Sync for WebpBox<T> {}
impl<T: ?Sized + UnwindSafe> UnwindSafe for WebpBox<T> {}
impl<T: ?Sized + RefUnwindSafe> RefUnwindSafe for WebpBox<T> {}
impl<T: ?Sized> WebpBox<T> {
pub unsafe fn from_raw(raw: *mut T) -> WebpBox<T> {
Self {
ptr: NonNull::new_unchecked(raw),
_marker: PhantomData,
}
}
pub fn into_raw(b: WebpBox<T>) -> *mut T {
let ptr = b.ptr;
mem::forget(b);
ptr.as_ptr()
}
}
impl<T: ?Sized> Deref for WebpBox<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { self.ptr.as_ref() }
}
}
impl<T: ?Sized> DerefMut for WebpBox<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.ptr.as_mut() }
}
}
impl<T: ?Sized> Drop for WebpBox<T> {
fn drop(&mut self) {
unsafe {
WebPFree(self.ptr.as_ptr() as *mut c_void);
}
}
}
#[cfg(feature = "0.5")]
use libwebp_sys::WebPFree;
#[cfg(not(feature = "0.5"))]
#[allow(non_snake_case)]
unsafe fn WebPFree(ptr: *mut c_void) {
extern "C" {
fn free(ptr: *mut c_void);
}
free(ptr);
}
impl<T: fmt::Debug + ?Sized> fmt::Debug for WebpBox<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self as &T, f)
}
}
#[inline]
pub(crate) unsafe fn wrap_bytes<F>(
ptr: *mut u8,
get_len: F,
) -> Result<WebpBox<[u8]>, WebPSimpleError>
where
F: FnOnce() -> usize,
{
if !ptr.is_null() {
let len = get_len();
Ok(WebpBox::from_raw(slice::from_raw_parts_mut(ptr, len)))
} else {
Err(WebPSimpleError)
}
}
pub struct WebpYuvBox {
y: NonNull<[u8]>,
u: NonNull<[u8]>,
v: NonNull<[u8]>,
}
unsafe impl Send for WebpYuvBox {}
unsafe impl Sync for WebpYuvBox {}
impl WebpYuvBox {
pub unsafe fn from_raw_yuv(y: *mut [u8], u: *mut [u8], v: *mut [u8]) -> WebpYuvBox {
Self {
y: NonNull::new_unchecked(y),
u: NonNull::new_unchecked(u),
v: NonNull::new_unchecked(v),
}
}
pub fn into_raw_yuv(self) -> (*mut [u8], *mut [u8], *mut [u8]) {
let y = self.y.as_ptr();
let u = self.u.as_ptr();
let v = self.v.as_ptr();
mem::forget(self);
(y, u, v)
}
pub fn y(&self) -> &[u8] {
unsafe { self.y.as_ref() }
}
pub fn y_mut(&mut self) -> &mut [u8] {
unsafe { self.y.as_mut() }
}
pub fn u(&self) -> &[u8] {
unsafe { self.u.as_ref() }
}
pub fn u_mut(&mut self) -> &mut [u8] {
unsafe { self.u.as_mut() }
}
pub fn v(&self) -> &[u8] {
unsafe { self.v.as_ref() }
}
pub fn v_mut(&mut self) -> &mut [u8] {
unsafe { self.v.as_mut() }
}
pub fn yuv(&self) -> (&[u8], &[u8], &[u8]) {
let y = unsafe { self.y.as_ref() };
let u = unsafe { self.u.as_ref() };
let v = unsafe { self.v.as_ref() };
(y, u, v)
}
pub fn yuv_mut(&mut self) -> (&mut [u8], &mut [u8], &mut [u8]) {
let y = unsafe { self.y.as_mut() };
let u = unsafe { self.u.as_mut() };
let v = unsafe { self.v.as_mut() };
(y, u, v)
}
pub fn into_y(self) -> WebpBox<[u8]> {
let y = self.y;
mem::forget(self);
WebpBox {
ptr: y,
_marker: PhantomData,
}
}
}
impl Drop for WebpYuvBox {
fn drop(&mut self) {
unsafe {
WebPFree(self.y.as_ptr() as *mut c_void);
}
}
}
impl fmt::Debug for WebpYuvBox {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("WebpYuvBox")
.field("y", &self.y())
.field("u", &self.u())
.field("v", &self.v())
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::panic::{RefUnwindSafe, UnwindSafe};
#[allow(unused)]
fn test_auto_traits() {
fn is_send<T: Send>() {}
fn is_sync<T: Sync>() {}
fn is_unwind_safe<T: UnwindSafe>() {}
fn is_ref_unwind_safe<T: RefUnwindSafe>() {}
is_send::<WebpBox<[u8]>>();
is_sync::<WebpBox<[u8]>>();
is_unwind_safe::<WebpBox<[u8]>>();
is_ref_unwind_safe::<WebpBox<[u8]>>();
is_send::<WebpYuvBox>();
is_sync::<WebpYuvBox>();
is_unwind_safe::<WebpYuvBox>();
is_ref_unwind_safe::<WebpYuvBox>();
}
}