fuchsia_inspect/writer/types/
double_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 double property 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 DoubleProperty {
16    inner: Inner<InnerValueType>,
17}
18
19impl InspectType for DoubleProperty {}
20
21crate::impl_inspect_type_internal!(DoubleProperty);
22
23impl Property<'_> for DoubleProperty {
24    type Type = f64;
25
26    fn set(&self, value: f64) {
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_double_metric(inner_ref.block_index, value);
31                }
32                Err(err) => error!(err:?; "Failed to set property"),
33            }
34        }
35    }
36}
37
38impl NumericProperty<'_> for DoubleProperty {
39    fn add(&self, value: f64) -> Option<f64> {
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_double_metric(inner_ref.block_index, value));
44                }
45                Err(err) => error!(err:?; "Failed to add property"),
46            }
47        }
48        None
49    }
50
51    fn subtract(&self, value: f64) -> Option<f64> {
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_double_metric(inner_ref.block_index, value));
56                }
57                Err(err) => error!(err:?; "Failed to subtract 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, Double};
70
71    #[fuchsia::test]
72    fn double_property() {
73        // Create and use a default value.
74        let default = DoubleProperty::default();
75        default.add(1.0);
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_double("property", 1.0);
82            property.get_block::<_, Double>(|property_block| {
83                assert_eq!(property_block.block_type(), Some(BlockType::DoubleValue));
84                assert_eq!(property_block.value(), 1.0);
85            });
86            node.get_block::<_, inspect_format::Node>(|node_block| {
87                assert_eq!(node_block.child_count(), 1);
88            });
89
90            property.set(2.0);
91            property.get_block::<_, Double>(|property_block| {
92                assert_eq!(property_block.value(), 2.0);
93            });
94
95            assert_eq!(property.subtract(5.5).unwrap(), -3.5);
96            property.get_block::<_, Double>(|property_block| {
97                assert_eq!(property_block.value(), -3.5);
98            });
99
100            assert_eq!(property.add(8.1).unwrap(), 4.6);
101            property.get_block::<_, Double>(|property_block| {
102                assert_eq!(property_block.value(), 4.6);
103            });
104        }
105        node.get_block::<_, inspect_format::Node>(|node_block| {
106            assert_eq!(node_block.child_count(), 0);
107        });
108    }
109}