@@ -1351,14 +1351,15 @@ pub trait UnwrapThrowExt<T>: Sized {
1351
1351
) ) {
1352
1352
let loc = core:: panic:: Location :: caller ( ) ;
1353
1353
let msg = alloc:: format!(
1354
- "`unwrap_throw` failed ({}:{}:{})" ,
1354
+ "called `{}::unwrap_throw()` ({}:{}:{})" ,
1355
+ core:: any:: type_name:: <Self >( ) ,
1355
1356
loc. file( ) ,
1356
1357
loc. line( ) ,
1357
1358
loc. column( )
1358
1359
) ;
1359
1360
self . expect_throw ( & msg)
1360
1361
} else {
1361
- self . expect_throw ( "`unwrap_throw` failed " )
1362
+ self . expect_throw ( "called `unwrap_throw()` " )
1362
1363
}
1363
1364
}
1364
1365
@@ -1376,11 +1377,43 @@ pub trait UnwrapThrowExt<T>: Sized {
1376
1377
}
1377
1378
1378
1379
impl < T > UnwrapThrowExt < T > for Option < T > {
1380
+ fn unwrap_throw ( self ) -> T {
1381
+ const MSG : & str = "called `Option::unwrap_throw()` on a `None` value" ;
1382
+
1383
+ if cfg ! ( all( target_arch = "wasm32" , target_os = "unknown" ) ) {
1384
+ if let Some ( val) = self {
1385
+ val
1386
+ } else if cfg ! ( debug_assertions) {
1387
+ let loc = core:: panic:: Location :: caller ( ) ;
1388
+ let msg =
1389
+ alloc:: format!( "{} ({}:{}:{})" , MSG , loc. file( ) , loc. line( ) , loc. column( ) , ) ;
1390
+
1391
+ throw_str ( & msg)
1392
+ } else {
1393
+ throw_str ( MSG )
1394
+ }
1395
+ } else {
1396
+ self . expect ( MSG )
1397
+ }
1398
+ }
1399
+
1379
1400
fn expect_throw ( self , message : & str ) -> T {
1380
1401
if cfg ! ( all( target_arch = "wasm32" , target_os = "unknown" ) ) {
1381
- match self {
1382
- Some ( val) => val,
1383
- None => throw_str ( message) ,
1402
+ if let Some ( val) = self {
1403
+ val
1404
+ } else if cfg ! ( debug_assertions) {
1405
+ let loc = core:: panic:: Location :: caller ( ) ;
1406
+ let msg = alloc:: format!(
1407
+ "{} ({}:{}:{})" ,
1408
+ message,
1409
+ loc. file( ) ,
1410
+ loc. line( ) ,
1411
+ loc. column( ) ,
1412
+ ) ;
1413
+
1414
+ throw_str ( & msg)
1415
+ } else {
1416
+ throw_str ( message)
1384
1417
}
1385
1418
} else {
1386
1419
self . expect ( message)
@@ -1393,36 +1426,55 @@ where
1393
1426
E : core:: fmt:: Debug ,
1394
1427
{
1395
1428
fn unwrap_throw ( self ) -> T {
1396
- if cfg ! ( all(
1397
- debug_assertions,
1398
- target_arch = "wasm32" ,
1399
- target_os = "unknown"
1400
- ) ) {
1429
+ const MSG : & str = "called `Result::unwrap_throw()` on an `Err` value" ;
1430
+
1431
+ if cfg ! ( all( target_arch = "wasm32" , target_os = "unknown" ) ) {
1401
1432
match self {
1402
1433
Ok ( val) => val,
1403
1434
Err ( err) => {
1404
- let loc = core:: panic:: Location :: caller ( ) ;
1405
- let msg = alloc:: format!(
1406
- "`unwrap_throw` failed ({}:{}:{}): {:?}" ,
1407
- loc. file( ) ,
1408
- loc. line( ) ,
1409
- loc. column( ) ,
1410
- err
1411
- ) ;
1412
-
1413
- throw_str ( & msg)
1435
+ if cfg ! ( debug_assertions) {
1436
+ let loc = core:: panic:: Location :: caller ( ) ;
1437
+ let msg = alloc:: format!(
1438
+ "{} ({}:{}:{}): {:?}" ,
1439
+ MSG ,
1440
+ loc. file( ) ,
1441
+ loc. line( ) ,
1442
+ loc. column( ) ,
1443
+ err
1444
+ ) ;
1445
+
1446
+ throw_str ( & msg)
1447
+ } else {
1448
+ throw_str ( MSG )
1449
+ }
1414
1450
}
1415
1451
}
1416
1452
} else {
1417
- self . expect ( "`unwrap_throw` failed" )
1453
+ self . expect ( MSG )
1418
1454
}
1419
1455
}
1420
1456
1421
1457
fn expect_throw ( self , message : & str ) -> T {
1422
1458
if cfg ! ( all( target_arch = "wasm32" , target_os = "unknown" ) ) {
1423
1459
match self {
1424
1460
Ok ( val) => val,
1425
- Err ( _) => throw_str ( message) ,
1461
+ Err ( err) => {
1462
+ if cfg ! ( debug_assertions) {
1463
+ let loc = core:: panic:: Location :: caller ( ) ;
1464
+ let msg = alloc:: format!(
1465
+ "{} ({}:{}:{}): {:?}" ,
1466
+ message,
1467
+ loc. file( ) ,
1468
+ loc. line( ) ,
1469
+ loc. column( ) ,
1470
+ err
1471
+ ) ;
1472
+
1473
+ throw_str ( & msg)
1474
+ } else {
1475
+ throw_str ( message)
1476
+ }
1477
+ }
1426
1478
}
1427
1479
} else {
1428
1480
self . expect ( message)
0 commit comments