Skip to content

Commit a0371d5

Browse files
authored
Re-export AHashMap and give it a trait extension (rojo-rbx#479)
1 parent 0cd17fa commit a0371d5

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

rbx_dom_weak/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ pub fn into_raw(self) -> (Ref, HashMap<Ref, Instance, RandomState>) {
9595
```
9696
to
9797
```rust
98-
pub fn into_raw(self) -> (Ref, HashMap<Ref, Instance, ahash::RandomState>) {
98+
pub fn into_raw(self) -> (Ref, AHashMap<Ref, Instance>) {
9999
```
100100

101101
### Other changes
102-
* Added `UstrMapExt`, a helper trait providing convenience methods `UstrMap::new` and `UstrMap::with_capacity`.
102+
* Added `HashMapExt`, a helper trait providing convenience methods `UstrMap::new`, `UstrMap::with_capacity`, `AHashMap::new`, and `AHashMap::with_capacity`.
103+
* Added re-exports for `AHashMap`.
103104
* Added re-exports for `ustr` (a convenience function for creating `Ustr`s), `Ustr`, `UstrMap`, and `UstrSet`.
104105
* Added `InstanceBuilder::with_property_capacity`, which can preallocate an `InstanceBuilder`'s property table. [#464]
105106
* Added `WeakDom::reserve`, which can preallocate additional space for instances in the `WeakDom`. [#465]

rbx_dom_weak/src/dom.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::VecDeque;
22

3-
use ahash::{HashMap, HashMapExt, HashSet, HashSetExt};
3+
use ahash::{AHashMap, AHashSet};
44
use rbx_types::{Ref, UniqueId, Variant};
55
use ustr::ustr;
66

@@ -15,18 +15,18 @@ use crate::instance::{Instance, InstanceBuilder};
1515
/// objects and insert them into the tree.
1616
#[derive(Debug)]
1717
pub struct WeakDom {
18-
instances: HashMap<Ref, Instance>,
18+
instances: AHashMap<Ref, Instance>,
1919
root_ref: Ref,
20-
unique_ids: HashSet<UniqueId>,
20+
unique_ids: AHashSet<UniqueId>,
2121
}
2222

2323
impl WeakDom {
2424
/// Construct a new `WeakDom` described by the given [`InstanceBuilder`].
2525
pub fn new(builder: InstanceBuilder) -> WeakDom {
2626
let mut dom = WeakDom {
27-
instances: HashMap::new(),
27+
instances: AHashMap::new(),
2828
root_ref: builder.referent,
29-
unique_ids: HashSet::new(),
29+
unique_ids: AHashSet::new(),
3030
};
3131

3232
dom.insert(Ref::none(), builder);
@@ -42,7 +42,7 @@ impl WeakDom {
4242
/// Consumes the WeakDom, returning its underlying root ref and backing
4343
/// storage. This method is useful when tree-preserving operations are too
4444
/// slow.
45-
pub fn into_raw(self) -> (Ref, HashMap<Ref, Instance>) {
45+
pub fn into_raw(self) -> (Ref, AHashMap<Ref, Instance>) {
4646
(self.root_ref, self.instances)
4747
}
4848

@@ -435,24 +435,24 @@ impl<'a> Iterator for WeakDomDescendants<'a> {
435435
impl Default for WeakDom {
436436
fn default() -> WeakDom {
437437
WeakDom {
438-
instances: HashMap::new(),
438+
instances: AHashMap::new(),
439439
root_ref: Ref::none(),
440-
unique_ids: HashSet::new(),
440+
unique_ids: AHashSet::new(),
441441
}
442442
}
443443
}
444444

445445
#[derive(Debug, Default)]
446446
struct CloneContext {
447447
queue: VecDeque<(Ref, Ref)>,
448-
ref_rewrites: HashMap<Ref, Ref>,
448+
ref_rewrites: AHashMap<Ref, Ref>,
449449
}
450450

451451
impl CloneContext {
452452
/// On any instances cloned during the operation, rewrite any Ref properties that
453453
/// point to instances that were also cloned.
454454
fn rewrite_refs(self, dest: &mut WeakDom) {
455-
let mut existing_dest_refs = HashSet::new();
455+
let mut existing_dest_refs = AHashSet::new();
456456

457457
for (_, new_ref) in self.ref_rewrites.iter() {
458458
let instance = dest

rbx_dom_weak/src/lib.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ mod viewer;
4747

4848
pub use rbx_types as types;
4949

50+
pub use ahash::AHashMap;
5051
pub use ustr::{ustr, Ustr, UstrMap, UstrSet};
5152

5253
pub use crate::{
@@ -55,22 +56,37 @@ pub use crate::{
5556
viewer::{DomViewer, ViewedInstance},
5657
};
5758

58-
/// Helper trait that provides convenience methods for `UstrMap`.
59-
pub trait UstrMapExt {
60-
/// Creates an empty `UstrMap` using the default value for its hasher.
59+
/// Helper trait that provides convenience methods for `AHashMap` and `UstrMap`.
60+
pub trait HashMapExt {
61+
/// Constructs an empty map.
6162
fn new() -> Self;
6263

63-
/// Creates an empty `UstrMap` with at least the specified capacity using
64-
/// the default value for its hasher.
64+
/// Constructs an empty map with at least the specified capacity.
6565
fn with_capacity(capacity: usize) -> Self;
6666
}
6767

68-
impl<V> UstrMapExt for UstrMap<V> {
68+
impl<V> HashMapExt for UstrMap<V> {
69+
/// Creates an empty `UstrMap` using the default value for its hasher.
6970
fn new() -> Self {
7071
UstrMap::default()
7172
}
7273

74+
/// Creates an empty `UstrMap` with at least the specified capacity using
75+
/// the default value for its hasher.
7376
fn with_capacity(capacity: usize) -> Self {
7477
UstrMap::with_capacity_and_hasher(capacity, Default::default())
7578
}
7679
}
80+
81+
impl<K, V> HashMapExt for AHashMap<K, V> {
82+
/// Creates an empty `AHashMap` using the default value for its hasher.
83+
fn new() -> Self {
84+
AHashMap::default()
85+
}
86+
87+
/// Creates an empty `AHashMap` with at least the specified capacity using
88+
/// the default value for its hasher.
89+
fn with_capacity(capacity: usize) -> Self {
90+
AHashMap::with_capacity_and_hasher(capacity, Default::default())
91+
}
92+
}

0 commit comments

Comments
 (0)