@@ -1323,21 +1323,6 @@ impl<T: ?Sized> *const T {
13231323 #[ must_use]
13241324 #[ stable( feature = "align_offset" , since = "1.36.0" ) ]
13251325 #[ 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) ]
13411326 pub const fn align_offset ( self , align : usize ) -> usize
13421327 where
13431328 T : Sized ,
@@ -1346,21 +1331,30 @@ impl<T: ?Sized> *const T {
13461331 panic ! ( "align_offset: align is not a power-of-two" ) ;
13471332 }
13481333
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+ }
13531340
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) }
13561351 }
13571352
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+ }
13641358 }
13651359
13661360 /// Returns whether the pointer is properly aligned for `T`.
@@ -1522,13 +1516,17 @@ impl<T: ?Sized> *const T {
15221516 #[ unstable( feature = "pointer_is_aligned" , issue = "96284" ) ]
15231517 #[ rustc_const_unstable( feature = "const_pointer_is_aligned" , issue = "none" ) ]
15241518 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+ }
15261522
15271523 #[ inline]
15281524 fn runtime ( ptr : * const u8 , align : usize ) -> bool {
15291525 ptr. addr ( ) & ( align - 1 ) == 0
15301526 }
15311527
1528+ // This optimizes to `(ptr + align - 1) & -align == ptr`, which is slightly
1529+ // slower than `ptr & (align - 1) == 0`
15321530 const fn comptime ( ptr : * const u8 , align : usize ) -> bool {
15331531 ptr. align_offset ( align) == 0
15341532 }
0 commit comments