-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
I would like to create a custom map type that automatically assigns keys to values on insert instead of expecting the user to pass a key. This map should be a reactive store, in the same way as the std HashMap is. The struct would look like this:
use rustc_hash::FxHashMap;
pub struct UidStore<Index, Value> {
next_id: Index,
store: FxHashMap<Index, Value>,
}However, if I would simply derive Store on this struct, this would make next_id and store public, which I don't want. I only want the hash map to me modified through special methods on UidStore, that automatically choose a key based on the next_id field.
Hence, I thought about manually implementing the reactivity for my struct, but I am stuck. This is what I have so far, copied from the hash map implementation in the dioxus repo:
use std::hash::Hash;
use dioxus::{signals::Writable, stores::Store};
use rustc_hash::FxHashMap;
pub struct UidStore<Index, Value> {
next_id: Index,
store: FxHashMap<Index, Value>,
}
pub trait UidStoreExt<Index, Value, Lens> {
fn insert(&mut self, value: Value) -> Index
where
Index: Eq + Hash,
Lens: Writable;
}
impl<Index, Value, Lens> UidStoreExt<Index, Value, Lens> for Store<UidStore<Index, Value>, Lens> {
fn insert(&mut self, value: Value) -> Index
where
Index: Eq + Hash,
Lens: Writable,
{
let key = todo!("how do I access next_id here without making it known to the reactive system?");
todo!("how do I insert into the hash map here?")
}
}I have no idea how I should implement the insert method. I want the insert operation translate into an insert into the hash map. This should of course enable the hash map to be reactive, but avoid making the next_id reactive.
I hope this is kind of clear, feel free to ask clarifying questions.