@@ -17,8 +17,8 @@ nonpoison_and_poison_unwrap_test!(
17
17
use locks:: Mutex ;
18
18
19
19
let m = Mutex :: new( ( ) ) ;
20
- drop( maybe_unwrap! ( m. lock( ) ) ) ;
21
- drop( maybe_unwrap! ( m. lock( ) ) ) ;
20
+ drop( maybe_unwrap( m. lock( ) ) ) ;
21
+ drop( maybe_unwrap( m. lock( ) ) ) ;
22
22
}
23
23
) ;
24
24
@@ -34,7 +34,7 @@ nonpoison_and_poison_unwrap_test!(
34
34
35
35
fn inc( m: & Mutex <u32 >) {
36
36
for _ in 0 ..J {
37
- * maybe_unwrap! ( m. lock( ) ) += 1 ;
37
+ * maybe_unwrap( m. lock( ) ) += 1 ;
38
38
}
39
39
}
40
40
@@ -58,7 +58,7 @@ nonpoison_and_poison_unwrap_test!(
58
58
for _ in 0 ..2 * K {
59
59
rx. recv( ) . unwrap( ) ;
60
60
}
61
- assert_eq!( * maybe_unwrap! ( m. lock( ) ) , J * K * 2 ) ;
61
+ assert_eq!( * maybe_unwrap( m. lock( ) ) , J * K * 2 ) ;
62
62
}
63
63
) ;
64
64
@@ -96,7 +96,7 @@ nonpoison_and_poison_unwrap_test!(
96
96
use locks:: Mutex ;
97
97
98
98
let m = Mutex :: new( NonCopy ( 10 ) ) ;
99
- assert_eq!( maybe_unwrap! ( m. into_inner( ) ) , NonCopy ( 10 ) ) ;
99
+ assert_eq!( maybe_unwrap( m. into_inner( ) ) , NonCopy ( 10 ) ) ;
100
100
}
101
101
) ;
102
102
@@ -116,7 +116,7 @@ nonpoison_and_poison_unwrap_test!(
116
116
let m = Mutex :: new( Foo ( num_drops. clone( ) ) ) ;
117
117
assert_eq!( num_drops. load( Ordering :: SeqCst ) , 0 ) ;
118
118
{
119
- let _inner = maybe_unwrap! ( m. into_inner( ) ) ;
119
+ let _inner = maybe_unwrap( m. into_inner( ) ) ;
120
120
assert_eq!( num_drops. load( Ordering :: SeqCst ) , 0 ) ;
121
121
}
122
122
assert_eq!( num_drops. load( Ordering :: SeqCst ) , 1 ) ;
@@ -129,8 +129,8 @@ nonpoison_and_poison_unwrap_test!(
129
129
use locks:: Mutex ;
130
130
131
131
let mut m = Mutex :: new( NonCopy ( 10 ) ) ;
132
- * maybe_unwrap! ( m. get_mut( ) ) = NonCopy ( 20 ) ;
133
- assert_eq!( maybe_unwrap! ( m. into_inner( ) ) , NonCopy ( 20 ) ) ;
132
+ * maybe_unwrap( m. get_mut( ) ) = NonCopy ( 20 ) ;
133
+ assert_eq!( maybe_unwrap( m. into_inner( ) ) , NonCopy ( 20 ) ) ;
134
134
}
135
135
) ;
136
136
@@ -144,7 +144,7 @@ nonpoison_and_poison_unwrap_test!(
144
144
145
145
let m = Mutex :: new( Cloneable ( 10 ) ) ;
146
146
147
- assert_eq!( maybe_unwrap! ( m. get_cloned( ) ) , Cloneable ( 10 ) ) ;
147
+ assert_eq!( maybe_unwrap( m. get_cloned( ) ) , Cloneable ( 10 ) ) ;
148
148
}
149
149
) ;
150
150
@@ -159,16 +159,17 @@ nonpoison_and_poison_unwrap_test!(
159
159
{
160
160
let m = Mutex :: new( init( ) ) ;
161
161
162
- assert_eq!( * maybe_unwrap! ( m. lock( ) ) , init( ) ) ;
163
- maybe_unwrap! ( m. set( value( ) ) ) ;
164
- assert_eq!( * maybe_unwrap! ( m. lock( ) ) , value( ) ) ;
162
+ assert_eq!( * maybe_unwrap( m. lock( ) ) , init( ) ) ;
163
+ maybe_unwrap( m. set( value( ) ) ) ;
164
+ assert_eq!( * maybe_unwrap( m. lock( ) ) , value( ) ) ;
165
165
}
166
166
167
167
inner( || NonCopy ( 10 ) , || NonCopy ( 20 ) ) ;
168
168
inner( || NonCopyNeedsDrop ( 10 ) , || NonCopyNeedsDrop ( 20 ) ) ;
169
169
}
170
170
) ;
171
171
172
+ // Ensure that old values that are replaced by `set` are correctly dropped.
172
173
nonpoison_and_poison_unwrap_test ! (
173
174
name: test_set_drop,
174
175
test_body: {
@@ -186,7 +187,7 @@ nonpoison_and_poison_unwrap_test!(
186
187
assert_eq!( num_drops. load( Ordering :: SeqCst ) , 0 ) ;
187
188
188
189
let different = Foo ( Arc :: new( AtomicUsize :: new( 42 ) ) ) ;
189
- maybe_unwrap! ( m. set( different) ) ;
190
+ maybe_unwrap( m. set( different) ) ;
190
191
assert_eq!( num_drops. load( Ordering :: SeqCst ) , 1 ) ;
191
192
}
192
193
) ;
@@ -202,9 +203,9 @@ nonpoison_and_poison_unwrap_test!(
202
203
{
203
204
let m = Mutex :: new( init( ) ) ;
204
205
205
- assert_eq!( * maybe_unwrap! ( m. lock( ) ) , init( ) ) ;
206
- assert_eq!( maybe_unwrap! ( m. replace( value( ) ) ) , init( ) ) ;
207
- assert_eq!( * maybe_unwrap! ( m. lock( ) ) , value( ) ) ;
206
+ assert_eq!( * maybe_unwrap( m. lock( ) ) , init( ) ) ;
207
+ assert_eq!( maybe_unwrap( m. replace( value( ) ) ) , init( ) ) ;
208
+ assert_eq!( * maybe_unwrap( m. lock( ) ) , value( ) ) ;
208
209
}
209
210
210
211
inner( || NonCopy ( 10 ) , || NonCopy ( 20 ) ) ;
@@ -257,8 +258,8 @@ nonpoison_and_poison_unwrap_test!(
257
258
let arc2 = Arc :: new( Mutex :: new( arc) ) ;
258
259
let ( tx, rx) = channel( ) ;
259
260
let _t = thread:: spawn( move || {
260
- let lock = maybe_unwrap! ( arc2. lock( ) ) ;
261
- let lock2 = maybe_unwrap! ( lock. lock( ) ) ;
261
+ let lock = maybe_unwrap( arc2. lock( ) ) ;
262
+ let lock2 = maybe_unwrap( lock. lock( ) ) ;
262
263
assert_eq!( * lock2, 1 ) ;
263
264
tx. send( ( ) ) . unwrap( ) ;
264
265
} ) ;
@@ -273,12 +274,12 @@ nonpoison_and_poison_unwrap_test!(
273
274
274
275
let mutex: & Mutex <[ i32 ] > = & Mutex :: new( [ 1 , 2 , 3 ] ) ;
275
276
{
276
- let b = & mut * maybe_unwrap! ( mutex. lock( ) ) ;
277
+ let b = & mut * maybe_unwrap( mutex. lock( ) ) ;
277
278
b[ 0 ] = 4 ;
278
279
b[ 2 ] = 5 ;
279
280
}
280
281
let comp: & [ i32 ] = & [ 4 , 2 , 5 ] ;
281
- assert_eq!( & * maybe_unwrap! ( mutex. lock( ) ) , comp) ;
282
+ assert_eq!( & * maybe_unwrap( mutex. lock( ) ) , comp) ;
282
283
}
283
284
) ;
284
285
@@ -289,16 +290,18 @@ nonpoison_and_poison_unwrap_test!(
289
290
290
291
let arr = [ 0 ; 4 ] ;
291
292
let lock = Mutex :: new( arr) ;
292
- let guard = maybe_unwrap! ( lock. lock( ) ) ;
293
+ let guard = maybe_unwrap( lock. lock( ) ) ;
293
294
let guard = MutexGuard :: map( guard, |arr| & mut arr[ ..2 ] ) ;
294
295
let mut guard = MappedMutexGuard :: map( guard, |slice| & mut slice[ 1 ..] ) ;
295
296
assert_eq!( guard. len( ) , 1 ) ;
296
297
guard[ 0 ] = 42 ;
297
298
drop( guard) ;
298
- assert_eq!( * maybe_unwrap! ( lock. lock( ) ) , [ 0 , 42 , 0 , 0 ] ) ;
299
+ assert_eq!( * maybe_unwrap( lock. lock( ) ) , [ 0 , 42 , 0 , 0 ] ) ;
299
300
}
300
301
) ;
301
302
303
+ // Ensures that both mutex types are able to be locked even after threads that hold the guards
304
+ // panic. This should be true even for the `nonpoison::Mutex`.
302
305
#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
303
306
nonpoison_and_poison_unwrap_test ! (
304
307
name: test_panics,
@@ -308,21 +311,21 @@ nonpoison_and_poison_unwrap_test!(
308
311
let mutex = Mutex :: new( 42 ) ;
309
312
310
313
let catch_unwind_result1 = panic:: catch_unwind( AssertUnwindSafe ( || {
311
- let _guard1 = maybe_unwrap! ( mutex. lock( ) ) ;
314
+ let _guard1 = maybe_unwrap( mutex. lock( ) ) ;
312
315
313
316
panic!( "test panic with mutex once" ) ;
314
317
} ) ) ;
315
318
assert!( catch_unwind_result1. is_err( ) ) ;
316
319
317
320
let catch_unwind_result2 = panic:: catch_unwind( AssertUnwindSafe ( || {
318
- let _guard2 = maybe_unwrap! ( mutex. lock( ) ) ;
321
+ let _guard2 = maybe_unwrap( mutex. lock( ) ) ;
319
322
320
323
panic!( "test panic with mutex twice" ) ;
321
324
} ) ) ;
322
325
assert!( catch_unwind_result2. is_err( ) ) ;
323
326
324
327
let catch_unwind_result3 = panic:: catch_unwind( AssertUnwindSafe ( || {
325
- let _guard3 = maybe_unwrap! ( mutex. lock( ) ) ;
328
+ let _guard3 = maybe_unwrap( mutex. lock( ) ) ;
326
329
327
330
panic!( "test panic with mutex thrice" ) ;
328
331
} ) ) ;
@@ -344,14 +347,14 @@ nonpoison_and_poison_unwrap_test!(
344
347
}
345
348
impl Drop for Unwinder {
346
349
fn drop( & mut self ) {
347
- * maybe_unwrap! ( self . i. lock( ) ) += 1 ;
350
+ * maybe_unwrap( self . i. lock( ) ) += 1 ;
348
351
}
349
352
}
350
353
let _u = Unwinder { i: arc2 } ;
351
354
panic!( ) ;
352
355
} )
353
356
. join( ) ;
354
- let lock = maybe_unwrap! ( arc. lock( ) ) ;
357
+ let lock = maybe_unwrap( arc. lock( ) ) ;
355
358
assert_eq!( * lock, 2 ) ;
356
359
}
357
360
) ;
0 commit comments