Skip to content

Add mutable IMap<K, V> stock implementation to windows-collections#4323

Merged
kennykerr merged 3 commits intomasterfrom
copilot/add-mutable-implementation-imap
Apr 29, 2026
Merged

Add mutable IMap<K, V> stock implementation to windows-collections#4323
kennykerr merged 3 commits intomasterfrom
copilot/add-mutable-implementation-imap

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

Adds a mutable IMap<K, V> stock implementation to windows-collections, mirroring the mutable IVector<T> added in #4322.

  • src/map.rs: StockMap<K, V> backed by RwLock<BTreeMap<K::Default, V::Default>>, implementing IMap<K, V> + IIterable<IKeyValuePair<K, V>>. Insert returns true when replacing an existing key; Remove returns E_BOUNDS for missing keys. GetView returns a point-in-time snapshot.
  • StockMapIterator: holds a ComObject<StockMap<K, V>> reference and an AtomicUsize current index (same pattern as StockVectorIterator), reading directly from the owner's RwLock<BTreeMap> on each call — no upfront copy at First() time.
  • src/key_value_pair.rs: shared StockKeyValuePair<K, V> type extracted into its own module and used by both map.rs and map_view.rs, eliminating the previous duplication.
  • From<BTreeMap<K::Default, V::Default>> impl for IMap<K, V>.
  • Tests: covers read, mutate, iterator, GetView snapshot isolation, and HSTRING keys.
let m = IMap::<HSTRING, i32>::from(BTreeMap::new());
m.Insert(h!("key"), 42)?;
assert_eq!(m.Lookup(h!("key"))?, 42);
assert!(m.Insert(h!("key"), 99)?); // true = replaced

let view = m.GetView()?;
m.Insert(h!("other"), 1)?;
assert_eq!(view.Size()?, 1); // snapshot unaffected

Comment thread crates/libs/collections/src/map.rs Outdated
V::Default: Clone,
{
fn First(&self) -> Result<IIterator<IKeyValuePair<K, V>>> {
let pairs: Vec<(K::Default, V::Default)> = self
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot The Vector implementation avoids the copy in its IIterable implementation. Is there a reason that is not possible here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in cbd0426. StockMapIterator now holds owner: ComObject<StockMap<K, V>> and an AtomicUsize current index — the same pattern as StockVectorIterator. First() no longer copies the map entries; iterator methods read directly from the owner's RwLock<BTreeMap> using map.iter().nth(current) / map.iter().skip(current).

Comment thread crates/libs/collections/src/map.rs Outdated
}

#[implement(IKeyValuePair<K, V>)]
struct StockMapKeyValuePair<K, V>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot this looks like the same as the one used in map_view.rs - can they be shared?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c18cd21. The type is now defined once in a new key_value_pair.rs module as StockKeyValuePair, and both map.rs and map_view.rs reference it via super::key_value_pair::StockKeyValuePair. The duplicate StockMapKeyValuePair in map.rs and the old StockKeyValuePair in map_view.rs have been removed.

@kennykerr kennykerr merged commit fb5dacd into master Apr 29, 2026
28 checks passed
@kennykerr kennykerr deleted the copilot/add-mutable-implementation-imap branch April 29, 2026 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants