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
use ixy::index::Layout;
use crate::buf::GridBuf;
impl<T, B, L> GridBuf<T, B, L>
where
T: bytemuck::Pod,
B: AsRef<[T]>,
L: Layout,
{
pub fn as_bytes(&self) -> &[u8] {
bytemuck::cast_slice(self.buffer.as_ref())
}
}
#[cfg(test)]
mod tests {
use crate::{buf::VecGrid, core::Pos, ops::GridWrite};
use bytemuck::{Pod, Zeroable};
#[derive(Clone, Copy, Default)]
#[repr(C)]
struct Rgba {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
unsafe impl Pod for Rgba {}
unsafe impl Zeroable for Rgba {}
#[test]
fn rgba_as_bytes() {
let mut grid = VecGrid::<Rgba, _>::new_row_major(2, 2);
grid.set(
Pos::new(0, 0),
Rgba {
r: 255,
g: 0,
b: 0,
a: 255,
},
)
.unwrap();
grid.set(
Pos::new(1, 0),
Rgba {
r: 0,
g: 255,
b: 0,
a: 255,
},
)
.unwrap();
grid.set(
Pos::new(0, 1),
Rgba {
r: 0,
g: 0,
b: 255,
a: 255,
},
)
.unwrap();
grid.set(
Pos::new(1, 1),
Rgba {
r: 255,
g: 255,
b: 0,
a: 255,
},
)
.unwrap();
let bytes = grid.as_bytes();
#[rustfmt::skip]
assert_eq!(
bytes,
&[
255, 0, 0, 255, // Red
0, 255, 0, 255, // Green
0, 0, 255, 255, // Blue
255, 255, 0, 255 // Yellow
]
);
}
}