Debian Codegraph @

third_party/rust/derive_more/tests/error/nightly/mod.rs

raw
1 use std::backtrace::Backtrace;
2
3 use super::*;
4
5 /// Asserts that backtrace returned by `Error::backtrace` method equals/not-equals
6 /// backtrace stored in object itself.
7 ///
8 /// Comparison is done by converting backtraces to strings
9 /// and then comparing these strings.
10 ///
11 /// ## Syntax
12 ///
13 /// * Equals: `assert_bt!(==, ...)`
14 /// * Not-equals: `assert_bt!(!=, ...)`
15 ///
16 /// ### Backtrace Access
17 ///
18 /// Shortcut for named-structs with `backtrace` field.
19 /// Access backtrace as `error.backtrace`.
20 ///
21 /// ```
22 /// assert_bt!(==, error);
23 /// ```
24 ///
25 /// Full form for named- and tuple-structs.
26 /// Access backtrace as `error.some_other_field` and `error.1` respectively.
27 ///
28 /// ```
29 /// assert_bt!(!=, error, some_other_field);
30 /// assert_bt!(==, error, 1);
31 /// ```
32 ///
33 /// Access as a method call.
34 /// Useful for enums (i.e., you can define a method that will match on enum variants
35 /// and return backtrace for each variant).
36 /// Access backtrace as `error.get_stored_backtrace_method()`.
37 ///
38 /// ```
39 /// assert_bt!(!=, error, .get_stored_backtrace_method);
40 /// ```
41 macro_rules! assert_bt {
42 (@impl $macro:ident, $error:expr, $backtrace:expr) => {
43 $macro!(std::any::request_ref::<Backtrace>(&$error).unwrap().to_string(), $backtrace.to_string());
44 };
45 (@expand $macro:ident, $error:expr, .$backtrace:ident) => {
46 assert_bt!(@impl $macro, $error, $error.$backtrace())
47 };
48 (@expand $macro:ident, $error:expr, .$backtrace:tt) => {
49 assert_bt!(@impl $macro, $error, $error.$backtrace)
50 };
51 (@expand $macro:ident, $error:expr, $backtrace:ident) => {
52 assert_bt!(@impl $macro, $error, $error.$backtrace)
53 };
54 (@expand $macro:ident, $error:expr, $backtrace:expr) => {
55 assert_bt!(@impl $macro, $error, $backtrace)
56 };
57 (@expand $macro:ident, $error:expr) => {
58 assert_bt!(@expand $macro, $error, backtrace)
59 };
60 (==, $($args:tt)*) => {
61 assert_bt!(@expand assert_eq, $($args)*)
62 };
63 (!=, $($args:tt)*) => {
64 assert_bt!(@expand assert_ne, $($args)*)
65 };
66 }
67
68 mod derives_for_enums_with_backtrace;
69 mod derives_for_generic_enums_with_backtrace;
70 mod derives_for_generic_structs_with_backtrace;
71 mod derives_for_structs_with_backtrace;
72
73 derive_display!(BacktraceErr);
74 #[derive(Debug)]
75 struct BacktraceErr {
76 backtrace: Backtrace,
77 }
78
79 impl Default for BacktraceErr {
80 fn default() -> Self {
81 Self {
82 backtrace: Backtrace::force_capture(),
83 }
84 }
85 }
86
87 impl Error for BacktraceErr {
88 fn provide<'a>(&'a self, demand: &mut std::any::Demand<'a>) {
89 demand
90 .provide_ref::<Backtrace>(&self.backtrace)
91 .provide_value::<i32>(42);
92 }
93 }