Debian Codegraph @

src/result_ext.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#, fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/};
2
3 use craterust-analyzer cargo display-error-chain 0.2.2 crate/::DisplayErrorChainrust-analyzer cargo display-error-chain 0.2.2 DisplayErrorChain#;
4
5 /// A helper extension trait to "unwrap" results with an error chain.
6 pub trait ResultExtrust-analyzer cargo display-error-chain 0.2.2 result_ext/ResultExt#<T, E> {
7 /// Like [`Result::unwrap`][Result::unwrap], but wraps the error in the
8 /// [DisplayErrorChain] and prints out the full chain in case an `Err(_)`
9 /// value is encountered.
10 #[track_caller]
11 fn unwrap_chainrust-analyzer cargo display-error-chain 0.2.2 result_ext/ResultExt#unwrap_chain().(self) -> T;
12
13 /// Like [`Result::expect`][Result::expect], but wraps the error in the
14 /// [DisplayErrorChain] and prints out the full chain in case an `Err(_)`
15 /// value is encountered.
16 #[track_caller]
17 fn expect_chainrust-analyzer cargo display-error-chain 0.2.2 result_ext/ResultExt#expect_chain().(self, msg: &str) -> T;
18 }
19
20 impl<T, E: Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#> ResultExtrust-analyzer cargo display-error-chain 0.2.2 result_ext/ResultExt#<T, E> for Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, E> {
21 #[inline]
22 #[track_caller]
23 fn unwrap_chainrust-analyzer cargo display-error-chain 0.2.2 result_ext/impl#[`Result<T, E>`][`ResultExt<T, E>`]unwrap_chain().(self) -> T {
24 match self {
25 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(value) => value,
26 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => unwrap_failedrust-analyzer cargo display-error-chain 0.2.2 result_ext/unwrap_failed().(
27 "called `Result::unwrap_chain()` on an `Err` value",
28 &DisplayErrorChainrust-analyzer cargo display-error-chain 0.2.2 DisplayErrorChain#::newrust-analyzer cargo display-error-chain 0.2.2 impl#[`DisplayErrorChain<E>`]new().(&e),
29 ),
30 }
31 }
32 #[inline]
33 #[track_caller]
34 fn expect_chainrust-analyzer cargo display-error-chain 0.2.2 result_ext/impl#[`Result<T, E>`][`ResultExt<T, E>`]expect_chain().(self, msg: &str) -> T {
35 match self {
36 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(value) => value,
37 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => unwrap_failedrust-analyzer cargo display-error-chain 0.2.2 result_ext/unwrap_failed().(msg, &DisplayErrorChainrust-analyzer cargo display-error-chain 0.2.2 DisplayErrorChain#::newrust-analyzer cargo display-error-chain 0.2.2 impl#[`DisplayErrorChain<E>`]new().(&e)),
38 }
39 }
40 }
41
42 // This is a separate function to reduce the code size of the methods
43 #[inline(never)]
44 #[cold]
45 #[track_caller]
46 fn unwrap_failedrust-analyzer cargo display-error-chain 0.2.2 result_ext/unwrap_failed().(msg: &str, error: &dyn fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#) -> ! {
47 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("{}: {}", msg, error)
48 }
49
50 #[cfg(test)]
51 mod testrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/ {
52 use superrust-analyzer cargo display-error-chain 0.2.2 result_ext/::ResultExtrust-analyzer cargo display-error-chain 0.2.2 result_ext/ResultExt#;
53
54 macro_rules! impl_errorrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/impl_error! {
55 ($ty:ty, $display:expr, $source:expr) => {
56 impl ::std::fmt::Display for $ty {
57 fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
58 write!(f, "{}", $display)
59 }
60 }
61
62 impl ::std::error::Error for $ty {
63 fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
64 $source
65 }
66 }
67 };
68 }
69
70 // `TopLevel` is caused by a `MidLevel`.
71 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
72 struct TopLevelrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/TopLevel#;
73 impl_errorrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/impl_error!!(TopLevelrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/TopLevel#, "top level", Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&MidLevelrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/MidLevel#));
74
75 // `MidLevel` is caused by a `LowLevel`.
76 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
77 struct MidLevelrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/MidLevel#;
78 impl_errorrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/impl_error!!(MidLevelrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/MidLevel#, "mid level", Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&LowLevelrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/LowLevel#));
79
80 // `LowLevel` is the cause itself.
81 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
82 struct LowLevelrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/LowLevel#;
83 impl_errorrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/impl_error!!(LowLevelrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/LowLevel#, "low level", Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
84
85 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
86 #[should_panic(expected = "\
87 called `Result::unwrap_chain()` on an `Err` value: top level\n\
88 Caused by:
89 -> mid level
90 -> low level")]
91 fn test_unwraprust-analyzer cargo display-error-chain 0.2.2 result_ext/test/test_unwrap().() {
92 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#::<(), _>(TopLevelrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/TopLevel#).unwrap_chainrust-analyzer cargo display-error-chain 0.2.2 result_ext/impl#[`Result<T, E>`][`ResultExt<T, E>`]unwrap_chain().();
93 }
94
95 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
96 #[should_panic(expected = "\
97 Some message: top level\n\
98 Caused by:
99 -> mid level
100 -> low level")]
101 fn test_expectrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/test_expect().() {
102 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#::<(), _>(TopLevelrust-analyzer cargo display-error-chain 0.2.2 result_ext/test/TopLevel#).expect_chainrust-analyzer cargo display-error-chain 0.2.2 result_ext/impl#[`Result<T, E>`][`ResultExt<T, E>`]expect_chain().("Some message");
103 }
104 }