47
47
48
48
& self . partitions [ ( hasher. finish ( ) as usize ) & self . partition_mask ]
49
49
}
50
+
51
+ #[ cfg( test) ]
52
+ fn get_partition_by_index ( & self , index : usize ) -> & RwLock < T > {
53
+ & self . partitions [ index]
54
+ }
50
55
}
51
56
52
57
/// Simple partitioned `Mutex`
79
84
fn get_partition < K : Eq + Hash > ( & self , key : & K ) -> & Mutex < T > {
80
85
let mut hasher = AHasher :: default ( ) ;
81
86
key. hash ( & mut hasher) ;
82
-
83
87
& self . partitions [ ( hasher. finish ( ) as usize ) & self . partition_mask ]
84
88
}
89
+
90
+ #[ cfg( test) ]
91
+ fn get_partition_by_index ( & self , index : usize ) -> & Mutex < T > {
92
+ & self . partitions [ index]
93
+ }
85
94
}
86
95
87
96
#[ cfg( test) ]
@@ -128,13 +137,12 @@ mod tests {
128
137
fn test_partitioned_mutex_vis_different_partition ( ) {
129
138
let tmp_vec: Vec < f32 > = Vec :: new ( ) ;
130
139
let test_locked_map = PartitionedMutex :: new ( tmp_vec, 4 ) ;
131
- let test_key_first = "test_key_first" . to_string ( ) ;
132
- let mutex_first = test_locked_map . get_partition ( & test_key_first ) ;
140
+ let mutex_first = test_locked_map . get_partition_by_index ( 0 ) ;
141
+
133
142
let mut _tmp_data = mutex_first. lock ( ) . unwrap ( ) ;
134
143
assert ! ( mutex_first. try_lock( ) . is_err( ) ) ;
135
144
136
- let test_key_second = "test_key_second" . to_string ( ) ;
137
- let mutex_second = test_locked_map. get_partition ( & test_key_second) ;
145
+ let mutex_second = test_locked_map. get_partition_by_index ( 1 ) ;
138
146
assert ! ( mutex_second. try_lock( ) . is_ok( ) ) ;
139
147
assert ! ( mutex_first. try_lock( ) . is_err( ) ) ;
140
148
}
@@ -143,13 +151,11 @@ mod tests {
143
151
fn test_partitioned_rwmutex_vis_different_partition ( ) {
144
152
let tmp_vec: Vec < f32 > = Vec :: new ( ) ;
145
153
let test_locked_map = PartitionedRwLock :: new ( tmp_vec, 4 ) ;
146
- let test_key_first = "test_key_first" . to_string ( ) ;
147
- let mutex_first = test_locked_map. get_partition ( & test_key_first) ;
154
+ let mutex_first = test_locked_map. get_partition_by_index ( 0 ) ;
148
155
let mut _tmp = mutex_first. write ( ) . unwrap ( ) ;
149
156
assert ! ( mutex_first. try_write( ) . is_err( ) ) ;
150
157
151
- let test_key_second = "test_key_second" . to_string ( ) ;
152
- let mutex_second_try_lock = test_locked_map. get_partition ( & test_key_second) ;
158
+ let mutex_second_try_lock = test_locked_map. get_partition_by_index ( 1 ) ;
153
159
assert ! ( mutex_second_try_lock. try_write( ) . is_ok( ) ) ;
154
160
assert ! ( mutex_first. try_write( ) . is_err( ) ) ;
155
161
}
0 commit comments