Skip to main content

fuchsia_inspect/writer/types/
uint_array.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::writer::{ArithmeticArrayProperty, ArrayProperty, Inner, InnerValueType, InspectType};
6use log::error;
7
8/// Inspect uint array data type.
9///
10/// NOTE: do not rely on PartialEq implementation for true comparison.
11/// Instead leverage the reader.
12///
13/// NOTE: Operations on a Default value are no-ops.
14#[derive(Debug, PartialEq, Eq, Default)]
15pub struct UintArrayProperty {
16    pub(crate) inner: Inner<InnerValueType>,
17}
18
19impl InspectType for UintArrayProperty {
20    fn into_recorded(self) -> crate::writer::types::RecordedInspectType {
21        crate::writer::types::RecordedInspectType::UintArray(self)
22    }
23}
24
25crate::impl_inspect_type_internal!(UintArrayProperty);
26
27impl ArrayProperty for UintArrayProperty {
28    type Type<'a> = u64;
29
30    fn set<'a>(&self, index: usize, value: impl Into<Self::Type<'a>>) {
31        if let Some(ref inner_ref) = self.inner.inner_ref() {
32            match inner_ref.state.try_lock() {
33                Ok(mut state) => {
34                    state.set_array_uint_slot(inner_ref.block_index, index, value.into())
35                }
36                Err(err) => error!(err:?; "Failed to set property"),
37            }
38        }
39    }
40
41    fn clear(&self) {
42        if let Some(ref inner_ref) = self.inner.inner_ref() {
43            inner_ref
44                .state
45                .try_lock()
46                .and_then(|mut state| state.clear_array(inner_ref.block_index, 0))
47                .unwrap_or_else(|e| {
48                    error!("Failed to clear property. Error: {:?}", e);
49                });
50        }
51    }
52}
53
54impl ArithmeticArrayProperty for UintArrayProperty {
55    fn add<'a>(&self, index: usize, value: Self::Type<'a>)
56    where
57        Self: 'a,
58    {
59        if let Some(ref inner_ref) = self.inner.inner_ref() {
60            match inner_ref.state.try_lock() {
61                Ok(mut state) => {
62                    state.add_array_uint_slot(inner_ref.block_index, index, value);
63                }
64                Err(err) => error!(err:?; "Failed to add property"),
65            }
66        }
67    }
68
69    fn subtract<'a>(&self, index: usize, value: Self::Type<'a>)
70    where
71        Self: 'a,
72    {
73        if let Some(ref inner_ref) = self.inner.inner_ref() {
74            match inner_ref.state.try_lock() {
75                Ok(mut state) => {
76                    state.subtract_array_uint_slot(inner_ref.block_index, index, value);
77                }
78                Err(err) => error!(err:?; "Failed to subtract property"),
79            }
80        }
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87    use crate::writer::testing_utils::GetBlockExt;
88    use crate::writer::{Inspector, Length};
89    use inspect_format::{Array, Uint};
90
91    #[fuchsia::test]
92    fn test_uint_array() {
93        // Create and use a default value.
94        let default = UintArrayProperty::default();
95        default.add(1, 1);
96
97        let inspector = Inspector::default();
98        let root = inspector.root();
99        let node = root.create_child("node");
100        {
101            let array = node.create_uint_array("array_property", 5);
102            assert_eq!(array.len().unwrap(), 5);
103
104            array.set(0, 5u64);
105            array.get_block::<_, Array<Uint>>(|array_block| {
106                assert_eq!(array_block.get(0).unwrap(), 5);
107            });
108
109            array.add(0, 5);
110            array.get_block::<_, Array<Uint>>(|array_block| {
111                assert_eq!(array_block.get(0).unwrap(), 10);
112            });
113
114            array.subtract(0, 3);
115            array.get_block::<_, Array<Uint>>(|array_block| {
116                assert_eq!(array_block.get(0).unwrap(), 7);
117            });
118
119            array.set(1, 2u64);
120            array.set(3, 3u64);
121
122            array.get_block::<_, Array<Uint>>(|array_block| {
123                for (i, value) in [7, 2, 0, 3, 0].iter().enumerate() {
124                    assert_eq!(array_block.get(i).unwrap(), *value);
125                }
126            });
127
128            array.clear();
129            array.get_block::<_, Array<Uint>>(|array_block| {
130                for i in 0..5 {
131                    assert_eq!(0, array_block.get(i).unwrap());
132                }
133            });
134
135            node.get_block::<_, inspect_format::Node>(|node_block| {
136                assert_eq!(node_block.child_count(), 1);
137            });
138        }
139        node.get_block::<_, inspect_format::Node>(|node_block| {
140            assert_eq!(node_block.child_count(), 0);
141        });
142    }
143}