15
15
* limitations under the License.
16
16
*/
17
17
18
-
19
18
use std:: {
20
19
mem:: MaybeUninit ,
21
20
ops:: { Deref , DerefMut } ,
@@ -115,7 +114,7 @@ impl<T: SlabPool> Slab<T> {
115
114
} ) )
116
115
}
117
116
118
- pub fn new ( new_object : T :: SlabType ) -> Option < BoxSlab < T > > {
117
+ pub fn try_new ( new_object : T :: SlabType ) -> Option < BoxSlab < T > > {
119
118
let slab = T :: get_slab ( ) ;
120
119
let mut inner = slab. 0 . lock ( ) . unwrap ( ) ;
121
120
if let Some ( index) = inner. map . first_false_index ( ) {
@@ -180,40 +179,37 @@ mod tests {
180
179
#[ test]
181
180
fn simple_alloc_free ( ) {
182
181
{
183
- let a = Slab :: < TestSlab > :: new ( Test { val : Arc :: new ( 10 ) } ) . unwrap ( ) ;
182
+ let a = Slab :: < TestSlab > :: try_new ( Test { val : Arc :: new ( 10 ) } ) . unwrap ( ) ;
184
183
assert_eq ! ( * a. val. deref( ) , 10 ) ;
185
184
let inner = TestSlab :: get_slab ( ) . 0 . lock ( ) . unwrap ( ) ;
186
- assert_eq ! ( inner. map. is_empty( ) , false ) ;
185
+ assert ! ( ! inner. map. is_empty( ) ) ;
187
186
}
188
187
// Validates that the 'Drop' got executed
189
188
let inner = TestSlab :: get_slab ( ) . 0 . lock ( ) . unwrap ( ) ;
190
- assert_eq ! ( inner. map. is_empty( ) , true ) ;
189
+ assert ! ( inner. map. is_empty( ) ) ;
191
190
println ! ( "Box Size {}" , std:: mem:: size_of:: <Box <Test >>( ) ) ;
192
191
println ! ( "BoxSlab Size {}" , std:: mem:: size_of:: <BoxSlab <TestSlab >>( ) ) ;
193
192
}
194
193
195
194
#[ test]
196
195
fn alloc_full_block ( ) {
197
196
{
198
- let a = Slab :: < TestSlab > :: new ( Test { val : Arc :: new ( 10 ) } ) . unwrap ( ) ;
199
- let b = Slab :: < TestSlab > :: new ( Test { val : Arc :: new ( 11 ) } ) . unwrap ( ) ;
200
- let c = Slab :: < TestSlab > :: new ( Test { val : Arc :: new ( 12 ) } ) . unwrap ( ) ;
197
+ let a = Slab :: < TestSlab > :: try_new ( Test { val : Arc :: new ( 10 ) } ) . unwrap ( ) ;
198
+ let b = Slab :: < TestSlab > :: try_new ( Test { val : Arc :: new ( 11 ) } ) . unwrap ( ) ;
199
+ let c = Slab :: < TestSlab > :: try_new ( Test { val : Arc :: new ( 12 ) } ) . unwrap ( ) ;
201
200
// Test that at overflow, we return None
202
- assert_eq ! (
203
- Slab :: <TestSlab >:: new( Test { val: Arc :: new( 13 ) } ) . is_none( ) ,
204
- true
205
- ) ;
201
+ assert ! ( Slab :: <TestSlab >:: try_new( Test { val: Arc :: new( 13 ) } ) . is_none( ) , ) ;
206
202
assert_eq ! ( * b. val. deref( ) , 11 ) ;
207
203
208
204
{
209
205
let inner = TestSlab :: get_slab ( ) . 0 . lock ( ) . unwrap ( ) ;
210
206
// Test that the bitmap is marked as full
211
- assert_eq ! ( inner. map. is_full( ) , true ) ;
207
+ assert ! ( inner. map. is_full( ) ) ;
212
208
}
213
209
214
210
// Purposefully drop, to test that new allocation is possible
215
211
std:: mem:: drop ( b) ;
216
- let d = Slab :: < TestSlab > :: new ( Test { val : Arc :: new ( 21 ) } ) . unwrap ( ) ;
212
+ let d = Slab :: < TestSlab > :: try_new ( Test { val : Arc :: new ( 21 ) } ) . unwrap ( ) ;
217
213
assert_eq ! ( * d. val. deref( ) , 21 ) ;
218
214
219
215
// Ensure older allocations are still valid
@@ -223,16 +219,16 @@ mod tests {
223
219
224
220
// Validates that the 'Drop' got executed - test that the bitmap is empty
225
221
let inner = TestSlab :: get_slab ( ) . 0 . lock ( ) . unwrap ( ) ;
226
- assert_eq ! ( inner. map. is_empty( ) , true ) ;
222
+ assert ! ( inner. map. is_empty( ) ) ;
227
223
}
228
224
229
225
#[ test]
230
226
fn test_drop_logic ( ) {
231
227
let root = Arc :: new ( 10 ) ;
232
228
{
233
- let _a = Slab :: < TestSlab > :: new ( Test { val : root. clone ( ) } ) . unwrap ( ) ;
234
- let _b = Slab :: < TestSlab > :: new ( Test { val : root. clone ( ) } ) . unwrap ( ) ;
235
- let _c = Slab :: < TestSlab > :: new ( Test { val : root. clone ( ) } ) . unwrap ( ) ;
229
+ let _a = Slab :: < TestSlab > :: try_new ( Test { val : root. clone ( ) } ) . unwrap ( ) ;
230
+ let _b = Slab :: < TestSlab > :: try_new ( Test { val : root. clone ( ) } ) . unwrap ( ) ;
231
+ let _c = Slab :: < TestSlab > :: try_new ( Test { val : root. clone ( ) } ) . unwrap ( ) ;
236
232
assert_eq ! ( Arc :: strong_count( & root) , 4 ) ;
237
233
}
238
234
// Test that Drop was correctly called on all the members of the pool
0 commit comments