/home/runner/work/bare_err_tree/bare_err_tree/bare_err_tree/src/pkg.rs
Line | Count | Source |
1 | | /* |
2 | | * This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
5 | | */ |
6 | | |
7 | | use core::{cmp::Ordering, fmt::Debug, hash::Hash}; |
8 | | |
9 | | #[cfg(feature = "source_line")] |
10 | | use core::panic::Location; |
11 | | |
12 | | #[cfg(feature = "tracing")] |
13 | | use tracing_error::SpanTrace; |
14 | | |
15 | | #[cfg(feature = "boxed")] |
16 | | use alloc::boxed::Box; |
17 | | |
18 | | /// Captures extra information for [`ErrTree`][`crate::ErrTree`] |
19 | | /// automatically. |
20 | | /// |
21 | | /// [`Self::new()`] must be called by a function annotated with |
22 | | /// `#[track_caller]` to capture the correct callsite. |
23 | | /// |
24 | | /// The inner fields are obscured to allow arbitrary metadata tracking |
25 | | /// combinations via feature flags without changing the API. The `boxed` |
26 | | /// feature can be enabled to store this in heap. |
27 | | /// |
28 | | /// All instances of this are considered equal, to avoid infecting sort order |
29 | | /// or comparisons between the parent error types. Hashing is a no-op. |
30 | | #[derive(Clone)] |
31 | | pub struct ErrTreePkg { |
32 | | #[cfg(not(feature = "boxed"))] |
33 | | #[allow(dead_code)] |
34 | | inner: InnerErrTreePkg, |
35 | | #[cfg(feature = "boxed")] |
36 | | #[allow(dead_code)] |
37 | | inner: Box<InnerErrTreePkg>, |
38 | | } |
39 | | |
40 | | #[derive(Clone)] |
41 | | pub struct InnerErrTreePkg { |
42 | | #[cfg(feature = "source_line")] |
43 | | location: &'static Location<'static>, |
44 | | #[cfg(feature = "tracing")] |
45 | | trace: SpanTrace, |
46 | | } |
47 | | |
48 | | impl ErrTreePkg { |
49 | | #[track_caller] |
50 | 3 | pub fn new() -> Self { |
51 | 3 | let inner = InnerErrTreePkg { |
52 | 3 | #[cfg(feature = "source_line")] |
53 | 3 | location: Location::caller(), |
54 | 3 | #[cfg(feature = "tracing")] |
55 | 3 | trace: SpanTrace::capture(), |
56 | 3 | }; |
57 | 3 | |
58 | 3 | #[cfg(feature = "boxed")] |
59 | 3 | let inner = Box::new(inner); |
60 | 3 | |
61 | 3 | Self { inner } |
62 | 3 | } |
63 | | |
64 | | #[cfg(feature = "source_line")] |
65 | | pub(crate) fn location(&self) -> &'static Location<'static> { |
66 | | self.inner.location |
67 | | } |
68 | | |
69 | | #[cfg(feature = "tracing")] |
70 | | pub(crate) fn trace(&self) -> &SpanTrace { |
71 | | &self.inner.trace |
72 | | } |
73 | | } |
74 | | |
75 | | impl Default for ErrTreePkg { |
76 | | #[cfg_attr(coverage, coverage(off))] |
77 | | #[track_caller] |
78 | | fn default() -> Self { |
79 | | Self::new() |
80 | | } |
81 | | } |
82 | | |
83 | | impl Debug for ErrTreePkg { |
84 | | #[cfg_attr(coverage, coverage(off))] |
85 | | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
86 | | write!(f, "...") |
87 | | } |
88 | | } |
89 | | |
90 | | impl PartialEq for ErrTreePkg { |
91 | | #[cfg_attr(coverage, coverage(off))] |
92 | | fn eq(&self, _other: &Self) -> bool { |
93 | | true |
94 | | } |
95 | | } |
96 | | |
97 | | impl Ord for ErrTreePkg { |
98 | | #[cfg_attr(coverage, coverage(off))] |
99 | | fn cmp(&self, _other: &Self) -> core::cmp::Ordering { |
100 | | Ordering::Equal |
101 | | } |
102 | | } |
103 | | |
104 | | impl Eq for ErrTreePkg {} |
105 | | |
106 | | impl PartialOrd for ErrTreePkg { |
107 | | #[cfg_attr(coverage, coverage(off))] |
108 | | fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { |
109 | | Some(self.cmp(other)) |
110 | | } |
111 | | } |
112 | | |
113 | | impl Hash for ErrTreePkg { |
114 | | #[cfg_attr(coverage, coverage(off))] |
115 | | fn hash<H: core::hash::Hasher>(&self, _state: &mut H) {} |
116 | | } |