assertr/assertions/std/
mem.rs

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