DynamicSubscriptions

Struct DynamicSubscriptions 

Source
pub struct DynamicSubscriptions<U> { /* private fields */ }
Expand description

A container for managing multiple subscriptions with ID-based tracking.

This struct provides a common abstraction for scenarios that need to:

  • Store multiple subscriptions/items dynamically
  • Add new items and get a unique ID
  • Remove specific items by ID (e.g., when an inner observable completes)
  • Unsubscribe all items at once

§Design

  • SmallVec Optimization: Uses SmallVec<[_; 2]> to avoid heap allocation for the common case of 0-2 items.
  • Pre-allocation Pattern: Supports reserve_id() + insert() to handle cyclic dependencies where the ID is needed before the item exists.

§Examples

use rxrust::subscription::DynamicSubscriptions;

let mut subs: DynamicSubscriptions<()> = DynamicSubscriptions::default();

// Normal add pattern
let id1 = subs.add(());
assert_eq!(subs.len(), 1);

// Pre-allocation pattern (for cyclic dependencies)
let id2 = subs.reserve_id();
// ... create observer with id2 ...
// ... subscribe and get subscription ...
subs.insert(id2, ());
assert_eq!(subs.len(), 2);

// Remove by ID
assert!(subs.remove(id1).is_some());
assert_eq!(subs.len(), 1);

Implementations§

Source§

impl<U> DynamicSubscriptions<U>

Source

pub fn new() -> Self

Create an empty container.

Source

pub fn add(&mut self, item: U) -> usize

Add an item and return its unique ID.

Source

pub fn reserve_id(&mut self) -> usize

Reserve the next ID without adding an item.

Use this with insert() when you need the ID before the item exists (e.g., for cyclic dependencies in observer patterns).

Source

pub fn insert(&mut self, id: usize, item: U)

Insert an item with a pre-reserved ID.

The ID should have been obtained from reserve_id().

Source

pub fn remove(&mut self, id: usize) -> Option<U>

Remove an item by ID. Returns true if found and removed.

Source

pub fn contains(&self, id: usize) -> bool

Check if an ID exists in the container.

Source

pub fn len(&self) -> usize

Get the number of items.

Source

pub fn is_empty(&self) -> bool

Check if empty.

Source

pub fn drain(&mut self) -> impl Iterator<Item = U> + '_

Drain all items.

Source

pub fn iter(&self) -> impl Iterator<Item = &U>

Iterate over all items.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut U>

Iterate over all items mutably.

Source§

impl<U: Subscription> DynamicSubscriptions<U>

Source

pub fn unsubscribe_all(&mut self)

Unsubscribe all items and clear the container.

Source

pub fn all_closed(&self) -> bool

Check if all items are closed.

Trait Implementations§

Source§

impl<U> Default for DynamicSubscriptions<U>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<U> Freeze for DynamicSubscriptions<U>
where U: Freeze,

§

impl<U> RefUnwindSafe for DynamicSubscriptions<U>
where U: RefUnwindSafe,

§

impl<U> Send for DynamicSubscriptions<U>
where U: Send,

§

impl<U> Sync for DynamicSubscriptions<U>
where U: Sync,

§

impl<U> Unpin for DynamicSubscriptions<U>
where U: Unpin,

§

impl<U> UnwindSafe for DynamicSubscriptions<U>

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> 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, 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.