@@ -1312,6 +1312,34 @@ macro_rules! int_impl {
1312
1312
}
1313
1313
}
1314
1314
1315
+ /// Unbounded shift left. Computes `self << rhs`, without bounding the value of `rhs`
1316
+ ///
1317
+ /// If `rhs` is larger or equal to the number of bits in `self`,
1318
+ /// the entire value is shifted out, and `0` is returned.
1319
+ ///
1320
+ /// # Examples
1321
+ ///
1322
+ /// Basic usage:
1323
+ /// ```
1324
+ /// #![feature(unbounded_shifts)]
1325
+ #[ doc = concat!( "assert_eq!(0x1" , stringify!( $SelfT) , ".unbounded_shl(4), 0x10);" ) ]
1326
+ #[ doc = concat!( "assert_eq!(0x1" , stringify!( $SelfT) , ".unbounded_shl(129), 0);" ) ]
1327
+ /// ```
1328
+ #[ unstable( feature = "unbounded_shifts" , issue = "129375" ) ]
1329
+ #[ rustc_const_unstable( feature = "const_unbounded_shifts" , issue = "129375" ) ]
1330
+ #[ must_use = "this returns the result of the operation, \
1331
+ without modifying the original"]
1332
+ #[ inline]
1333
+ pub const fn unbounded_shl( self , rhs: u32 ) -> $SelfT{
1334
+ if rhs < Self :: BITS {
1335
+ // SAFETY:
1336
+ // rhs is just checked to be in-range above
1337
+ unsafe { self . unchecked_shl( rhs) }
1338
+ } else {
1339
+ 0
1340
+ }
1341
+ }
1342
+
1315
1343
/// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
1316
1344
/// larger than or equal to the number of bits in `self`.
1317
1345
///
@@ -1410,6 +1438,40 @@ macro_rules! int_impl {
1410
1438
}
1411
1439
}
1412
1440
1441
+ /// Unbounded shift right. Computes `self >> rhs`, without bounding the value of `rhs`
1442
+ ///
1443
+ /// If `rhs` is larger or equal to the number of bits in `self`,
1444
+ /// the entire value is shifted out, which yields `0` for a positive number,
1445
+ /// and `-1` for a negative number.
1446
+ ///
1447
+ /// # Examples
1448
+ ///
1449
+ /// Basic usage:
1450
+ /// ```
1451
+ /// #![feature(unbounded_shifts)]
1452
+ #[ doc = concat!( "assert_eq!(0x10" , stringify!( $SelfT) , ".unbounded_shr(4), 0x1);" ) ]
1453
+ #[ doc = concat!( "assert_eq!(0x10" , stringify!( $SelfT) , ".unbounded_shr(129), 0);" ) ]
1454
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MIN.unbounded_shr(129), -1);" ) ]
1455
+ /// ```
1456
+ #[ unstable( feature = "unbounded_shifts" , issue = "129375" ) ]
1457
+ #[ rustc_const_unstable( feature = "const_unbounded_shifts" , issue = "129375" ) ]
1458
+ #[ must_use = "this returns the result of the operation, \
1459
+ without modifying the original"]
1460
+ #[ inline]
1461
+ pub const fn unbounded_shr( self , rhs: u32 ) -> $SelfT{
1462
+ if rhs < Self :: BITS {
1463
+ // SAFETY:
1464
+ // rhs is just checked to be in-range above
1465
+ unsafe { self . unchecked_shr( rhs) }
1466
+ } else {
1467
+ // A shift by `Self::BITS-1` suffices for signed integers, because the sign bit is copied for each of the shifted bits.
1468
+
1469
+ // SAFETY:
1470
+ // `Self::BITS-1` is guaranteed to be less than `Self::BITS`
1471
+ unsafe { self . unchecked_shr( Self :: BITS - 1 ) }
1472
+ }
1473
+ }
1474
+
1413
1475
/// Checked absolute value. Computes `self.abs()`, returning `None` if
1414
1476
/// `self == MIN`.
1415
1477
///
0 commit comments