pub struct CellMap<K, V, M = CellMutable>{ /* private fields */ }Expand description
A reactive HashMap with per-key observability.
§Example
use hyphae::{CellMap, Gettable, Watchable, Signal};
let map = CellMap::<String, i32>::new();
// Observe a specific key
let value_cell = map.get(&"counter".to_string());
assert_eq!(value_cell.get(), None);
// Insert triggers update
map.insert("counter".to_string(), 42);
assert_eq!(value_cell.get(), Some(42));
// Observe all entries
let entries = map.entries();
assert_eq!(entries.get().len(), 1);Implementations§
Source§impl<K, V> CellMap<K, V, CellMutable>
impl<K, V> CellMap<K, V, CellMutable>
Sourcepub fn own(&self, guard: SubscriptionGuard)
pub fn own(&self, guard: SubscriptionGuard)
Own a subscription guard, keeping it alive as long as this CellMap exists.
Sourcepub fn own_guard(&self, guard: SubscriptionGuard)
pub fn own_guard(&self, guard: SubscriptionGuard)
Own a subscription guard, keeping it alive as long as this CellMap exists.
This enables building custom reactive CellMaps driven by external cells.
Sourcepub fn insert(&self, key: K, value: V) -> Option<V>
pub fn insert(&self, key: K, value: V) -> Option<V>
Insert a key-value pair, returning the old value if present.
Sourcepub fn insert_many(&self, entries: Vec<(K, V)>)
pub fn insert_many(&self, entries: Vec<(K, V)>)
Insert multiple key-value pairs and emit a single batch diff.
Sourcepub fn remove_many(&self, keys: Vec<K>)
pub fn remove_many(&self, keys: Vec<K>)
Remove multiple keys and emit a single batch diff.
Sourcepub fn replace_all(&self, entries: Vec<(K, V)>)
pub fn replace_all(&self, entries: Vec<(K, V)>)
Replace all entries atomically, emitting a single Batch diff.
Removes keys not in entries, inserts/updates keys that are.
Skips no-op updates (existing key with equal value).
Emits MapDiff::Batch with the actual changes so downstream
subscribers see one atomic replacement instead of N individual diffs.
Sourcepub fn apply_batch(&self, changes: Vec<MapDiff<K, V>>)
pub fn apply_batch(&self, changes: Vec<MapDiff<K, V>>)
Apply a batch of diffs and emit them as one MapDiff::Batch.
Sourcepub fn with_name(self, name: impl Into<Arc<str>>) -> Self
pub fn with_name(self, name: impl Into<Arc<str>>) -> Self
Give this CellMap a name for debugging. Names its internal cells accordingly.
Sourcepub fn lock(self) -> CellMap<K, V, CellImmutable>
pub fn lock(self) -> CellMap<K, V, CellImmutable>
Lock the map to prevent further mutations.
Source§impl<K, V, M> CellMap<K, V, M>
impl<K, V, M> CellMap<K, V, M>
Sourcepub fn downgrade(&self) -> WeakCellMap<K, V>
pub fn downgrade(&self) -> WeakCellMap<K, V>
Create a weak handle to this map.
Sourcepub fn get(&self, key: &K) -> Cell<Option<V>, CellImmutable>
pub fn get(&self, key: &K) -> Cell<Option<V>, CellImmutable>
Get an observable Cell for a specific key.
Returns a Cell<Option<V>> that updates whenever the key’s value changes.
Multiple calls with the same key return the same underlying Cell.
Sourcepub fn entries(&self) -> Cell<Vec<(K, V)>, CellImmutable>
pub fn entries(&self) -> Cell<Vec<(K, V)>, CellImmutable>
Get an observable Cell of all entries.
Returns a derived cell that maintains entries incrementally via diffs. The initial value is computed from the current map state, then updates are applied incrementally as O(1) operations per diff.
Sourcepub fn items(&self) -> Cell<Vec<V>, CellImmutable>
pub fn items(&self) -> Cell<Vec<V>, CellImmutable>
Get an observable Cell of all values.
This maintains its own diff-driven projection to avoid forcing an intermediate entries materialization on hot value-only paths.
Sourcepub fn keys(&self) -> Cell<Vec<K>, CellImmutable>
pub fn keys(&self) -> Cell<Vec<K>, CellImmutable>
Get an observable Cell of all keys.
Sourcepub fn size(&self) -> Cell<usize, CellImmutable>
pub fn size(&self) -> Cell<usize, CellImmutable>
Get an observable Cell of the map size.
This is the preferred reactive count operator because it reuses the internally maintained length cell instead of materializing entries.
Sourcepub fn len(&self) -> Cell<usize, CellImmutable>
pub fn len(&self) -> Cell<usize, CellImmutable>
Get an observable Cell of the map length.
Alias for CellMap::size.
Sourcepub fn diffs(&self) -> Cell<MapDiff<K, V>, CellImmutable>
pub fn diffs(&self) -> Cell<MapDiff<K, V>, CellImmutable>
Get an observable Cell of diff notifications.
Emits MapDiff updates. Starts with MapDiff::Initial { entries: vec![] }.
Sourcepub fn snapshot(&self) -> Vec<(K, V)>
pub fn snapshot(&self) -> Vec<(K, V)>
Get a point-in-time snapshot of all entries (non-reactive).
Unlike entries(), this does NOT create a Cell or subscribe to changes.
Use this for one-shot reads where you don’t need live updates.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Check if key exists (non-reactive).
Sourcepub fn subscribe_diffs<F>(&self, callback: F) -> SubscriptionGuard
pub fn subscribe_diffs<F>(&self, callback: F) -> SubscriptionGuard
Subscribe to diffs with an initial snapshot.
The callback is first called with MapDiff::Initial containing all current
entries, then called with subsequent diffs as the map changes.
Returns a guard that cancels the subscription when dropped.
Source§impl<K, V, M> CellMap<K, V, M>
impl<K, V, M> CellMap<K, V, M>
Sourcepub fn nest<PK>(
&self,
fk: impl Fn(&V) -> PK + Send + Sync + 'static,
) -> NestedMap<PK, K, V>
pub fn nest<PK>( &self, fk: impl Fn(&V) -> PK + Send + Sync + 'static, ) -> NestedMap<PK, K, V>
Create a grouped view of this map, partitioned by a foreign-key extractor.
The returned NestedMap observes this CellMap reactively and
maintains a live FK index. It implements ReactiveKeys and
ReactiveMap, so it composes with joins exactly like a CellMap.
let orders: CellMap<OrderId, Order> = CellMap::new();
let nested = orders.nest(|order| order.customer_id.clone());
// nested: NestedMap<CustomerId, OrderId, Order>Trait Implementations§
Source§impl<K, V> Default for CellMap<K, V, CellMutable>
impl<K, V> Default for CellMap<K, V, CellMutable>
Source§impl<K, V, M> MapQuery<K, V> for CellMap<K, V, M>
impl<K, V, M> MapQuery<K, V> for CellMap<K, V, M>
Source§fn materialize(self) -> CellMap<K, V, CellImmutable>
fn materialize(self) -> CellMap<K, V, CellImmutable>
No-op materialize: the cell map is already a cached, multicast source.
Just flip the marker to CellImmutable and reuse the same Arc<inner>.
Source§impl<K, V, M> ReactiveKeys for CellMap<K, V, M>
impl<K, V, M> ReactiveKeys for CellMap<K, V, M>
Auto Trait Implementations§
impl<K, V, M> Freeze for CellMap<K, V, M>
impl<K, V, M = CellMutable> !RefUnwindSafe for CellMap<K, V, M>
impl<K, V, M> Send for CellMap<K, V, M>where
M: Send,
impl<K, V, M> Sync for CellMap<K, V, M>where
M: Sync,
impl<K, V, M> Unpin for CellMap<K, V, M>where
M: Unpin,
impl<K, V, M> UnsafeUnpin for CellMap<K, V, M>
impl<K, V, M = CellMutable> !UnwindSafe for CellMap<K, V, M>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<K, V, M> CountByExt<K, V> for M
impl<K, V, M> CountByExt<K, V> for M
Source§impl<K, V, M> GroupByExt<K, V> for M
impl<K, V, M> GroupByExt<K, V> for M
Source§impl<K, V, M> InnerJoinExt<K, V> for M
impl<K, V, M> InnerJoinExt<K, V> for M
Source§fn inner_join<R, RV>(self, right: R) -> InnerJoinByKeyPlan<Self, R, K, V, RV>
fn inner_join<R, RV>(self, right: R) -> InnerJoinByKeyPlan<Self, R, K, V, RV>
Source§fn inner_join_fk<R, RK, RV>(
self,
right: R,
) -> InnerJoinByPairPlan<Self, R, K, V, RK, RV, K, impl Fn(&K, &V) -> K + Send + Sync + 'static, impl Fn(&RK, &RV) -> K + Send + Sync + 'static>where
R: MapQuery<RK, RV>,
RK: Hash + Eq + CellValue,
RV: CellValue + HasForeignKey<V>,
<<RV as HasForeignKey<V>>::ForeignKey as IdFor<V>>::MapKey: Into<K>,
fn inner_join_fk<R, RK, RV>(
self,
right: R,
) -> InnerJoinByPairPlan<Self, R, K, V, RK, RV, K, impl Fn(&K, &V) -> K + Send + Sync + 'static, impl Fn(&RK, &RV) -> K + Send + Sync + 'static>where
R: MapQuery<RK, RV>,
RK: Hash + Eq + CellValue,
RV: CellValue + HasForeignKey<V>,
<<RV as HasForeignKey<V>>::ForeignKey as IdFor<V>>::MapKey: Into<K>,
Source§fn inner_join_by<R, RK, RV, JK, FL, FR>(
self,
right: R,
left_key: FL,
right_key: FR,
) -> InnerJoinByPairPlan<Self, R, K, V, RK, RV, JK, FL, FR>
fn inner_join_by<R, RK, RV, JK, FL, FR>( self, right: R, left_key: FL, right_key: FR, ) -> InnerJoinByPairPlan<Self, R, K, V, RK, RV, JK, FL, FR>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more