fuchsia_inspect_contrib/log/
impls.rs

1// Copyright 2019 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 super::WriteInspect;
6use fuchsia_inspect::Node;
7use std::borrow::Cow;
8
9// --- Utility macros to help with implementing WriteInspect ---
10
11macro_rules! impl_write_inspect {
12    ($inspect_value_type:ident, $_self:ident => $self_expr:expr, $($ty:ty),+) => {
13        $(
14            impl WriteInspect for $ty {
15                fn write_inspect<'a>(&$_self, writer: &Node, key: impl Into<Cow<'a, str>>) {
16                    write_inspect_value!($inspect_value_type, writer, key, $self_expr);
17                }
18            }
19        )+
20    }
21}
22
23macro_rules! write_inspect_value {
24    (Str, $node_writer:expr, $key:expr, $expr:expr) => {
25        $node_writer.record_string($key, $expr);
26    };
27    (StrRef, $node_writer:expr, $key:expr, $expr:expr) => {
28        $node_writer.record_string($key, $expr);
29    };
30    (Uint, $node_writer:expr, $key:expr, $expr:expr) => {
31        $node_writer.record_uint($key, $expr);
32    };
33    (Int, $node_writer:expr, $key:expr, $expr:expr) => {
34        $node_writer.record_int($key, $expr);
35    };
36    (Double, $node_writer:expr, $key:expr, $expr:expr) => {
37        $node_writer.record_double($key, $expr);
38    };
39    (Bool, $node_writer:expr, $key:expr, $expr:expr) => {
40        $node_writer.record_bool($key, $expr);
41    };
42}
43
44// --- Implementations for basic types ---
45
46impl_write_inspect!(Str, self => self, String);
47impl_write_inspect!(StrRef, self => *self, &str);
48impl_write_inspect!(Uint, self => (*self).into(), u8, u16, u32);
49impl_write_inspect!(Uint, self => *self, u64);
50impl_write_inspect!(Int, self => (*self).into(), i8, i16, i32);
51impl_write_inspect!(Int, self => *self, i64);
52impl_write_inspect!(Double, self => (*self).into(), f32);
53impl_write_inspect!(Double, self => *self, f64);
54impl_write_inspect!(Bool, self => *self, bool);
55
56impl<V: WriteInspect + ?Sized> WriteInspect for &V {
57    fn write_inspect<'a>(&self, writer: &Node, key: impl Into<Cow<'a, str>>) {
58        (*self).write_inspect(writer, key)
59    }
60}