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>
impl<U> DynamicSubscriptions<U>
Sourcepub fn reserve_id(&mut self) -> usize
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).
Sourcepub fn insert(&mut self, id: usize, item: U)
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§impl<U: Subscription> DynamicSubscriptions<U>
impl<U: Subscription> DynamicSubscriptions<U>
Sourcepub fn unsubscribe_all(&mut self)
pub fn unsubscribe_all(&mut self)
Unsubscribe all items and clear the container.
Sourcepub fn all_closed(&self) -> bool
pub fn all_closed(&self) -> bool
Check if all items are closed.
Trait Implementations§
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>where
U: RefUnwindSafe + 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