fuchsia_inspect/writer/types/
lazy_node.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::{Error, Inner, InnerType, InspectType, State};
6use inspect_format::BlockIndex;
7
8/// Inspect Lazy Node data type.
9/// NOTE: do not rely on PartialEq implementation for true comparison.
10/// Instead leverage the reader.
11///
12/// NOTE: Operations on a Default value are no-ops.
13#[derive(Debug, PartialEq, Eq, Default)]
14pub struct LazyNode {
15    inner: Inner<InnerLazyNodeType>,
16}
17
18impl InspectType for LazyNode {}
19
20crate::impl_inspect_type_internal!(LazyNode);
21
22#[derive(Default, Debug)]
23struct InnerLazyNodeType;
24
25impl InnerType for InnerLazyNodeType {
26    type Data = ();
27    fn free(state: &State, _: &Self::Data, block_index: BlockIndex) -> Result<(), Error> {
28        let mut state_lock = state.try_lock()?;
29        state_lock
30            .free_lazy_node(block_index)
31            .map_err(|err| Error::free("lazy node", block_index, err))
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use crate::writer::testing_utils::GetBlockExt;
38    use crate::writer::types::Inspector;
39    use futures::FutureExt;
40    use inspect_format::{BlockType, Link, LinkNodeDisposition};
41
42    #[fuchsia::test]
43    fn lazy_values() {
44        let inspector = Inspector::default();
45        let node = inspector.root().create_child("node");
46        {
47            let lazy_node =
48                node.create_lazy_values("lazy", || async move { Ok(Inspector::default()) }.boxed());
49            lazy_node.get_block::<_, Link>(|lazy_node_block| {
50                assert_eq!(lazy_node_block.block_type(), Some(BlockType::LinkValue));
51                assert_eq!(
52                    lazy_node_block.link_node_disposition(),
53                    Some(LinkNodeDisposition::Inline)
54                );
55                assert_eq!(*lazy_node_block.content_index(), 6);
56            });
57            node.get_block::<_, inspect_format::Node>(|node_block| {
58                assert_eq!(node_block.child_count(), 1);
59            });
60        }
61        node.get_block::<_, inspect_format::Node>(|node_block| {
62            assert_eq!(node_block.child_count(), 0);
63        });
64    }
65
66    #[fuchsia::test]
67    fn lazy_node() {
68        let inspector = Inspector::default();
69        let node = inspector.root().create_child("node");
70        {
71            let lazy_node =
72                node.create_lazy_child("lazy", || async move { Ok(Inspector::default()) }.boxed());
73            lazy_node.get_block::<_, Link>(|lazy_node_block| {
74                assert_eq!(lazy_node_block.block_type(), Some(BlockType::LinkValue));
75                assert_eq!(
76                    lazy_node_block.link_node_disposition(),
77                    Some(LinkNodeDisposition::Child)
78                );
79                assert_eq!(*lazy_node_block.content_index(), 6);
80            });
81            node.get_block::<_, inspect_format::Node>(|node_block| {
82                assert_eq!(node_block.child_count(), 1);
83            });
84        }
85        node.get_block::<_, inspect_format::Node>(|node_block| {
86            assert_eq!(node_block.child_count(), 0);
87        });
88    }
89}