forked from godot-rust/gdnative
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_array.rs
60 lines (50 loc) · 1.84 KB
/
color_array.rs
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
use crate::core_types::Color;
use crate::core_types::TypedArray;
/// A reference-counted vector of `Color` that uses Godot's pool allocator.
pub type ColorArray = TypedArray<Color>;
godot_test!(
test_color_array_access {
use crate::object::NewRef as _;
let arr = ColorArray::from_vec(vec![
Color::from_rgb(1.0, 0.0, 0.0),
Color::from_rgb(0.0, 1.0, 0.0),
Color::from_rgb(0.0, 0.0, 1.0),
]);
let original_read = {
let read = arr.read();
assert_eq!(&[
Color::from_rgb(1.0, 0.0, 0.0),
Color::from_rgb(0.0, 1.0, 0.0),
Color::from_rgb(0.0, 0.0, 1.0),
], read.as_slice());
read.clone()
};
let mut cow_arr = arr.new_ref();
{
let mut write = cow_arr.write();
assert_eq!(3, write.len());
for i in write.as_mut_slice() {
i.b = 1.0;
}
}
assert_eq!(Color::from_rgb(1.0, 0.0, 1.0), cow_arr.get(0));
assert_eq!(Color::from_rgb(0.0, 1.0, 1.0), cow_arr.get(1));
assert_eq!(Color::from_rgb(0.0, 0.0, 1.0), cow_arr.get(2));
// the write shouldn't have affected the original array
assert_eq!(&[
Color::from_rgb(1.0, 0.0, 0.0),
Color::from_rgb(0.0, 1.0, 0.0),
Color::from_rgb(0.0, 0.0, 1.0),
], original_read.as_slice());
}
);
godot_test!(
test_color_array_debug {
let arr = ColorArray::from_vec(vec![
Color::from_rgb(1.0, 0.0, 0.0),
Color::from_rgb(0.0, 1.0, 0.0),
Color::from_rgb(0.0, 0.0, 1.0),
]);
assert_eq!(format!("{:?}", arr), "[Color { r: 1.0, g: 0.0, b: 0.0, a: 1.0 }, Color { r: 0.0, g: 1.0, b: 0.0, a: 1.0 }, Color { r: 0.0, g: 0.0, b: 1.0, a: 1.0 }]");
}
);