fuchsia_inspect/writer/types/
int_array.rs1use crate::writer::{ArithmeticArrayProperty, ArrayProperty, Inner, InnerValueType, InspectType};
6use log::error;
7
8#[derive(Debug, PartialEq, Eq, Default)]
15pub struct IntArrayProperty {
16 pub(crate) inner: Inner<InnerValueType>,
17}
18
19impl InspectType for IntArrayProperty {}
20
21crate::impl_inspect_type_internal!(IntArrayProperty);
22
23impl ArrayProperty for IntArrayProperty {
24 type Type<'a> = i64;
25
26 fn set<'a>(&self, index: usize, value: impl Into<Self::Type<'a>>) {
27 if let Some(ref inner_ref) = self.inner.inner_ref() {
28 match inner_ref.state.try_lock() {
29 Ok(mut state) => {
30 state.set_array_int_slot(inner_ref.block_index, index, value.into())
31 }
32 Err(err) => error!(err:?; "Failed to set property"),
33 }
34 }
35 }
36
37 fn clear(&self) {
38 if let Some(ref inner_ref) = self.inner.inner_ref() {
39 inner_ref
40 .state
41 .try_lock()
42 .and_then(|mut state| state.clear_array(inner_ref.block_index, 0))
43 .unwrap_or_else(|e| {
44 error!("Failed to clear property. Error: {:?}", e);
45 });
46 }
47 }
48}
49
50impl ArithmeticArrayProperty for IntArrayProperty {
51 fn add<'a>(&self, index: usize, value: Self::Type<'a>)
52 where
53 Self: 'a,
54 {
55 if let Some(ref inner_ref) = self.inner.inner_ref() {
56 match inner_ref.state.try_lock() {
57 Ok(mut state) => {
58 state.add_array_int_slot(inner_ref.block_index, index, value);
59 }
60 Err(err) => error!(err:?; "Failed to add property"),
61 }
62 }
63 }
64
65 fn subtract<'a>(&self, index: usize, value: Self::Type<'a>)
66 where
67 Self: 'a,
68 {
69 if let Some(ref inner_ref) = self.inner.inner_ref() {
70 match inner_ref.state.try_lock() {
71 Ok(mut state) => {
72 state.subtract_array_int_slot(inner_ref.block_index, index, value);
73 }
74 Err(err) => error!(err:?; "Failed to subtract property"),
75 }
76 }
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83 use crate::writer::testing_utils::GetBlockExt;
84 use crate::writer::Length;
85 use crate::{assert_update_is_atomic, Inspector};
86 use inspect_format::{Array, Int};
87
88 #[fuchsia::test]
89 fn test_int_array() {
90 let default = IntArrayProperty::default();
92 default.add(1, 1);
93
94 let inspector = Inspector::default();
95 let root = inspector.root();
96 let node = root.create_child("node");
97 {
98 let array = node.create_int_array("array_property", 5);
99 assert_eq!(array.len().unwrap(), 5);
100
101 array.set(0, 5);
102 array.get_block::<_, Array<Int>>(|array_block| {
103 assert_eq!(array_block.get(0).unwrap(), 5);
104 });
105
106 array.add(0, 5);
107 array.get_block::<_, Array<Int>>(|array_block| {
108 assert_eq!(array_block.get(0).unwrap(), 10);
109 });
110
111 array.subtract(0, 3);
112
113 array.get_block::<_, Array<Int>>(|array_block| {
114 assert_eq!(array_block.get(0).unwrap(), 7);
115 });
116
117 array.set(1, 2);
118 array.set(3, -3);
119
120 array.get_block::<_, Array<Int>>(|array_block| {
121 for (i, value) in [7, 2, 0, -3, 0].iter().enumerate() {
122 assert_eq!(array_block.get(i).unwrap(), *value);
123 }
124 });
125
126 array.clear();
127 array.get_block::<_, Array<Int>>(|array_block| {
128 for i in 0..5 {
129 assert_eq!(0, array_block.get(i).unwrap());
130 }
131 });
132
133 node.get_block::<_, inspect_format::Node>(|node_block| {
134 assert_eq!(node_block.child_count(), 1);
135 });
136 }
137 node.get_block::<_, inspect_format::Node>(|node_block| {
138 assert_eq!(node_block.child_count(), 0);
139 });
140 }
141
142 #[fuchsia::test]
143 fn property_atomics() {
144 let inspector = Inspector::default();
145 let array = inspector.root().create_int_array("array", 5);
146
147 assert_update_is_atomic!(array, |array| {
148 array.set(0, 0);
149 array.set(1, 1);
150 array.set(2, 2);
151 array.set(3, 3);
152 array.set(4, 4);
153 });
154 }
155}