seize/reclaim.rs
1//! Common memory reclaimers.
2//!
3//! Functions in this module can be passed to
4//! [`retire`](crate::Collector::retire) to free allocated memory or run drop
5//! glue. See [the guide](crate#custom-reclaimers) for details about memory
6//! reclamation, and writing custom reclaimers.
7
8use std::ptr;
9
10use crate::{AsLink, Link};
11
12/// Reclaims memory allocated with [`Box`].
13///
14/// This function calls [`Box::from_raw`] on the linked pointer.
15///
16/// # Safety
17///
18/// Ensure that the correct type annotations are used when
19/// passing this function to [`retire`](crate::Collector::retire).
20/// The value retired must have been of type `T` to be retired through
21/// `boxed::<T>`.
22pub unsafe fn boxed<T: AsLink>(link: *mut Link) {
23 unsafe {
24 let _: Box<T> = Box::from_raw(Link::cast(link));
25 }
26}
27
28/// Reclaims memory by dropping the value in place.
29///
30/// This function calls [`ptr::drop_in_place`] on the linked pointer.
31///
32/// # Safety
33///
34/// Ensure that the correct type annotations are used when
35/// passing this function to [`retire`](crate::Collector::retire).
36/// The value retired must have been of type `T` to be retired through
37/// `in_place::<T>`.
38pub unsafe fn in_place<T: AsLink>(link: *mut Link) {
39 unsafe {
40 ptr::drop_in_place::<T>(Link::cast(link));
41 }
42}