fuchsia_inspect/writer/types/
double_array.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::writer::{ArithmeticArrayProperty, ArrayProperty, Inner, InnerValueType, InspectType};
use tracing::error;

/// Inspect double array data type.
///
/// NOTE: do not rely on PartialEq implementation for true comparison.
/// Instead leverage the reader.
///
/// NOTE: Operations on a Default value are no-ops.
#[derive(Debug, PartialEq, Eq, Default)]
pub struct DoubleArrayProperty {
    pub(crate) inner: Inner<InnerValueType>,
}

impl InspectType for DoubleArrayProperty {}

crate::impl_inspect_type_internal!(DoubleArrayProperty);

impl ArrayProperty for DoubleArrayProperty {
    type Type = f64;

    fn set(&self, index: usize, value: impl Into<Self::Type>) {
        if let Some(ref inner_ref) = self.inner.inner_ref() {
            inner_ref
                .state
                .try_lock()
                .and_then(|mut state| {
                    state.set_array_double_slot(inner_ref.block_index, index, value.into())
                })
                .unwrap_or_else(|err| {
                    error!(?err, "Failed to set property");
                });
        }
    }

    fn clear(&self) {
        if let Some(ref inner_ref) = self.inner.inner_ref() {
            inner_ref
                .state
                .try_lock()
                .and_then(|mut state| state.clear_array(inner_ref.block_index, 0))
                .unwrap_or_else(|err| {
                    error!(?err, "Failed to clear property.");
                });
        }
    }
}

impl ArithmeticArrayProperty for DoubleArrayProperty {
    fn add(&self, index: usize, value: f64) {
        if let Some(ref inner_ref) = self.inner.inner_ref() {
            inner_ref
                .state
                .try_lock()
                .and_then(|mut state| {
                    state.add_array_double_slot(inner_ref.block_index, index, value)
                })
                .unwrap_or_else(|err| {
                    error!(?err, "Failed to add property");
                });
        }
    }

    fn subtract(&self, index: usize, value: f64) {
        if let Some(ref inner_ref) = self.inner.inner_ref() {
            inner_ref
                .state
                .try_lock()
                .and_then(|mut state| {
                    state.subtract_array_double_slot(inner_ref.block_index, index, value)
                })
                .unwrap_or_else(|err| {
                    error!(?err, "Failed to subtract property");
                });
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::writer::testing_utils::GetBlockExt;
    use crate::writer::Inspector;
    use crate::Length;

    #[fuchsia::test]
    fn test_double_array() {
        // Create and use a default value.
        let default = DoubleArrayProperty::default();
        default.add(1, 1.0);

        let inspector = Inspector::default();
        let root = inspector.root();
        let node = root.create_child("node");
        {
            let array = node.create_double_array("array_property", 5);
            assert_eq!(array.len().unwrap(), 5);

            array.set(0, 5.0);
            array.get_block(|block| {
                assert_eq!(block.array_get_double_slot(0).unwrap(), 5.0);
            });

            array.add(0, 5.3);
            array.get_block(|array_block| {
                assert_eq!(array_block.array_get_double_slot(0).unwrap(), 10.3);
            });

            array.subtract(0, 3.4);
            array.get_block(|array_block| {
                assert_eq!(array_block.array_get_double_slot(0).unwrap(), 6.9);
            });

            array.set(1, 2.5);
            array.set(3, -3.1);

            array.get_block(|array_block| {
                for (i, value) in [6.9, 2.5, 0.0, -3.1, 0.0].iter().enumerate() {
                    assert_eq!(array_block.array_get_double_slot(i).unwrap(), *value);
                }
            });

            array.clear();
            array.get_block(|array_block| {
                for i in 0..5 {
                    assert_eq!(0.0, array_block.array_get_double_slot(i).unwrap());
                }
            });

            node.get_block(|block| {
                assert_eq!(block.child_count().unwrap(), 1);
            });
        }
        node.get_block(|block| {
            assert_eq!(block.child_count().unwrap(), 0);
        });
    }
}