fuchsia_inspect_contrib/log/
impls.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
// Copyright 2019 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 super::WriteInspect;
use fuchsia_inspect::{Node, StringReference};

// --- Utility macros to help with implementing WriteInspect ---

macro_rules! impl_write_inspect {
    ($inspect_value_type:ident, $_self:ident => $self_expr:expr, $($ty:ty),+) => {
        $(
            impl WriteInspect for $ty {
                fn write_inspect(&$_self, writer: &Node, key: impl Into<StringReference>) {
                    write_inspect_value!($inspect_value_type, writer, key, $self_expr);
                }
            }
        )+
    }
}

macro_rules! write_inspect_value {
    (Str, $node_writer:expr, $key:expr, $expr:expr) => {
        $node_writer.record_string($key, $expr);
    };
    (Uint, $node_writer:expr, $key:expr, $expr:expr) => {
        $node_writer.record_uint($key, $expr);
    };
    (Int, $node_writer:expr, $key:expr, $expr:expr) => {
        $node_writer.record_int($key, $expr);
    };
    (Double, $node_writer:expr, $key:expr, $expr:expr) => {
        $node_writer.record_double($key, $expr);
    };
    (Bool, $node_writer:expr, $key:expr, $expr:expr) => {
        $node_writer.record_bool($key, $expr);
    };
}

// --- Implementations for basic types ---

impl_write_inspect!(Str, self => &self, &str, String);
impl_write_inspect!(Uint, self => (*self).into(), u8, u16, u32);
impl_write_inspect!(Uint, self => *self, u64);
impl_write_inspect!(Int, self => (*self).into(), i8, i16, i32);
impl_write_inspect!(Int, self => *self, i64);
impl_write_inspect!(Double, self => (*self).into(), f32);
impl_write_inspect!(Double, self => *self, f64);
impl_write_inspect!(Bool, self => *self, bool);

impl<V: WriteInspect + ?Sized> WriteInspect for &V {
    fn write_inspect(&self, writer: &Node, key: impl Into<StringReference>) {
        (*self).write_inspect(writer, key)
    }
}