@@ -125,6 +125,16 @@ impl EntityRow {
125
125
Self ( index)
126
126
}
127
127
128
+ /// Equivalent to [`new`](Self::new) except that it takes a `u32` instead of a `NonMaxU32`.
129
+ ///
130
+ /// Returns `None` if the index is `u32::MAX`.
131
+ pub const fn from_raw_u32 ( index : u32 ) -> Option < Self > {
132
+ match NonMaxU32 :: new ( index) {
133
+ Some ( index) => Some ( Self ( index) ) ,
134
+ None => None ,
135
+ }
136
+ }
137
+
128
138
/// Gets the index of the entity.
129
139
#[ inline( always) ]
130
140
pub const fn index ( self ) -> u32 {
@@ -445,13 +455,9 @@ impl Hash for Entity {
445
455
}
446
456
447
457
impl Entity {
448
- /// Construct an [`Entity`] from a raw `row` value and a non-zero `generation` value.
449
- /// Ensure that the generation value is never greater than `0x7FFF_FFFF`.
458
+ /// Constructs an [`Entity`] from a raw `row` value and a non-zero `generation` value.
450
459
#[ inline( always) ]
451
- pub ( crate ) const fn from_raw_and_generation (
452
- row : EntityRow ,
453
- generation : EntityGeneration ,
454
- ) -> Entity {
460
+ pub const fn from_raw_and_generation ( row : EntityRow , generation : EntityGeneration ) -> Entity {
455
461
Self { row, generation }
456
462
}
457
463
@@ -1324,12 +1330,11 @@ mod tests {
1324
1330
1325
1331
#[ test]
1326
1332
fn entity_bits_roundtrip ( ) {
1327
- let r = EntityRow :: new ( NonMaxU32 :: new ( 0xDEADBEEF ) . unwrap ( ) ) ;
1333
+ let r = EntityRow :: from_raw_u32 ( 0xDEADBEEF ) . unwrap ( ) ;
1328
1334
assert_eq ! ( EntityRow :: from_bits( r. to_bits( ) ) , r) ;
1329
1335
1330
- // Generation cannot be greater than 0x7FFF_FFFF else it will be an invalid Entity id
1331
1336
let e = Entity :: from_raw_and_generation (
1332
- EntityRow :: new ( NonMaxU32 :: new ( 0xDEADBEEF ) . unwrap ( ) ) ,
1337
+ EntityRow :: from_raw_u32 ( 0xDEADBEEF ) . unwrap ( ) ,
1333
1338
EntityGeneration :: from_bits ( 0x5AADF00D ) ,
1334
1339
) ;
1335
1340
assert_eq ! ( Entity :: from_bits( e. to_bits( ) ) , e) ;
@@ -1368,15 +1373,15 @@ mod tests {
1368
1373
1369
1374
#[ test]
1370
1375
fn entity_const ( ) {
1371
- const C1 : Entity = Entity :: from_raw ( EntityRow :: new ( NonMaxU32 :: new ( 42 ) . unwrap ( ) ) ) ;
1376
+ const C1 : Entity = Entity :: from_raw ( EntityRow :: from_raw_u32 ( 42 ) . unwrap ( ) ) ;
1372
1377
assert_eq ! ( 42 , C1 . index( ) ) ;
1373
1378
assert_eq ! ( 0 , C1 . generation( ) . to_bits( ) ) ;
1374
1379
1375
1380
const C2 : Entity = Entity :: from_bits ( 0x0000_00ff_0000_00cc ) ;
1376
1381
assert_eq ! ( !0x0000_00cc , C2 . index( ) ) ;
1377
1382
assert_eq ! ( 0x0000_00ff , C2 . generation( ) . to_bits( ) ) ;
1378
1383
1379
- const C3 : u32 = Entity :: from_raw ( EntityRow :: new ( NonMaxU32 :: new ( 33 ) . unwrap ( ) ) ) . index ( ) ;
1384
+ const C3 : u32 = Entity :: from_raw ( EntityRow :: from_raw_u32 ( 33 ) . unwrap ( ) ) . index ( ) ;
1380
1385
assert_eq ! ( 33 , C3 ) ;
1381
1386
1382
1387
const C4 : u32 = Entity :: from_bits ( 0x00dd_00ff_1111_1111 )
@@ -1421,41 +1426,41 @@ mod tests {
1421
1426
fn entity_comparison ( ) {
1422
1427
assert_eq ! (
1423
1428
Entity :: from_raw_and_generation(
1424
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1429
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1425
1430
EntityGeneration :: from_bits( 456 )
1426
1431
) ,
1427
1432
Entity :: from_raw_and_generation(
1428
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1433
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1429
1434
EntityGeneration :: from_bits( 456 )
1430
1435
)
1431
1436
) ;
1432
1437
assert_ne ! (
1433
1438
Entity :: from_raw_and_generation(
1434
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1439
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1435
1440
EntityGeneration :: from_bits( 789 )
1436
1441
) ,
1437
1442
Entity :: from_raw_and_generation(
1438
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1443
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1439
1444
EntityGeneration :: from_bits( 456 )
1440
1445
)
1441
1446
) ;
1442
1447
assert_ne ! (
1443
1448
Entity :: from_raw_and_generation(
1444
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1449
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1445
1450
EntityGeneration :: from_bits( 456 )
1446
1451
) ,
1447
1452
Entity :: from_raw_and_generation(
1448
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1453
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1449
1454
EntityGeneration :: from_bits( 789 )
1450
1455
)
1451
1456
) ;
1452
1457
assert_ne ! (
1453
1458
Entity :: from_raw_and_generation(
1454
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1459
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1455
1460
EntityGeneration :: from_bits( 456 )
1456
1461
) ,
1457
1462
Entity :: from_raw_and_generation(
1458
- EntityRow :: new ( NonMaxU32 :: new ( 456 ) . unwrap( ) ) ,
1463
+ EntityRow :: from_raw_u32 ( 456 ) . unwrap( ) ,
1459
1464
EntityGeneration :: from_bits( 123 )
1460
1465
)
1461
1466
) ;
@@ -1464,93 +1469,93 @@ mod tests {
1464
1469
1465
1470
assert ! (
1466
1471
Entity :: from_raw_and_generation(
1467
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1472
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1468
1473
EntityGeneration :: from_bits( 456 )
1469
1474
) >= Entity :: from_raw_and_generation(
1470
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1475
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1471
1476
EntityGeneration :: from_bits( 456 )
1472
1477
)
1473
1478
) ;
1474
1479
assert ! (
1475
1480
Entity :: from_raw_and_generation(
1476
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1481
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1477
1482
EntityGeneration :: from_bits( 456 )
1478
1483
) <= Entity :: from_raw_and_generation(
1479
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1484
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1480
1485
EntityGeneration :: from_bits( 456 )
1481
1486
)
1482
1487
) ;
1483
1488
assert ! (
1484
1489
!( Entity :: from_raw_and_generation(
1485
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1490
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1486
1491
EntityGeneration :: from_bits( 456 )
1487
1492
) < Entity :: from_raw_and_generation(
1488
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1493
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1489
1494
EntityGeneration :: from_bits( 456 )
1490
1495
) )
1491
1496
) ;
1492
1497
assert ! (
1493
1498
!( Entity :: from_raw_and_generation(
1494
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1499
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1495
1500
EntityGeneration :: from_bits( 456 )
1496
1501
) > Entity :: from_raw_and_generation(
1497
- EntityRow :: new ( NonMaxU32 :: new ( 123 ) . unwrap( ) ) ,
1502
+ EntityRow :: from_raw_u32 ( 123 ) . unwrap( ) ,
1498
1503
EntityGeneration :: from_bits( 456 )
1499
1504
) )
1500
1505
) ;
1501
1506
1502
1507
assert ! (
1503
1508
Entity :: from_raw_and_generation(
1504
- EntityRow :: new ( NonMaxU32 :: new ( 9 ) . unwrap( ) ) ,
1509
+ EntityRow :: from_raw_u32 ( 9 ) . unwrap( ) ,
1505
1510
EntityGeneration :: from_bits( 1 )
1506
1511
) < Entity :: from_raw_and_generation(
1507
- EntityRow :: new ( NonMaxU32 :: new ( 1 ) . unwrap( ) ) ,
1512
+ EntityRow :: from_raw_u32 ( 1 ) . unwrap( ) ,
1508
1513
EntityGeneration :: from_bits( 9 )
1509
1514
)
1510
1515
) ;
1511
1516
assert ! (
1512
1517
Entity :: from_raw_and_generation(
1513
- EntityRow :: new ( NonMaxU32 :: new ( 1 ) . unwrap( ) ) ,
1518
+ EntityRow :: from_raw_u32 ( 1 ) . unwrap( ) ,
1514
1519
EntityGeneration :: from_bits( 9 )
1515
1520
) > Entity :: from_raw_and_generation(
1516
- EntityRow :: new ( NonMaxU32 :: new ( 9 ) . unwrap( ) ) ,
1521
+ EntityRow :: from_raw_u32 ( 9 ) . unwrap( ) ,
1517
1522
EntityGeneration :: from_bits( 1 )
1518
1523
)
1519
1524
) ;
1520
1525
1521
1526
assert ! (
1522
1527
Entity :: from_raw_and_generation(
1523
- EntityRow :: new ( NonMaxU32 :: new ( 1 ) . unwrap( ) ) ,
1528
+ EntityRow :: from_raw_u32 ( 1 ) . unwrap( ) ,
1524
1529
EntityGeneration :: from_bits( 1 )
1525
1530
) > Entity :: from_raw_and_generation(
1526
- EntityRow :: new ( NonMaxU32 :: new ( 2 ) . unwrap( ) ) ,
1531
+ EntityRow :: from_raw_u32 ( 2 ) . unwrap( ) ,
1527
1532
EntityGeneration :: from_bits( 1 )
1528
1533
)
1529
1534
) ;
1530
1535
assert ! (
1531
1536
Entity :: from_raw_and_generation(
1532
- EntityRow :: new ( NonMaxU32 :: new ( 1 ) . unwrap( ) ) ,
1537
+ EntityRow :: from_raw_u32 ( 1 ) . unwrap( ) ,
1533
1538
EntityGeneration :: from_bits( 1 )
1534
1539
) >= Entity :: from_raw_and_generation(
1535
- EntityRow :: new ( NonMaxU32 :: new ( 2 ) . unwrap( ) ) ,
1540
+ EntityRow :: from_raw_u32 ( 2 ) . unwrap( ) ,
1536
1541
EntityGeneration :: from_bits( 1 )
1537
1542
)
1538
1543
) ;
1539
1544
assert ! (
1540
1545
Entity :: from_raw_and_generation(
1541
- EntityRow :: new ( NonMaxU32 :: new ( 2 ) . unwrap( ) ) ,
1546
+ EntityRow :: from_raw_u32 ( 2 ) . unwrap( ) ,
1542
1547
EntityGeneration :: from_bits( 2 )
1543
1548
) < Entity :: from_raw_and_generation(
1544
- EntityRow :: new ( NonMaxU32 :: new ( 1 ) . unwrap( ) ) ,
1549
+ EntityRow :: from_raw_u32 ( 1 ) . unwrap( ) ,
1545
1550
EntityGeneration :: from_bits( 2 )
1546
1551
)
1547
1552
) ;
1548
1553
assert ! (
1549
1554
Entity :: from_raw_and_generation(
1550
- EntityRow :: new ( NonMaxU32 :: new ( 2 ) . unwrap( ) ) ,
1555
+ EntityRow :: from_raw_u32 ( 2 ) . unwrap( ) ,
1551
1556
EntityGeneration :: from_bits( 2 )
1552
1557
) <= Entity :: from_raw_and_generation(
1553
- EntityRow :: new ( NonMaxU32 :: new ( 1 ) . unwrap( ) ) ,
1558
+ EntityRow :: from_raw_u32 ( 1 ) . unwrap( ) ,
1554
1559
EntityGeneration :: from_bits( 2 )
1555
1560
)
1556
1561
) ;
@@ -1564,15 +1569,12 @@ mod tests {
1564
1569
let hash = EntityHash ;
1565
1570
1566
1571
let first_id = 0xC0FFEE << 8 ;
1567
- let first_hash = hash. hash_one ( Entity :: from_raw ( EntityRow :: new (
1568
- NonMaxU32 :: new ( first_id) . unwrap ( ) ,
1569
- ) ) ) ;
1572
+ let first_hash =
1573
+ hash. hash_one ( Entity :: from_raw ( EntityRow :: from_raw_u32 ( first_id) . unwrap ( ) ) ) ;
1570
1574
1571
1575
for i in 1 ..=255 {
1572
1576
let id = first_id + i;
1573
- let hash = hash. hash_one ( Entity :: from_raw ( EntityRow :: new (
1574
- NonMaxU32 :: new ( id) . unwrap ( ) ,
1575
- ) ) ) ;
1577
+ let hash = hash. hash_one ( Entity :: from_raw ( EntityRow :: from_raw_u32 ( id) . unwrap ( ) ) ) ;
1576
1578
assert_eq ! ( first_hash. wrapping_sub( hash) as u32 , i) ;
1577
1579
}
1578
1580
}
@@ -1584,15 +1586,12 @@ mod tests {
1584
1586
let hash = EntityHash ;
1585
1587
1586
1588
let first_id = 0xC0FFEE ;
1587
- let first_hash = hash. hash_one ( Entity :: from_raw ( EntityRow :: new (
1588
- NonMaxU32 :: new ( first_id) . unwrap ( ) ,
1589
- ) ) ) >> 57 ;
1589
+ let first_hash =
1590
+ hash. hash_one ( Entity :: from_raw ( EntityRow :: from_raw_u32 ( first_id) . unwrap ( ) ) ) >> 57 ;
1590
1591
1591
1592
for bit in 0 ..u32:: BITS {
1592
1593
let id = first_id ^ ( 1 << bit) ;
1593
- let hash = hash. hash_one ( Entity :: from_raw ( EntityRow :: new (
1594
- NonMaxU32 :: new ( id) . unwrap ( ) ,
1595
- ) ) ) >> 57 ;
1594
+ let hash = hash. hash_one ( Entity :: from_raw ( EntityRow :: from_raw_u32 ( id) . unwrap ( ) ) ) >> 57 ;
1596
1595
assert_ne ! ( hash, first_hash) ;
1597
1596
}
1598
1597
}
@@ -1617,7 +1616,7 @@ mod tests {
1617
1616
1618
1617
#[ test]
1619
1618
fn entity_debug ( ) {
1620
- let entity = Entity :: from_raw ( EntityRow :: new ( NonMaxU32 :: new ( 42 ) . unwrap ( ) ) ) ;
1619
+ let entity = Entity :: from_raw ( EntityRow :: from_raw_u32 ( 42 ) . unwrap ( ) ) ;
1621
1620
let string = format ! ( "{entity:?}" ) ;
1622
1621
assert_eq ! ( string, "42v0" ) ;
1623
1622
@@ -1628,7 +1627,7 @@ mod tests {
1628
1627
1629
1628
#[ test]
1630
1629
fn entity_display ( ) {
1631
- let entity = Entity :: from_raw ( EntityRow :: new ( NonMaxU32 :: new ( 42 ) . unwrap ( ) ) ) ;
1630
+ let entity = Entity :: from_raw ( EntityRow :: from_raw_u32 ( 42 ) . unwrap ( ) ) ;
1632
1631
let string = format ! ( "{entity}" ) ;
1633
1632
assert_eq ! ( string, "42v0" ) ;
1634
1633
0 commit comments