1use std::collections::VecDeque;
22
23pub use heap_drop_macros::HeapDrop;
24
25pub trait HeapDrop {
29 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>>;
31}
32
33impl<T: HeapDrop + 'static> HeapDrop for Option<T> {
36 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
37 match *self {
38 Some(value) => vec![Box::new(value) as Box<dyn HeapDrop>],
39 None => vec![],
40 }
41 }
42}
43
44impl<T: HeapDrop + 'static> HeapDrop for Box<T> {
45 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
46 vec![*self as Box<dyn HeapDrop>]
47 }
48}
49
50impl<T: HeapDrop + 'static> HeapDrop for Vec<T> {
51 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
52 self.into_iter().map(|item| Box::new(item) as Box<dyn HeapDrop>).collect()
53 }
54}
55
56impl<T: HeapDrop + 'static> HeapDrop for VecDeque<T> {
57 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
58 self.into_iter().map(|item| Box::new(item) as Box<dyn HeapDrop>).collect()
59 }
60}
61
62impl<T: HeapDrop + 'static> HeapDrop for std::collections::LinkedList<T> {
63 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
64 self.into_iter().map(|item| Box::new(item) as Box<dyn HeapDrop>).collect()
65 }
66}
67
68impl<T: HeapDrop + 'static> HeapDrop for std::collections::HashSet<T> {
69 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
70 self.into_iter().map(|item| Box::new(item) as Box<dyn HeapDrop>).collect()
71 }
72}
73
74impl<K, V: HeapDrop + 'static> HeapDrop for std::collections::HashMap<K, V> {
75 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
76 self.into_values().map(|value| Box::new(value) as Box<dyn HeapDrop>).collect()
77 }
78}
79
80impl<T: HeapDrop + 'static> HeapDrop for std::cell::Cell<T> {
81 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
82 vec![Box::new(self.into_inner()) as Box<dyn HeapDrop>]
83 }
84}
85
86impl<T: HeapDrop + 'static> HeapDrop for core::cell::RefCell<T> {
87 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
88 vec![Box::new(self.into_inner()) as Box<dyn HeapDrop>]
89 }
90}
91
92impl<T: HeapDrop + 'static> HeapDrop for std::rc::Rc<T> {
93 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
94 vec![Box::new((*self).clone()) as Box<dyn HeapDrop>]
95 }
96}
97
98impl<T: HeapDrop + 'static> HeapDrop for std::sync::Arc<T> {
99 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
100 vec![Box::new((*self).clone()) as Box<dyn HeapDrop>]
101 }
102}
103
104impl<T: HeapDrop + 'static> HeapDrop for std::sync::Mutex<T> {
105 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
106 vec![Box::new(self.into_inner().unwrap()) as Box<dyn HeapDrop>]
107 }
108}
109
110impl<T: HeapDrop + 'static> HeapDrop for std::sync::RwLock<T> {
111 fn into_children(self: Box<Self>) -> Vec<Box<dyn HeapDrop>> {
112 vec![Box::new(self.into_inner().unwrap()) as Box<dyn HeapDrop>]
113 }
114}
115
116#[doc(hidden)]
119pub struct HeapDropDetect<T>(pub Box<T>);
120
121#[doc(hidden)]
122pub trait HeapDropDetectFallback {
123 fn detect(self) -> Option<Box<dyn HeapDrop>>;
124}
125
126impl<T> HeapDropDetectFallback for HeapDropDetect<T> {
127 fn detect(self) -> Option<Box<dyn HeapDrop>> {
128 None
129 }
130}
131
132impl<T: HeapDrop + 'static> HeapDropDetect<T> {
133 pub fn detect(self) -> Option<Box<dyn HeapDrop>> {
134 Some(self.0 as Box<dyn HeapDrop>)
135 }
136}
137
138#[doc(hidden)]
143#[macro_export]
144macro_rules! maybe_as_heap_drop {
145 ($val:expr) => {{
146 #[allow(unused_imports)]
147 use $crate::HeapDropDetectFallback;
148 $crate::HeapDropDetect(Box::new($val)).detect()
149 }};
150}
151
152pub fn heap_drop<T: HeapDrop>(value: T) {
174 let mut queue = VecDeque::new();
175
176 queue.push_back(Box::new(value) as Box<dyn HeapDrop>);
178 while let Some(item) = queue.pop_front() {
179 let children = item.into_children();
180 queue.extend(children);
181 }
182}