comet/internal/
finalize_trait.rs

1pub type FinalizationCallback = extern "C" fn(*mut u8);
2
3/// The FinalizeTrait specifies how to finalize objects.
4pub trait FinalizeTrait<T> {
5    /// If true finalizer is executed at the end of GC cycle for this type. 
6    /// In most cases compiler should be smart enough to determine if `NON_TRIVIAL_DTOR` is true
7    /// but in some rare unsafe cases you might set it to `false` by yourself.
8    const NON_TRIVIAL_DTOR: bool = std::mem::needs_drop::<T>();
9    /// Finalization callback executed by GC at the end of GC cycle. It defaults to [FinalizeTrait::finalize]. 
10    const CALLBACK: Option<FinalizationCallback> = if Self::NON_TRIVIAL_DTOR {
11        Some(Self::finalize)
12    } else {
13        None
14    };
15    /// Callback that is executed at the end of GC cycle. It invokes `T::drop` by default.
16    extern "C" fn finalize(obj: *mut u8) {
17        unsafe {
18            core::ptr::drop_in_place(obj.cast::<T>());
19        }
20    }
21}