fuchsia_inspect/writer/types/
double_array.rs1use crate::writer::{ArithmeticArrayProperty, ArrayProperty, Inner, InnerValueType, InspectType};
6use log::error;
7
8#[derive(Debug, PartialEq, Eq, Default)]
15pub struct DoubleArrayProperty {
16 pub(crate) inner: Inner<InnerValueType>,
17}
18
19impl InspectType for DoubleArrayProperty {}
20
21crate::impl_inspect_type_internal!(DoubleArrayProperty);
22
23impl ArrayProperty for DoubleArrayProperty {
24 type Type<'a> = f64;
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_double_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(|err| {
44 error!(err:?; "Failed to clear property.");
45 });
46 }
47 }
48}
49
50impl ArithmeticArrayProperty for DoubleArrayProperty {
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_double_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_double_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::Inspector;
85 use crate::Length;
86 use inspect_format::{Array, Double};
87
88 #[fuchsia::test]
89 fn test_double_array() {
90 let default = DoubleArrayProperty::default();
92 default.add(1, 1.0);
93
94 let inspector = Inspector::default();
95 let root = inspector.root();
96 let node = root.create_child("node");
97 {
98 let array = node.create_double_array("array_property", 5);
99 assert_eq!(array.len().unwrap(), 5);
100
101 array.set(0, 5.0);
102 array.get_block::<_, Array<Double>>(|block| {
103 assert_eq!(block.get(0).unwrap(), 5.0);
104 });
105
106 array.add(0, 5.3);
107 array.get_block::<_, Array<Double>>(|array_block| {
108 assert_eq!(array_block.get(0).unwrap(), 10.3);
109 });
110
111 array.subtract(0, 3.4);
112 array.get_block::<_, Array<Double>>(|array_block| {
113 assert_eq!(array_block.get(0).unwrap(), 6.9);
114 });
115
116 array.set(1, 2.5);
117 array.set(3, -3.1);
118
119 array.get_block::<_, Array<Double>>(|array_block| {
120 for (i, value) in [6.9, 2.5, 0.0, -3.1, 0.0].iter().enumerate() {
121 assert_eq!(array_block.get(i).unwrap(), *value);
122 }
123 });
124
125 array.clear();
126 array.get_block::<_, Array<Double>>(|array_block| {
127 for i in 0..5 {
128 assert_eq!(0.0, array_block.get(i).unwrap());
129 }
130 });
131
132 node.get_block::<_, inspect_format::Node>(|block| {
133 assert_eq!(block.child_count(), 1);
134 });
135 }
136 node.get_block::<_, inspect_format::Node>(|block| {
137 assert_eq!(block.child_count(), 0);
138 });
139 }
140}