@@ -5,9 +5,10 @@ use core::fmt;
5
5
use core:: hash;
6
6
use core:: iter;
7
7
8
+ use crate :: alloc:: hashbrown:: raw:: RawIter ;
8
9
use crate :: alloc:: prelude:: * ;
9
10
use crate :: alloc:: { self , String } ;
10
- use crate :: alloc:: { btree_map , BTreeMap } ;
11
+ use crate :: alloc:: { hash_map , HashMap } ;
11
12
12
13
use crate as rune;
13
14
use crate :: compile:: ItemBuf ;
@@ -21,7 +22,7 @@ use crate::Any;
21
22
///
22
23
/// [`into_iter`]: struct.Object.html#method.into_iter
23
24
/// [`Object`]: struct.Object.html
24
- pub type IntoIter = btree_map :: IntoIter < String , Value > ;
25
+ pub type IntoIter = hash_map :: IntoIter < String , Value > ;
25
26
26
27
/// A mutable iterator over the entries of a `Object`.
27
28
///
@@ -30,7 +31,7 @@ pub type IntoIter = btree_map::IntoIter<String, Value>;
30
31
///
31
32
/// [`iter_mut`]: struct.Object.html#method.iter_mut
32
33
/// [`Object`]: struct.Object.html
33
- pub type IterMut < ' a > = btree_map :: IterMut < ' a , String , Value > ;
34
+ pub type IterMut < ' a > = hash_map :: IterMut < ' a , String , Value > ;
34
35
35
36
/// An iterator over the entries of a `Object`.
36
37
///
@@ -39,7 +40,7 @@ pub type IterMut<'a> = btree_map::IterMut<'a, String, Value>;
39
40
///
40
41
/// [`iter`]: struct.Object.html#method.iter
41
42
/// [`Object`]: struct.Object.html
42
- pub type Iter < ' a > = btree_map :: Iter < ' a , String , Value > ;
43
+ pub type Iter < ' a > = hash_map :: Iter < ' a , String , Value > ;
43
44
44
45
/// An iterator over the keys of a `HashMap`.
45
46
///
@@ -48,7 +49,7 @@ pub type Iter<'a> = btree_map::Iter<'a, String, Value>;
48
49
///
49
50
/// [`keys`]: struct.Object.html#method.keys
50
51
/// [`Object`]: struct.Object.html
51
- pub type Keys < ' a > = btree_map :: Keys < ' a , String , Value > ;
52
+ pub type Keys < ' a > = hash_map :: Keys < ' a , String , Value > ;
52
53
53
54
/// An iterator over the values of a `HashMap`.
54
55
///
@@ -57,7 +58,7 @@ pub type Keys<'a> = btree_map::Keys<'a, String, Value>;
57
58
///
58
59
/// [`values`]: struct.Object.html#method.values
59
60
/// [`Object`]: struct.Object.html
60
- pub type Values < ' a > = btree_map :: Values < ' a , String , Value > ;
61
+ pub type Values < ' a > = hash_map :: Values < ' a , String , Value > ;
61
62
62
63
/// Struct representing a dynamic anonymous object.
63
64
///
@@ -82,7 +83,7 @@ pub type Values<'a> = btree_map::Values<'a, String, Value>;
82
83
#[ repr( transparent) ]
83
84
#[ rune( builtin, static_type = OBJECT_TYPE ) ]
84
85
pub struct Object {
85
- inner : BTreeMap < String , Value > ,
86
+ inner : HashMap < String , Value > ,
86
87
}
87
88
88
89
impl Object {
@@ -98,7 +99,7 @@ impl Object {
98
99
#[ rune:: function( keep, path = Self :: new) ]
99
100
pub fn new ( ) -> Self {
100
101
Self {
101
- inner : BTreeMap :: new ( ) ,
102
+ inner : HashMap :: new ( ) ,
102
103
}
103
104
}
104
105
@@ -117,11 +118,11 @@ impl Object {
117
118
}
118
119
119
120
/// Construct a new object with the given capacity.
120
- pub fn with_capacity ( # [ allow ( unused ) ] capacity : usize ) -> alloc:: Result < Self > {
121
+ pub fn with_capacity ( capacity : usize ) -> alloc:: Result < Self > {
121
122
// BTreeMap doesn't support setting capacity on creation but we keep
122
123
// this here in case we want to switch store later.
123
124
Ok ( Self {
124
- inner : BTreeMap :: new ( ) ,
125
+ inner : HashMap :: try_with_capacity ( capacity ) ? ,
125
126
} )
126
127
}
127
128
@@ -248,7 +249,7 @@ impl Object {
248
249
///
249
250
/// If the map did not have this key present, `None` is returned.
250
251
pub fn insert ( & mut self , k : String , v : Value ) -> alloc:: Result < Option < Value > > {
251
- Ok ( self . inner . try_insert ( k, v) ? )
252
+ self . inner . try_insert ( k, v)
252
253
}
253
254
254
255
/// Clears the object, removing all key-value pairs. Keeps the allocated
@@ -260,7 +261,7 @@ impl Object {
260
261
}
261
262
262
263
/// Convert into inner.
263
- pub fn into_inner ( self ) -> BTreeMap < String , Value > {
264
+ pub fn into_inner ( self ) -> HashMap < String , Value > {
264
265
self . inner
265
266
}
266
267
@@ -308,30 +309,31 @@ impl Object {
308
309
#[ rune:: function( keep, path = Self :: iter) ]
309
310
pub fn rune_iter ( this : Ref < Self > ) -> Iterator {
310
311
struct Iter {
311
- iter : btree_map :: IterRaw < String , Value > ,
312
+ iter : RawIter < ( String , Value ) > ,
312
313
_guard : RawRef ,
313
314
}
314
315
315
316
impl iter:: Iterator for Iter {
316
317
type Item = VmResult < ( String , Value ) > ;
317
318
318
319
fn next ( & mut self ) -> Option < Self :: Item > {
319
- let ( key, value) = self . iter . next ( ) ?;
320
-
321
320
unsafe {
322
- let key = match ( * key) . try_clone ( ) {
321
+ let bucket = self . iter . next ( ) ?;
322
+ let ( key, value) = bucket. as_ref ( ) ;
323
+
324
+ let key = match key. try_clone ( ) {
323
325
Ok ( key) => key,
324
326
Err ( err) => return Some ( VmResult :: err ( err) ) ,
325
327
} ;
326
328
327
- Some ( VmResult :: Ok ( ( key, ( * value) . clone ( ) ) ) )
329
+ Some ( VmResult :: Ok ( ( key, value. clone ( ) ) ) )
328
330
}
329
331
}
330
332
}
331
333
332
334
// SAFETY: we're holding onto the related reference guard, and making
333
335
// sure that it's dropped after the iterator.
334
- let iter = unsafe { this. inner . iter_raw ( ) } ;
336
+ let iter = unsafe { this. inner . raw_table ( ) . iter ( ) } ;
335
337
let ( _, _guard) = Ref :: into_raw ( this) ;
336
338
337
339
let iter = Iter { iter, _guard } ;
0 commit comments