@@ -146,6 +146,32 @@ where
146
146
}
147
147
}
148
148
149
+ impl < P > Box < P , Init >
150
+ where
151
+ P : Pool ,
152
+ {
153
+ /// Forgets the contents of this memory block without running its destructor.
154
+ ///
155
+ /// Note that this this does not return the memory block to the pool. The
156
+ /// block can be reused, or returned to the pool by dropping it.
157
+ pub fn forget ( self ) -> Box < P , Uninit > {
158
+ let node = self . inner . node ;
159
+
160
+ mem:: forget ( self ) ;
161
+ mem:: forget ( unsafe {
162
+ ptr:: read ( node. as_ref ( ) . data . get ( ) )
163
+ } ) ;
164
+
165
+ Box {
166
+ inner : super :: Box {
167
+ node,
168
+ _state : PhantomData ,
169
+ } ,
170
+ _pool : PhantomData ,
171
+ }
172
+ }
173
+ }
174
+
149
175
impl < P > Deref for Box < P >
150
176
where
151
177
P : Pool ,
@@ -348,17 +374,23 @@ mod tests {
348
374
349
375
let x = A :: alloc ( ) . unwrap ( ) . init ( X :: new ( ) ) ;
350
376
let y = A :: alloc ( ) . unwrap ( ) . init ( X :: new ( ) ) ;
377
+ let z = A :: alloc ( ) . unwrap ( ) . init ( X :: new ( ) ) ;
351
378
352
- assert_eq ! ( COUNT . load( Ordering :: Relaxed ) , 2 ) ;
379
+ assert_eq ! ( COUNT . load( Ordering :: Relaxed ) , 3 ) ;
353
380
354
381
// this runs `X`'s destructor
355
382
drop ( x) ;
356
383
357
- assert_eq ! ( COUNT . load( Ordering :: Relaxed ) , 1 ) ;
384
+ assert_eq ! ( COUNT . load( Ordering :: Relaxed ) , 2 ) ;
358
385
359
386
// this leaks memory
360
387
mem:: forget ( y) ;
361
388
362
- assert_eq ! ( COUNT . load( Ordering :: Relaxed ) , 1 ) ;
389
+ assert_eq ! ( COUNT . load( Ordering :: Relaxed ) , 2 ) ;
390
+
391
+ // this forgets `X` without leaking memory
392
+ z. forget ( ) ;
393
+
394
+ assert_eq ! ( COUNT . load( Ordering :: Relaxed ) , 2 ) ;
363
395
}
364
396
}
0 commit comments