assertr/assertions/std/
mem.rs1use crate::{AssertThat, Mode, Type, tracking::AssertionTracking};
2use core::fmt::Write;
3use indoc::writedoc;
4
5#[allow(clippy::return_self_not_must_use)]
7pub trait MemAssertions {
8 fn needs_drop(self) -> Self;
9
10 fn need_drop(self) -> Self
11 where
12 Self: Sized,
13 {
14 self.needs_drop()
15 }
16}
17
18impl<T, M: Mode> MemAssertions for AssertThat<'_, Type<T>, M> {
19 #[track_caller]
20 fn needs_drop(self) -> Self {
21 self.track_assertion();
22 let actual = self.actual();
23 if !actual.needs_drop() {
24 self.fail(|w: &mut String| {
25 writedoc! {w, r"
26 Type {actual:#?} was expected to need drop,
27
28 but dropping it is guaranteed to have no side effect.
29
30 You may forgot to `impl Drop` for this type.
31 ", actual = actual.get_type_name()}
32 });
33 }
34 self
35 }
36}
37
38#[cfg(test)]
39mod tests {
40 mod needs_drop {
41 use crate::assert_that_type;
42 use crate::prelude::*;
43 use indoc::formatdoc;
44
45 #[test]
46 fn succeeds_when_type_needs_drop() {
47 struct NeedsDrop;
48 impl Drop for NeedsDrop {
49 fn drop(&mut self) {
50 }
52 }
53
54 assert_that_type::<NeedsDrop>().needs_drop();
55 }
56
57 #[test]
58 fn panics_when_type_does_not_need_drop() {
59 struct DoeNotNeedDrop;
60
61 assert_that_panic_by(|| {
62 assert_that_type::<DoeNotNeedDrop>()
63 .with_location(false)
64 .needs_drop();
65 })
66 .has_type::<String>()
67 .is_equal_to(formatdoc! {r#"
68 -------- assertr --------
69 Type "assertr::assertions::std::mem::tests::needs_drop::panics_when_type_does_not_need_drop::DoeNotNeedDrop" was expected to need drop,
70
71 but dropping it is guaranteed to have no side effect.
72
73 You may forgot to `impl Drop` for this type.
74 -------- assertr --------
75 "#});
76 }
77 }
78}