Debian Codegraph @

tests/test_downcast.rs

raw symbols SCIP
1 #![allow(clippy::assertions_on_result_states, clippy::wildcard_imports)]
2
3 mod commonrust-analyzer cargo anyhow 1.0.103 common/;
4 mod droprust-analyzer cargo anyhow 1.0.103 drop/;
5
6 use selfrust-analyzer cargo anyhow 1.0.103 crate/::commonrust-analyzer cargo anyhow 1.0.103 common/::*;
7 use selfrust-analyzer cargo anyhow 1.0.103 crate/::droprust-analyzer cargo anyhow 1.0.103 drop/::{DetectDroprust-analyzer cargo anyhow 1.0.103 drop/DetectDrop#, Flagrust-analyzer cargo anyhow 1.0.103 drop/Flag#};
8 use anyhowrust-analyzer cargo anyhow 1.0.103 crate/::Errorrust-analyzer cargo anyhow 1.0.103 Error#;
9 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# as StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#;
10 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::{selfrust-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#};
11 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/;
12
13 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
14 fn test_downcastrust-analyzer cargo anyhow 1.0.103 test_downcast().() {
15 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
16 "oh no!",
17 bail_literalrust-analyzer cargo anyhow 1.0.103 common/bail_literal().().unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().downcastrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast().::<&str>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
18 );
19 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
20 "oh no!",
21 bail_fmtrust-analyzer cargo anyhow 1.0.103 common/bail_fmt().().unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().downcastrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
22 );
23 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
24 "oh no!",
25 bail_errorrust-analyzer cargo anyhow 1.0.103 common/bail_error().()
26 .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().()
27 .downcastrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast().::<iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#>()
28 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
29 .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
30 );
31 }
32
33 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
34 fn test_downcast_refrust-analyzer cargo anyhow 1.0.103 test_downcast_ref().() {
35 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
36 "oh no!",
37 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().bail_literalrust-analyzer cargo anyhow 1.0.103 common/bail_literal().().unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().downcast_refrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast_ref().::<&str>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
38 );
39 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
40 "oh no!",
41 bail_fmtrust-analyzer cargo anyhow 1.0.103 common/bail_fmt().().unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().downcast_refrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast_ref().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
42 );
43 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
44 "oh no!",
45 bail_errorrust-analyzer cargo anyhow 1.0.103 common/bail_error().()
46 .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().()
47 .downcast_refrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast_ref().::<iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#>()
48 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()
49 .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
50 );
51 }
52
53 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
54 fn test_downcast_mutrust-analyzer cargo anyhow 1.0.103 test_downcast_mut().() {
55 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
56 "oh no!",
57 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().bail_literalrust-analyzer cargo anyhow 1.0.103 common/bail_literal().().unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().downcast_mutrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast_mut().::<&str>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
58 );
59 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
60 "oh no!",
61 bail_fmtrust-analyzer cargo anyhow 1.0.103 common/bail_fmt().().unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().().downcast_mutrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast_mut().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
62 );
63 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
64 "oh no!",
65 bail_errorrust-analyzer cargo anyhow 1.0.103 common/bail_error().()
66 .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().()
67 .downcast_mutrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast_mut().::<iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#>()
68 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()
69 .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
70 );
71
72 let mut bailed = bail_fmtrust-analyzer cargo anyhow 1.0.103 common/bail_fmt().().unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
73 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().bailed.downcast_mutrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast_mut().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() = "clobber".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
74 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bailed.downcast_refrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast_ref().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), "clobber");
75 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bailed.downcast_mutrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast_mut().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), "clobber");
76 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bailed.downcastrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), "clobber");
77 }
78
79 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
80 fn test_droprust-analyzer cargo anyhow 1.0.103 test_drop().() {
81 let has_dropped = Flagrust-analyzer cargo anyhow 1.0.103 drop/Flag#::newrust-analyzer cargo anyhow 1.0.103 drop/impl#[Flag]new().();
82 let error = Errorrust-analyzer cargo anyhow 1.0.103 Error#::newrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]new().(DetectDroprust-analyzer cargo anyhow 1.0.103 drop/DetectDrop#::newrust-analyzer cargo anyhow 1.0.103 drop/impl#[DetectDrop]new().(&has_dropped));
83 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(error.downcastrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast().::<DetectDroprust-analyzer cargo anyhow 1.0.103 drop/DetectDrop#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
84 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(has_dropped.getrust-analyzer cargo anyhow 1.0.103 drop/impl#[Flag]get().());
85 }
86
87 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
88 fn test_as_refrust-analyzer cargo anyhow 1.0.103 test_as_ref().() {
89 let error = bail_errorrust-analyzer cargo anyhow 1.0.103 common/bail_error().().unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
90 let ref_dyn: &dyn StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# = error.as_refrust-analyzer cargo anyhow 1.0.103 error/impl#[Error][`AsRef<dyn Error + 'static>`]as_ref().();
91 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("oh no!", ref_dyn.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
92 let ref_dyn_send_sync: &(dyn StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync#) = error.as_refrust-analyzer cargo anyhow 1.0.103 error/impl#[Error][`AsRef<dyn Error + Send + Sync + 'static>`]as_ref().();
93 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("oh no!", ref_dyn_send_sync.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
94 }
95
96 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
97 fn test_large_alignmentrust-analyzer cargo anyhow 1.0.103 test_large_alignment().() {
98 #[repr(align(64))]
99 #[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:)]
100 struct LargeAlignedErrorrust-analyzer cargo anyhow 1.0.103 LargeAlignedError#(&'static str);
101
102 impl Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for LargeAlignedErrorrust-analyzer cargo anyhow 1.0.103 LargeAlignedError# {
103 fn fmtrust-analyzer cargo anyhow 1.0.103 impl#[LargeAlignedError][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
104 f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(self.0rust-analyzer cargo anyhow 1.0.103 LargeAlignedError#0.)
105 }
106 }
107
108 impl StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for LargeAlignedErrorrust-analyzer cargo anyhow 1.0.103 LargeAlignedError# {}
109
110 let error = Errorrust-analyzer cargo anyhow 1.0.103 Error#::newrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]new().(LargeAlignedErrorrust-analyzer cargo anyhow 1.0.103 LargeAlignedError#("oh no!"));
111 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
112 "oh no!",
113 error.downcast_refrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast_ref().::<LargeAlignedErrorrust-analyzer cargo anyhow 1.0.103 LargeAlignedError#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().0rust-analyzer cargo anyhow 1.0.103 LargeAlignedError#0.
114 );
115 }
116
117 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
118 fn test_unsuccessful_downcastrust-analyzer cargo anyhow 1.0.103 test_unsuccessful_downcast().() {
119 let mut error = bail_errorrust-analyzer cargo anyhow 1.0.103 common/bail_error().().unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
120 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(error.downcast_refrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast_ref().::<&str>().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().());
121 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(error.downcast_mutrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast_mut().::<&str>().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().());
122 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(error.downcastrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]downcast().::<&str>().is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
123 }