pub struct ClosureSubscription<F>(pub F);Expand description
A functional adapter that turns a closure into a Subscription.
This struct allows users to define teardown/unsubscription logic using a
simple closure, without needing to define a custom struct that implements
the Subscription trait. It is particularly useful when working with the
create operator, where you
often need to return a cleanup action.
§Why is this needed?
Rust’s orphan rules and type inference limitations prevent us from directly
implementing Subscription for all FnOnce() closures globally.
ClosureSubscription serves as an explicit wrapper to tell the compiler:
“Treat this closure as a Subscription”.
§Zero-Cost Abstraction
This is a newtype wrapper that compiles down to a direct function call. There is no runtime overhead compared to calling the closure directly.
§Example
use std::convert::Infallible;
use rxrust::{prelude::*, subscription::ClosureSubscription};
Local::create::<(), Infallible, _, _>(|_emitter| {
println!("Subscribed");
// Return a closure wrapped in ClosureSubscription as the teardown logic
ClosureSubscription(move || {
println!("Unsubscribed - cleaning up resources");
})
});Tuple Fields§
§0: FTrait Implementations§
Source§impl<F> Subscription for ClosureSubscription<F>where
F: FnOnce(),
impl<F> Subscription for ClosureSubscription<F>where
F: FnOnce(),
Source§fn unsubscribe(self)
fn unsubscribe(self)
Cancel the subscription (terminal operation, consumes self)
Source§fn unsubscribe_when_dropped(self) -> SubscriptionGuard<Self>where
Self: Sized,
fn unsubscribe_when_dropped(self) -> SubscriptionGuard<Self>where
Self: Sized,
Activates “RAII” behavior for this subscription. That means
unsubscribe() will be called automatically as soon as the returned
value goes out of scope. Read moreAuto Trait Implementations§
impl<F> Freeze for ClosureSubscription<F>where
F: Freeze,
impl<F> RefUnwindSafe for ClosureSubscription<F>where
F: RefUnwindSafe,
impl<F> Send for ClosureSubscription<F>where
F: Send,
impl<F> Sync for ClosureSubscription<F>where
F: Sync,
impl<F> Unpin for ClosureSubscription<F>where
F: Unpin,
impl<F> UnwindSafe for ClosureSubscription<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> BoxedSubscriptionInner for Twhere
T: Subscription,
impl<T> BoxedSubscriptionInner for Twhere
T: Subscription,
fn boxed_unsubscribe(self: Box<T>)
fn boxed_is_closed(&self) -> bool
Source§impl<T> IntoBoxedSubscription<BoxedSubscription> for Twhere
T: Subscription + 'static,
impl<T> IntoBoxedSubscription<BoxedSubscription> for Twhere
T: Subscription + 'static,
Source§fn into_boxed(self) -> BoxedSubscription
fn into_boxed(self) -> BoxedSubscription
Convert this subscription into a boxed subscription.
Source§impl<T> IntoBoxedSubscription<BoxedSubscriptionSend> for Twhere
T: Subscription + Send + 'static,
impl<T> IntoBoxedSubscription<BoxedSubscriptionSend> for Twhere
T: Subscription + Send + 'static,
Source§fn into_boxed(self) -> BoxedSubscriptionSend
fn into_boxed(self) -> BoxedSubscriptionSend
Convert this subscription into a boxed subscription.