fuchsia_inspect/writer/types/
uint_property.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::{Inner, InnerValueType, InspectType, NumericProperty, Property};
6use log::error;
7
8/// Inspect uint property 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 UintProperty {
16    inner: Inner<InnerValueType>,
17}
18
19impl InspectType for UintProperty {}
20
21crate::impl_inspect_type_internal!(UintProperty);
22
23impl Property<'_> for UintProperty {
24    type Type = u64;
25
26    fn set(&self, value: u64) {
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_uint_metric(inner_ref.block_index, value);
31                }
32                Err(err) => error!(err:?; "Failed to set property"),
33            }
34        }
35    }
36}
37
38impl NumericProperty<'_> for UintProperty {
39    fn add(&self, value: u64) -> Option<u64> {
40        if let Some(ref inner_ref) = self.inner.inner_ref() {
41            match inner_ref.state.try_lock() {
42                Ok(mut state) => {
43                    return Some(state.add_uint_metric(inner_ref.block_index, value));
44                }
45                Err(err) => error!(err:?; "Failed to set property"),
46            }
47        }
48        None
49    }
50
51    fn subtract(&self, value: u64) -> Option<u64> {
52        if let Some(ref inner_ref) = self.inner.inner_ref() {
53            match inner_ref.state.try_lock() {
54                Ok(mut state) => {
55                    return Some(state.subtract_uint_metric(inner_ref.block_index, value));
56                }
57                Err(err) => error!(err:?; "Failed to set property"),
58            }
59        }
60        None
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    use crate::writer::testing_utils::{get_state, GetBlockExt};
68    use crate::writer::Node;
69    use inspect_format::{BlockType, Uint};
70
71    #[fuchsia::test]
72    fn uint_property() {
73        // Create and use a default value.
74        let default = UintProperty::default();
75        default.add(1);
76
77        let state = get_state(4096);
78        let root = Node::new_root(state);
79        let node = root.create_child("node");
80        {
81            let property = node.create_uint("property", 1);
82            property.get_block::<_, Uint>(|block| {
83                assert_eq!(block.block_type(), Some(BlockType::UintValue));
84                assert_eq!(block.value(), 1);
85            });
86            node.get_block::<_, inspect_format::Node>(|block| {
87                assert_eq!(block.child_count(), 1);
88            });
89
90            property.set(5);
91            property.get_block::<_, Uint>(|block| {
92                assert_eq!(block.value(), 5);
93            });
94
95            assert_eq!(property.subtract(3).unwrap(), 2);
96            property.get_block::<_, Uint>(|block| {
97                assert_eq!(block.value(), 2);
98            });
99
100            assert_eq!(property.add(8).unwrap(), 10);
101            property.get_block::<_, Uint>(|block| {
102                assert_eq!(block.value(), 10);
103            });
104        }
105        node.get_block::<_, inspect_format::Node>(|block| {
106            assert_eq!(block.child_count(), 0);
107        });
108    }
109}