Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 /*!
2 A utility type that allows you to defer dropping your data to a background
3 thread. See [`DeferDrop`] for details.
4
5 Inspired by [https://abramov.io/rust-dropping-things-in-another-thread](https://abramov.io/rust-dropping-things-in-another-thread)
6
7 # Features
8
9 - `serde`: when enabled, adds a [`Serialize`] and [`Deserialize`]
10 implementation to [`DeferDrop`]
11 */
12
13 use std::{
14 mem::{self, ManuallyDrop},
15 ops::{Deref, DerefMut},
16 thread::{self, JoinHandle},
17 };
18
19 use crossbeam_channel::{self as channel, Sender};
20 use once_cell::sync::OnceCell;
21
22 #[cfg(feature = "serde")]
23 use serde::{Deserialize, Serialize};
24
25 /// Wrapper type that, when dropped, sends the inner value to a global
26 /// background thread to be dropped. Useful in cases where a value takes a
27 /// long time to drop (for instance, a windows file that might block on close,
28 /// or a large data structure that has to extensively recursively trawl
29 /// itself).
30 ///
31 /// `DeferDrop` implements `Deref` and `DerefMut`, meaning it can be
32 /// dereferenced and freely used like a container around its inner type.
33 ///
34 /// # Notes:
35 ///
36 /// Carefully consider whether this pattern is necessary for your use case.
37 /// Like all worker-thread abstractions, sending the value to a separate
38 /// thread comes with its own costs, so it should only be done if performance
39 /// profiling indicates that it's a performance gain.
40 ///
41 /// There is only one global worker thread. Dropped values are enqueued in an
42 /// unbounded channel to be consumed by this thread; if you produce more
43 /// garbage than the thread can handle, this will cause unbounded memory
44 /// consumption. There is currently no way for the thread to signal or block
45 /// if it is overwhelmed.
46 ///
47 /// All of the standard non-determinism threading caveats apply here. The
48 /// objects are guaranteed to be destructed in the order received through a
49 /// channel, which means that objects sent from a single thread will be
50 /// destructed in order. However, there is no guarantee about the ordering of
51 /// interleaved values from different threads. Additionally, there are no
52 /// guarantees about how long the values will be queued before being dropped,
53 /// or even that they will be dropped at all. If your `main` thread terminates
54 /// before all drops could be completed, they will be silently lost (as though
55 /// via a [`mem::forget`]). This behavior is entirely up to your OS's thread
56 /// scheduler. There is no way to receive a signal indicating when a particular
57 /// object was dropped.
58 ///
59 /// # Example
60 ///
61 /// ```
62 /// use defer_drop::DeferDrop;
63 /// use std::time::{Instant, Duration};
64 /// use std::iter::repeat_with;
65 ///
66 /// let massive_vec: Vec<Vec<i32>> = repeat_with(|| vec![1, 2, 3])
67 /// .take(1_000_000)
68 /// .collect();
69 ///
70 /// let deferred = DeferDrop::new(massive_vec.clone());
71 ///
72 /// fn timer(f: impl FnOnce()) -> Duration {
73 /// let start = Instant::now();
74 /// f();
75 /// Instant::now() - start
76 /// }
77 ///
78 /// let drop1 = timer(move || drop(massive_vec));
79 /// let drop2 = timer(move || drop(deferred));
80 ///
81 /// assert!(drop2 < drop1);
82 /// ```
83 #[repr(transparent)]
84 #[derive(Debug, Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
85 pub struct DeferDrop<T: Send + 'static> {
86 inner: ManuallyDrop<T>,
87 }
88
89 impl<T: Send + 'static> DeferDrop<T> {
90 /// Create a new `DeferDrop` value.
91 #[inline]
92 pub fn new(value: T) -> Self {
93 DeferDrop {
94 inner: ManuallyDrop::new(value),
95 }
96 }
97
98 /// Unwrap the `DeferDrop`, returning the inner value. This has the effect
99 /// of cancelling the deferred drop behavior; ownership of the inner value
100 /// is transferred to the caller.
101 pub fn into_inner(mut this: Self) -> T {
102 let value = unsafe { ManuallyDrop::take(&mut this.inner) };
103 mem::forget(this);
104 value
105 }
106 }
107
108 static GARBAGE_CAN: OnceCell<GarbageCan> = OnceCell::new();
109
110 impl<T: Send + 'static> Drop for DeferDrop<T> {
111 fn drop(&mut self) {
112 GARBAGE_CAN
113 .get_or_init(|| GarbageCan::new("defer-drop background thread".to_owned()))
114 .throw_away(unsafe { ManuallyDrop::take(&mut self.inner) });
115 }
116 }
117
118 impl<T: Send + 'static> From<T> for DeferDrop<T> {
119 #[inline]
120 fn from(value: T) -> Self {
121 Self::new(value)
122 }
123 }
124
125 impl<T: Send + 'static> AsRef<T> for DeferDrop<T> {
126 #[inline]
127 fn as_ref(&self) -> &T {
128 &self.inner
129 }
130 }
131
132 impl<T: Send + 'static> AsMut<T> for DeferDrop<T> {
133 #[inline]
134 fn as_mut(&mut self) -> &mut T {
135 &mut self.inner
136 }
137 }
138
139 impl<T: Send + 'static> Deref for DeferDrop<T> {
140 type Target = T;
141
142 #[inline]
143 fn deref(&self) -> &Self::Target {
144 &self.inner
145 }
146 }
147
148 impl<T: Send + 'static> DerefMut for DeferDrop<T> {
149 #[inline]
150 fn deref_mut(&mut self) -> &mut Self::Target {
151 &mut self.inner
152 }
153 }
154
155 #[cfg(feature = "serde")]
156 impl<T: Serialize + Send + 'static> Serialize for DeferDrop<T> {
157 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
158 where
159 S: serde::Serializer,
160 {
161 self.as_ref().serialize(serializer)
162 }
163 }
164
165 #[cfg(feature = "serde")]
166 impl<'de, T: Deserialize<'de> + Send + 'static> Deserialize<'de> for DeferDrop<T> {
167 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
168 where
169 D: serde::Deserializer<'de>,
170 {
171 T::deserialize(deserializer).map(Self::new)
172 }
173 }
174
175 struct GarbageCan {
176 sender: Sender<Box<dyn Send>>,
177 handle: JoinHandle<()>,
178 }
179
180 impl GarbageCan {
181 fn new(name: String) -> Self {
182 let (sender, receiver) = channel::unbounded();
183
184 // TODO: drops should never panic, but if one does, we should
185 // probably abort the process
186 let handle = thread::Builder::new()
187 .name(name)
188 .spawn(move || receiver.into_iter().for_each(drop))
189 .expect("failed to spawn defer-drop background thread");
190
191 Self { sender, handle }
192 }
193
194 fn throw_away<T: Send + 'static>(&self, value: T) {
195 // Only send to the garbage can if we're not currently in the garbage
196 // can; if we are, just drop it eagerly.
197 if thread::current().id() != self.handle.thread().id() {
198 let boxed = Box::new(value);
199 self.sender.send(boxed).unwrap();
200 }
201 }
202 }
203
204 #[cfg(test)]
205 mod tests {
206 use crossbeam_channel as channel;
207 use std::{
208 sync::{Arc, Mutex},
209 thread,
210 time::Duration,
211 };
212
213 use crate::DeferDrop;
214
215 #[test]
216 fn test() {
217 /// This struct, when dropped, reports the thread ID of its dropping
218 /// thread to the channel
219 struct ThreadReporter {
220 chan: channel::Sender<thread::ThreadId>,
221 }
222
223 impl Drop for ThreadReporter {
224 fn drop(&mut self) {
225 self.chan.send(thread::current().id()).unwrap();
226 }
227 }
228
229 let (sender, receiver) = channel::bounded(1);
230 let this_thread_id = thread::current().id();
231
232 let thing = DeferDrop::new(ThreadReporter { chan: sender });
233 drop(thing);
234
235 match receiver.recv_timeout(Duration::from_secs(1)) {
236 Ok(id) => assert_ne!(
237 id, this_thread_id,
238 "thing wasn't dropped in a different thread"
239 ),
240 Err(_) => panic!("thing wasn't dropped within one second of being dropped"),
241 }
242 }
243
244 // Test that `DeferDrop` values that are sent into the dropper thread are
245 // dropped locally (that is, they don't re-send into the channel)
246 #[test]
247 fn test_no_recursive_send() {
248 #[allow(dead_code)]
249 struct DropOrderRecorder<T> {
250 id: u32,
251 value: T,
252 record: Arc<Mutex<Vec<u32>>>,
253 }
254
255 impl<T> Drop for DropOrderRecorder<T> {
256 fn drop(&mut self) {
257 self.record.lock().unwrap().push(self.id)
258 }
259 }
260
261 let drop_order_record: Arc<Mutex<Vec<u32>>> = Default::default();
262
263 let value = DeferDrop::new(DropOrderRecorder {
264 id: 0,
265 record: drop_order_record.clone(),
266 value: [
267 DeferDrop::new(DropOrderRecorder {
268 id: 1,
269 record: drop_order_record.clone(),
270 value: [
271 DeferDrop::new(DropOrderRecorder {
272 id: 2,
273 record: drop_order_record.clone(),
274 value: (),
275 }),
276 DeferDrop::new(DropOrderRecorder {
277 id: 3,
278 record: drop_order_record.clone(),
279 value: (),
280 }),
281 ],
282 }),
283 DeferDrop::new(DropOrderRecorder {
284 id: 4,
285 record: drop_order_record.clone(),
286 value: [
287 DeferDrop::new(DropOrderRecorder {
288 id: 5,
289 record: drop_order_record.clone(),
290 value: (),
291 }),
292 DeferDrop::new(DropOrderRecorder {
293 id: 6,
294 record: drop_order_record.clone(),
295 value: (),
296 }),
297 ],
298 }),
299 ],
300 });
301
302 drop(value);
303
304 loop {
305 thread::yield_now();
306 let lock = drop_order_record.lock().unwrap();
307 if lock.len() >= 7 {
308 break;
309 }
310 }
311
312 let lock = drop_order_record.lock().unwrap();
313
314 assert_eq!(lock.as_slice(), [0, 1, 2, 3, 4, 5, 6])
315 }
316 }