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
pub unsafe trait RawPixelSized<T>: Sized {
const CHANNELS: usize;
}
unsafe impl<T> RawPixelSized<T> for [T; 1] {
const CHANNELS: usize = 1;
}
unsafe impl<T> RawPixelSized<T> for [T; 2] {
const CHANNELS: usize = 2;
}
unsafe impl<T> RawPixelSized<T> for [T; 3] {
const CHANNELS: usize = 3;
}
unsafe impl<T> RawPixelSized<T> for [T; 4] {
const CHANNELS: usize = 4;
}
pub unsafe trait RawPixel<T> {
fn channels(&self) -> usize;
unsafe fn from_raw_parts<'a>(pointer: *const T, length: usize) -> &'a Self;
unsafe fn from_raw_parts_mut<'a>(pointer: *mut T, length: usize) -> &'a mut Self;
fn as_ptr(&self) -> *const T;
fn as_mut_ptr(&mut self) -> *mut T;
}
unsafe impl<P: RawPixelSized<T>, T> RawPixel<T> for P {
#[inline]
fn channels(&self) -> usize {
P::CHANNELS
}
#[inline]
unsafe fn from_raw_parts<'a>(pointer: *const T, length: usize) -> &'a Self {
assert_eq!(length, Self::CHANNELS);
&*(pointer as *const Self)
}
#[inline]
unsafe fn from_raw_parts_mut<'a>(pointer: *mut T, length: usize) -> &'a mut Self {
assert_eq!(length, Self::CHANNELS);
&mut *(pointer as *mut Self)
}
#[inline]
fn as_ptr(&self) -> *const T {
self as *const Self as *const T
}
#[inline]
fn as_mut_ptr(&mut self) -> *mut T {
self as *mut Self as *mut T
}
}
unsafe impl<T> RawPixel<T> for [T] {
#[inline]
fn channels(&self) -> usize {
self.len()
}
#[inline]
unsafe fn from_raw_parts<'a>(pointer: *const T, length: usize) -> &'a Self {
::std::slice::from_raw_parts(pointer, length)
}
#[inline]
unsafe fn from_raw_parts_mut<'a>(pointer: *mut T, length: usize) -> &'a mut Self {
::std::slice::from_raw_parts_mut(pointer, length)
}
#[inline]
fn as_ptr(&self) -> *const T {
self.as_ptr()
}
#[inline]
fn as_mut_ptr(&mut self) -> *mut T {
self.as_mut_ptr()
}
}