ClosureSubscription

Struct ClosureSubscription 

Source
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: F

Trait Implementations§

Source§

impl<F> Subscription for ClosureSubscription<F>
where F: FnOnce(),

Source§

fn unsubscribe(self)

Cancel the subscription (terminal operation, consumes self)
Source§

fn is_closed(&self) -> bool

Check if the subscription is closed (completed or unsubscribed)
Source§

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 more

Auto 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> BoxedSubscriptionInner for T
where T: Subscription,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoBoxedSubscription<BoxedSubscription> for T
where T: Subscription + 'static,

Source§

fn into_boxed(self) -> BoxedSubscription

Convert this subscription into a boxed subscription.
Source§

impl<T> IntoBoxedSubscription<BoxedSubscriptionSend> for T
where T: Subscription + Send + 'static,

Source§

fn into_boxed(self) -> BoxedSubscriptionSend

Convert this subscription into a boxed subscription.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.