rx_rust/disposable/
bound_drop_disposal.rs

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