Module collection

Module collection 

Source
Expand description

Reactive collections with watcher support.

This module provides a trait-based approach for creating observable collections that can notify watchers when their contents change. It supports both reactive collections that emit change notifications and static collections that provide one-time snapshots.

§Core Components

  • Collection: A trait defining the interface for observable collections
  • List<T>: A reactive list implementation using Rc<RefCell<Vec<T>>>
  • AnyCollection<T>: A type-erased wrapper for storing different collection types

§Collection Types

The module provides Collection implementations for:

  • List<T>: Fully reactive with ongoing change notifications
  • Vec<T>: Static collection with one-time watcher notifications
  • [T; N]: Static array with one-time watcher notifications

§Usage Example

use nami::collection::{Collection, List};

// Create a reactive list
let mut list = List::new();
list.push(1);
list.push(2);

// Watch for changes in a specific range
let _guard = list.watch(0..2, |ctx| {
    println!("Items changed: {:?}", ctx.into_value());
});

// Modifications will trigger the watcher
list.push(3);

§Range-based Watching

All collections support range-based watching using standard Rust range syntax:

  • collection.watch(.., watcher) - Watch entire collection
  • collection.watch(1..5, watcher) - Watch indices 1 through 4
  • collection.watch(2.., watcher) - Watch from index 2 to end
  • collection.watch(..3, watcher) - Watch from start to index 2

§Type Erasure

The AnyCollection wrapper allows storing different collection types in the same container while preserving the ability to observe them:

use nami::collection::{AnyCollection, List};

let list = List::from(vec![1, 2, 3]);
let any_collection = AnyCollection::new(list);

// Still supports watching despite type erasure
let _guard = any_collection.watch(.., |ctx| {
    // Handle change notifications
});

Structs§

AnyCollection
A type-erased wrapper for any collection that implements Collection.
List
A reactive list that can be observed for changes.

Traits§

Collection
A trait for collections that can be observed for changes.

Type Aliases§

BoxCollectionWatcher
A boxed collection watcher.