@@ -1323,21 +1323,6 @@ impl<T: ?Sized> *const T {
1323
1323
#[ must_use]
1324
1324
#[ stable( feature = "align_offset" , since = "1.36.0" ) ]
1325
1325
#[ rustc_const_unstable( feature = "const_align_offset" , issue = "90962" ) ]
1326
- #[ cfg( not( bootstrap) ) ]
1327
- pub const fn align_offset ( self , align : usize ) -> usize
1328
- where
1329
- T : Sized ,
1330
- {
1331
- assert ! ( align. is_power_of_two( ) , "align_offset: align is not a power-of-two" ) ;
1332
-
1333
- // SAFETY: `align` has been checked to be a power of 2 above
1334
- unsafe { align_offset ( self , align) }
1335
- }
1336
-
1337
- #[ stable( feature = "align_offset" , since = "1.36.0" ) ]
1338
- #[ rustc_const_unstable( feature = "const_align_offset" , issue = "90962" ) ]
1339
- #[ allow( missing_docs) ]
1340
- #[ cfg( bootstrap) ]
1341
1326
pub const fn align_offset ( self , align : usize ) -> usize
1342
1327
where
1343
1328
T : Sized ,
@@ -1346,21 +1331,30 @@ impl<T: ?Sized> *const T {
1346
1331
panic ! ( "align_offset: align is not a power-of-two" ) ;
1347
1332
}
1348
1333
1349
- fn rt_impl < T > ( p : * const T , align : usize ) -> usize {
1350
- // SAFETY: `align` has been checked to be a power of 2 above
1351
- unsafe { align_offset ( p, align) }
1352
- }
1334
+ #[ cfg( bootstrap) ]
1335
+ {
1336
+ fn rt_impl < T > ( p : * const T , align : usize ) -> usize {
1337
+ // SAFETY: `align` has been checked to be a power of 2 above
1338
+ unsafe { align_offset ( p, align) }
1339
+ }
1353
1340
1354
- const fn ctfe_impl < T > ( _: * const T , _: usize ) -> usize {
1355
- usize:: MAX
1341
+ const fn ctfe_impl < T > ( _: * const T , _: usize ) -> usize {
1342
+ usize:: MAX
1343
+ }
1344
+
1345
+ // SAFETY:
1346
+ // It is permissible for `align_offset` to always return `usize::MAX`,
1347
+ // algorithm correctness can not depend on `align_offset` returning non-max values.
1348
+ //
1349
+ // As such the behaviour can't change after replacing `align_offset` with `usize::MAX`, only performance can.
1350
+ unsafe { intrinsics:: const_eval_select ( ( self , align) , ctfe_impl, rt_impl) }
1356
1351
}
1357
1352
1358
- // SAFETY:
1359
- // It is permissible for `align_offset` to always return `usize::MAX`,
1360
- // algorithm correctness can not depend on `align_offset` returning non-max values.
1361
- //
1362
- // As such the behaviour can't change after replacing `align_offset` with `usize::MAX`, only performance can.
1363
- unsafe { intrinsics:: const_eval_select ( ( self , align) , ctfe_impl, rt_impl) }
1353
+ #[ cfg( not( bootstrap) ) ]
1354
+ {
1355
+ // SAFETY: `align` has been checked to be a power of 2 above
1356
+ unsafe { align_offset ( self , align) }
1357
+ }
1364
1358
}
1365
1359
1366
1360
/// Returns whether the pointer is properly aligned for `T`.
@@ -1522,13 +1516,17 @@ impl<T: ?Sized> *const T {
1522
1516
#[ unstable( feature = "pointer_is_aligned" , issue = "96284" ) ]
1523
1517
#[ rustc_const_unstable( feature = "const_pointer_is_aligned" , issue = "none" ) ]
1524
1518
pub const fn is_aligned_to ( self , align : usize ) -> bool {
1525
- assert ! ( align. is_power_of_two( ) , "is_aligned_to: align is not a power-of-two" ) ;
1519
+ if !align. is_power_of_two ( ) {
1520
+ panic ! ( "is_aligned_to: align is not a power-of-two" )
1521
+ }
1526
1522
1527
1523
#[ inline]
1528
1524
fn runtime ( ptr : * const u8 , align : usize ) -> bool {
1529
1525
ptr. addr ( ) & ( align - 1 ) == 0
1530
1526
}
1531
1527
1528
+ // This optimizes to `(ptr + align - 1) & -align == ptr`, which is slightly
1529
+ // slower than `ptr & (align - 1) == 0`
1532
1530
const fn comptime ( ptr : * const u8 , align : usize ) -> bool {
1533
1531
ptr. align_offset ( align) == 0
1534
1532
}
0 commit comments