1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::{fmt, mem::ManuallyDrop, rc::Rc};

use crate::{
    export::{DynReporterPlugin, ReporterPluginFilter, UnsafeReporterPlugin},
    import::serde::PluginLibrary,
};

#[allow(clippy::module_name_repetitions)]
pub struct ReporterPlugin {
    pub(crate) library: Rc<PluginLibrary>,

    pub(crate) reporter: ManuallyDrop<Box<DynReporterPlugin>>,
    pub(crate) filter: ReporterPluginFilter,

    pub(crate) finalised: bool,
}

impl ReporterPlugin {
    pub(crate) fn finalise(mut self) {
        self.finalised = true;

        std::mem::drop(self);
    }
}

impl Drop for ReporterPlugin {
    fn drop(&mut self) {
        if self.finalised {
            unsafe {
                ManuallyDrop::take(&mut self.reporter).finalise_boxed();
            }
        } else {
            unsafe {
                (self.library.declaration.drop)(ManuallyDrop::new(UnsafeReporterPlugin {
                    reporter: ManuallyDrop::take(&mut self.reporter),
                    filter: self.filter,
                }));
            }
        }
    }
}

impl fmt::Debug for ReporterPlugin {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(&*self.reporter, fmt)
    }
}