rx_rust/disposable/
bound_drop_disposal.rs

1use crate::disposable::Disposable;
2use educe::Educe;
3
4/// A disposal that calls the `dispose` method of a `Disposable` when dropped.
5#[derive(Educe)]
6#[educe(Debug)]
7pub struct BoundDropDisposal<T>(Option<T>)
8where
9    T: Disposable;
10
11impl<T> BoundDropDisposal<T>
12where
13    T: Disposable,
14{
15    pub fn new(disposal: T) -> Self {
16        Self(Some(disposal))
17    }
18}
19
20impl<T> Disposable for BoundDropDisposal<T>
21where
22    T: Disposable,
23{
24    fn dispose(self) {
25        // drop self to call the dispose
26    }
27}
28
29impl<T> Drop for BoundDropDisposal<T>
30where
31    T: Disposable,
32{
33    fn drop(&mut self) {
34        if let Some(disposal) = self.0.take() {
35            disposal.dispose();
36        }
37    }
38}