use std::collections::VecDeque;
pub use heap_drop_macros::HeapDrop;
pub trait HeapDrop {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>>;
}
impl<T: HeapDrop + 'static> HeapDrop for Option<T> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
match *self {
Some(value) => vec![Box::new(value) as Box<dyn HeapDrop>],
None => vec![],
}
}
}
impl<T: HeapDrop + 'static> HeapDrop for Box<T> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
vec![*self as Box<dyn HeapDrop>]
}
}
impl<T: HeapDrop + 'static> HeapDrop for Vec<T> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
self.into_iter().map(|item| Box::new(item) as Box<dyn HeapDrop>).collect()
}
}
impl<T: HeapDrop + 'static> HeapDrop for VecDeque<T> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
self.into_iter().map(|item| Box::new(item) as Box<dyn HeapDrop>).collect()
}
}
impl<T: HeapDrop + 'static> HeapDrop for std::collections::LinkedList<T> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
self.into_iter().map(|item| Box::new(item) as Box<dyn HeapDrop>).collect()
}
}
impl<T: HeapDrop + 'static> HeapDrop for std::collections::HashSet<T> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
self.into_iter().map(|item| Box::new(item) as Box<dyn HeapDrop>).collect()
}
}
impl<K, V: HeapDrop + 'static> HeapDrop for std::collections::HashMap<K, V> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
self.into_values().map(|value| Box::new(value) as Box<dyn HeapDrop>).collect()
}
}
impl<T: HeapDrop + 'static> HeapDrop for std::cell::Cell<T> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
vec![Box::new(self.into_inner()) as Box<dyn HeapDrop>]
}
}
impl<T: HeapDrop + 'static> HeapDrop for core::cell::RefCell<T> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
vec![Box::new(self.into_inner()) as Box<dyn HeapDrop>]
}
}
impl<T: HeapDrop + 'static> HeapDrop for std::rc::Rc<T> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
vec![Box::new((*self).clone()) as Box<dyn HeapDrop>]
}
}
impl<T: HeapDrop + 'static> HeapDrop for std::sync::Arc<T> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
vec![Box::new((*self).clone()) as Box<dyn HeapDrop>]
}
}
impl<T: HeapDrop + 'static> HeapDrop for std::sync::Mutex<T> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
vec![Box::new(self.into_inner().unwrap()) as Box<dyn HeapDrop>]
}
}
impl<T: HeapDrop + 'static> HeapDrop for std::sync::RwLock<T> {
fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
vec![Box::new(self.into_inner().unwrap()) as Box<dyn HeapDrop>]
}
}
#[doc(hidden)]
pub struct HeapDropDetect<T>(pub Box<T>);
#[doc(hidden)]
pub trait HeapDropDetectFallback {
fn detect(self) -> Option<Box<dyn HeapDrop>>;
}
impl<T> HeapDropDetectFallback for HeapDropDetect<T> {
fn detect(self) -> Option<Box<dyn HeapDrop>> {
None
}
}
impl<T: HeapDrop + 'static> HeapDropDetect<T> {
pub fn detect(self) -> Option<Box<dyn HeapDrop>> {
Some(self.0 as Box<dyn HeapDrop>)
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! maybe_as_heap_drop {
($val:expr) => {{
#[allow(unused_imports)]
use $crate::HeapDropDetectFallback;
$crate::HeapDropDetect(Box::new($val)).detect()
}};
}
pub fn heap_drop<T: HeapDrop>(value: T) {
let mut queue = VecDeque::new();
queue.push_back(Box::new(value) as Box<dyn HeapDrop>);
while let Some(item) = queue.pop_front() {
let children = item.into_children();
queue.extend(children);
}
}