Skip to main content

rx_rust/disposable/
mod.rs

1pub mod bound_drop_disposal;
2pub mod boxed_disposal;
3pub mod callback_disposal;
4pub mod chain_disposal;
5mod delegate_disposal;
6pub mod empty_disposal;
7pub mod option_disposal;
8pub mod shared_disposal;
9pub use crate::delegate_disposal;
10use crate::{
11    disposable::{
12        boxed_disposal::BoxedDisposal, chain_disposal::ChainDisposal,
13        either_disposal::EitherDisposal, option_disposal::OptionDisposal,
14    },
15    observable::Subscription,
16    utils::types::MaybeSend,
17};
18pub mod either_disposal;
19
20/// A trait that represents a disposable resource.
21pub trait Disposable {
22    /// Disposes of the resource.
23    fn dispose(self);
24}
25
26pub trait DisposableExt: Disposable + Sized {
27    fn into_boxed<'dis>(self) -> BoxedDisposal<'dis>
28    where
29        Self: MaybeSend + 'dis,
30    {
31        BoxedDisposal::new(self)
32    }
33
34    /// Converts this disposal into a subscription whose inner disposal is
35    /// created through [`From`].
36    fn into_subscription<D>(self) -> Subscription<D>
37    where
38        D: Disposable + From<Self>,
39    {
40        Subscription::new(self.into())
41    }
42
43    fn into_option(self) -> OptionDisposal<Self> {
44        OptionDisposal::some(self)
45    }
46
47    fn then<D: Disposable>(self, other: D) -> ChainDisposal<Self, D> {
48        ChainDisposal::new(self, other)
49    }
50
51    fn into_left<D2: Disposable>(self) -> EitherDisposal<Self, D2> {
52        EitherDisposal::Left(self)
53    }
54
55    fn into_right<D1: Disposable>(self) -> EitherDisposal<D1, Self> {
56        EitherDisposal::Right(self)
57    }
58}
59
60impl<D> DisposableExt for D where D: Disposable {}