rx_rust/disposable/
boxed_disposal.rs1use crate::{disposable::Disposable, utils::types::NecessarySendSync};
2
3cfg_if::cfg_if! {
4 if #[cfg(feature = "single-threaded")] {
5 pub struct BoxedDisposal<'dis>(Box<dyn FnOnce() + 'dis>);
7 } else {
8 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}
26
27impl std::fmt::Debug for BoxedDisposal<'_> {
28 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29 f.write_str(std::any::type_name::<Self>())
30 }
31}