fuchsia_inspect/writer/types/
string_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 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::private::InspectTypeInternal;
use crate::writer::{ArrayProperty, Inner, InnerValueType, InspectType, State, StringReference};
use inspect_format::BlockIndex;

#[derive(Debug, PartialEq, Eq, Default)]
pub struct StringArrayProperty {
    inner: Inner<InnerValueType>,
}

impl InspectType for StringArrayProperty {}

impl InspectTypeInternal for StringArrayProperty {
    fn new(state: State, block_index: BlockIndex) -> Self {
        Self { inner: Inner::new(state, block_index) }
    }

    fn is_valid(&self) -> bool {
        self.inner.is_valid()
    }

    fn new_no_op() -> Self {
        Self { inner: Inner::None }
    }

    fn state(&self) -> Option<State> {
        Some(self.inner.inner_ref()?.state.clone())
    }

    fn block_index(&self) -> Option<BlockIndex> {
        Some(self.inner.inner_ref()?.block_index)
    }

    fn atomic_access<R, F: FnOnce(&Self) -> R>(&self, f: F) -> R {
        match self.inner.inner_ref() {
            None => {
                // If the node was a no-op we still execute the `update_fn` even if all operations
                // inside it will be no-ops to return `R`.
                f(self)
            }
            Some(inner_ref) => {
                // Silently ignore the error when fail to lock (as in any regular operation).
                // All operations performed in the `update_fn` won't update the vmo
                // generation count since we'll be holding one lock here.
                inner_ref.state.begin_transaction();
                let result = f(self);
                inner_ref.state.end_transaction();
                result
            }
        }
    }
}

impl ArrayProperty for StringArrayProperty {
    type Type = StringReference;

    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_string_slot(inner_ref.block_index, index, value.into())
                })
                .ok();
        }
    }

    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))
                .ok();
        }
    }
}

impl Drop for StringArrayProperty {
    fn drop(&mut self) {
        self.clear();
    }
}

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

    impl StringArrayProperty {
        pub fn load_string_slot(&self, slot: usize) -> Option<String> {
            self.inner.inner_ref().and_then(|inner_ref| {
                inner_ref
                    .state
                    .try_lock()
                    .and_then(|state| {
                        state.load_string(
                            state
                                .get_block(self.block_index().unwrap())
                                .array_get_string_index_slot(slot)?,
                        )
                    })
                    .ok()
            })
        }
    }

    #[fuchsia::test]
    fn string_array_property() {
        let inspector = Inspector::default();
        let root = inspector.root();
        let node = root.create_child("node");

        {
            let array = node.create_string_array("string_array", 5);
            assert_eq!(array.len().unwrap(), 5);
            node.get_block(|node_block| {
                assert_eq!(node_block.child_count().unwrap(), 1);
            });

            array.set(0, "0");
            array.set(1, "1");
            array.set(2, "2");
            array.set(3, "3");
            array.set(4, "4");

            // this should fail silently
            array.set(5, "5");
            assert!(array.load_string_slot(5).is_none());

            let expected: Vec<String> =
                vec!["0".into(), "1".into(), "2".into(), "3".into(), "4".into()];

            assert_json_diff!(inspector, root: {
                node: {
                    string_array: expected,
                },
            });

            array.clear();

            let expected: Vec<String> = vec![String::new(); 5];

            assert_json_diff!(inspector, root: {
                node: {
                    string_array: expected,
                },
            });

            assert!(array.load_string_slot(5).is_none());
        }

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

    #[fuchsia::test]
    fn property_atomics() {
        let inspector = Inspector::default();
        let array = inspector.root().create_string_array("string_array", 5);

        assert_update_is_atomic!(array, |array| {
            array.set(0, "0");
            array.set(1, "1");
            array.set(2, "2");
            array.set(3, "3");
            array.set(4, "4");
        });
    }
}