File tree 2 files changed +36
-6
lines changed
2 files changed +36
-6
lines changed Original file line number Diff line number Diff line change @@ -879,15 +879,30 @@ impl<T: ?Sized> *const T {
879
879
/// # } }
880
880
/// ```
881
881
#[ stable( feature = "align_offset" , since = "1.36.0" ) ]
882
- pub fn align_offset ( self , align : usize ) -> usize
882
+ #[ rustc_const_unstable( feature = "const_align_offset" , issue = "90962" ) ]
883
+ pub const fn align_offset ( self , align : usize ) -> usize
883
884
where
884
885
T : Sized ,
885
886
{
886
887
if !align. is_power_of_two ( ) {
887
888
panic ! ( "align_offset: align is not a power-of-two" ) ;
888
889
}
889
- // SAFETY: `align` has been checked to be a power of 2 above
890
- unsafe { align_offset ( self , align) }
890
+
891
+ fn rt_impl < T > ( p : * const T , align : usize ) -> usize {
892
+ // SAFETY: `align` has been checked to be a power of 2 above
893
+ unsafe { align_offset ( p, align) }
894
+ }
895
+
896
+ const fn ctfe_impl < T > ( _: * const T , _: usize ) -> usize {
897
+ usize:: MAX
898
+ }
899
+
900
+ // SAFETY:
901
+ // It is permisseble for `align_offset` to always return `usize::MAX`,
902
+ // algorithm correctness can not depend on `align_offset` returning non-max values.
903
+ //
904
+ // As such the behaviour can't change after replacing `align_offset` with `usize::MAX`, only performance can.
905
+ unsafe { intrinsics:: const_eval_select ( ( self , align) , ctfe_impl, rt_impl) }
891
906
}
892
907
}
893
908
Original file line number Diff line number Diff line change @@ -1142,15 +1142,30 @@ impl<T: ?Sized> *mut T {
1142
1142
/// # } }
1143
1143
/// ```
1144
1144
#[ stable( feature = "align_offset" , since = "1.36.0" ) ]
1145
- pub fn align_offset ( self , align : usize ) -> usize
1145
+ #[ rustc_const_unstable( feature = "const_align_offset" , issue = "90962" ) ]
1146
+ pub const fn align_offset ( self , align : usize ) -> usize
1146
1147
where
1147
1148
T : Sized ,
1148
1149
{
1149
1150
if !align. is_power_of_two ( ) {
1150
1151
panic ! ( "align_offset: align is not a power-of-two" ) ;
1151
1152
}
1152
- // SAFETY: `align` has been checked to be a power of 2 above
1153
- unsafe { align_offset ( self , align) }
1153
+
1154
+ fn rt_impl < T > ( p : * mut T , align : usize ) -> usize {
1155
+ // SAFETY: `align` has been checked to be a power of 2 above
1156
+ unsafe { align_offset ( p, align) }
1157
+ }
1158
+
1159
+ const fn ctfe_impl < T > ( _: * mut T , _: usize ) -> usize {
1160
+ usize:: MAX
1161
+ }
1162
+
1163
+ // SAFETY:
1164
+ // It is permisseble for `align_offset` to always return `usize::MAX`,
1165
+ // algorithm correctness can not depend on `align_offset` returning non-max values.
1166
+ //
1167
+ // As such the behaviour can't change after replacing `align_offset` with `usize::MAX`, only performance can.
1168
+ unsafe { intrinsics:: const_eval_select ( ( self , align) , ctfe_impl, rt_impl) }
1154
1169
}
1155
1170
}
1156
1171
You can’t perform that action at this time.
0 commit comments