fuchsia_inspect/writer/types/
int_property.rs1use crate::writer::{Inner, InnerValueType, InspectType, NumericProperty, Property};
6use log::error;
7
8#[derive(Debug, PartialEq, Eq, Default)]
15pub struct IntProperty {
16 inner: Inner<InnerValueType>,
17}
18
19impl InspectType for IntProperty {}
20
21crate::impl_inspect_type_internal!(IntProperty);
22
23impl Property<'_> for IntProperty {
24 type Type = i64;
25
26 fn set(&self, value: i64) {
27 if let Some(ref inner_ref) = self.inner.inner_ref() {
28 match inner_ref.state.try_lock() {
29 Ok(mut state) => state.set_int_metric(inner_ref.block_index, value),
30 Err(err) => error!(err:?; "Failed to set property"),
31 }
32 }
33 }
34}
35
36impl NumericProperty<'_> for IntProperty {
37 fn add(&self, value: i64) -> Option<i64> {
38 if let Some(ref inner_ref) = self.inner.inner_ref() {
39 match inner_ref.state.try_lock() {
40 Ok(mut state) => {
41 return Some(state.add_int_metric(inner_ref.block_index, value));
42 }
43 Err(err) => error!(err:?; "Failed to add property"),
44 }
45 }
46 None
47 }
48
49 fn subtract(&self, value: i64) -> Option<i64> {
50 if let Some(ref inner_ref) = self.inner.inner_ref() {
51 match inner_ref.state.try_lock() {
52 Ok(mut state) => {
53 return Some(state.subtract_int_metric(inner_ref.block_index, value));
54 }
55 Err(err) => error!(err:?; "Failed to subtract property"),
56 }
57 }
58 None
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65 use crate::assert_update_is_atomic;
66 use crate::writer::testing_utils::{get_state, GetBlockExt};
67 use crate::writer::{Inspector, Node};
68 use inspect_format::{BlockType, Int};
69
70 #[fuchsia::test]
71 fn int_property() {
72 let default = IntProperty::default();
74 default.add(1);
75
76 let state = get_state(4096);
77 let root = Node::new_root(state);
78 let node = root.create_child("node");
79 {
80 let property = node.create_int("property", 1);
81 property.get_block::<_, Int>(|property_block| {
82 assert_eq!(property_block.block_type(), Some(BlockType::IntValue));
83 assert_eq!(property_block.value(), 1);
84 });
85 node.get_block::<_, inspect_format::Node>(|node_block| {
86 assert_eq!(node_block.child_count(), 1);
87 });
88
89 property.set(2);
90 property.get_block::<_, Int>(|property_block| {
91 assert_eq!(property_block.value(), 2);
92 });
93
94 assert_eq!(property.subtract(5).unwrap(), -3);
95 property.get_block::<_, Int>(|property_block| {
96 assert_eq!(property_block.value(), -3);
97 });
98
99 assert_eq!(property.add(8).unwrap(), 5);
100 property.get_block::<_, Int>(|property_block| {
101 assert_eq!(property_block.value(), 5);
102 });
103 }
104 node.get_block::<_, inspect_format::Node>(|node_block| {
105 assert_eq!(node_block.child_count(), 0);
106 });
107 }
108
109 #[fuchsia::test]
110 fn property_atomics() {
111 let inspector = Inspector::default();
112 let property = inspector.root().create_int("property", 5);
113
114 assert_update_is_atomic!(property, |property| {
115 property.subtract(1);
116 property.subtract(2);
117 });
118 }
119}