fuchsia_inspect/writer/types/
value_list.rs1use crate::writer::types::InspectType;
6use derivative::Derivative;
7use fuchsia_sync::Mutex;
8
9type InspectTypeList = Vec<Box<dyn InspectType>>;
10
11#[derive(Derivative)]
13#[derivative(Debug, PartialEq, Eq)]
14pub struct ValueList {
15 #[derivative(PartialEq = "ignore")]
16 #[derivative(Debug = "ignore")]
17 values: Mutex<Option<InspectTypeList>>,
18}
19
20impl Default for ValueList {
21 fn default() -> Self {
22 ValueList::new()
23 }
24}
25
26impl ValueList {
27 pub fn new() -> Self {
29 Self { values: Mutex::new(None) }
30 }
31
32 pub fn record(&self, value: impl InspectType + 'static) {
34 let boxed_value = Box::new(value);
35 let mut values_lock = self.values.lock();
36 if let Some(ref mut values) = *values_lock {
37 values.push(boxed_value);
38 } else {
39 *values_lock = Some(vec![boxed_value]);
40 }
41 }
42
43 pub fn clear(&self) {
46 let mut values_lock = self.values.lock();
47 *values_lock = None;
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54 use crate::writer::types::Inspector;
55 use diagnostics_assertions::assert_json_diff;
56
57 #[fuchsia::test]
58 fn value_list_record() {
59 let inspector = Inspector::default();
60 let child = inspector.root().create_child("test");
61 let value_list = ValueList::new();
62 assert!(value_list.values.lock().is_none());
63 value_list.record(child);
64 assert_eq!(value_list.values.lock().as_ref().unwrap().len(), 1);
65 }
66
67 #[fuchsia::test]
68 fn value_list_drop_recorded() {
69 let inspector = Inspector::default();
70 let child = inspector.root().create_child("test");
71 let value_list = ValueList::new();
72 assert!(value_list.values.lock().is_none());
73 value_list.record(child);
74 assert_eq!(value_list.values.lock().as_ref().unwrap().len(), 1);
75 assert_json_diff!(inspector, root: {
76 test: {},
77 });
78
79 value_list.clear();
80 assert!(value_list.values.lock().is_none());
81 assert_json_diff!(inspector, root: {});
82 }
83}