rx_rust/disposable/
boxed_disposal.rs

1use crate::{disposable::Disposable, utils::types::NecessarySendSync};
2
3cfg_if::cfg_if! {
4    if #[cfg(feature = "single-threaded")] {
5        /// Type-erased disposal for single-threaded builds to handle this problem <https://stackoverflow.com/q/46620790/9315497>
6        pub struct BoxedDisposal<'dis>(Box<dyn FnOnce() + 'dis>);
7    } else {
8        /// Type-erased disposal for multi-threaded builds to handle this problem <https://stackoverflow.com/q/46620790/9315497>
9        pub struct BoxedDisposal<'dis>(Box<dyn FnOnce() + Send + Sync + 'dis>);
10    }
11}
12
13impl<'dis> BoxedDisposal<'dis> {
14    pub fn new(disposal: impl Disposable + NecessarySendSync + 'dis) -> Self {
15        Self(Box::new(|| {
16            disposal.dispose();
17        }))
18    }
19}
20
21impl Disposable for BoxedDisposal<'_> {
22    fn dispose(self) {
23        self.0();
24    }
25}