1
1
use core:: cmp:: { Ordering , PartialOrd } ;
2
2
use core:: intrinsics;
3
3
use core:: mem:: MaybeUninit ;
4
- use core:: ptr;
5
4
6
5
use crate :: StaticVec ;
7
6
35
34
// function is called from, so these are safe hints to give to the
36
35
// optimizer.
37
36
unsafe {
38
- intrinsics:: assume ( !is_null_const ( src) ) ;
39
- intrinsics:: assume ( !is_null_mut ( dest) ) ;
37
+ intrinsics:: assume ( !src. is_null ( ) ) ;
38
+ // Curiously, the explicit typecast to `*mut T` on the next line
39
+ // is necessary to get it to compile. Without the typecast, `rustc` can't figure out
40
+ // what the type is supposed to be for some reason.
41
+ intrinsics:: assume ( !( dest as * mut T ) . is_null ( ) ) ;
40
42
}
41
43
while i > 0 {
42
44
unsafe {
@@ -103,7 +105,7 @@ pub(crate) fn quicksort_internal<T: Copy + PartialOrd>(
103
105
// We call this function from exactly one place where `low` and `high` are known to be within an
104
106
// appropriate range before getting passed into it, so there's no need to check them again here.
105
107
// We also know that `values` will never be null, so we can safely give an optimizer hint here.
106
- unsafe { intrinsics:: assume ( !is_null_mut ( values) ) } ;
108
+ unsafe { intrinsics:: assume ( !values. is_null ( ) ) } ;
107
109
loop {
108
110
let mut i = low;
109
111
let mut j = high;
@@ -164,65 +166,9 @@ pub(crate) union Repr<T> {
164
166
pub ( crate ) raw : FatPtr < T > ,
165
167
}
166
168
167
- /// A local `const fn` version of `ptr.is_null()`.
168
- #[ allow( clippy:: cmp_null) ]
169
- #[ inline( always) ]
170
- pub ( crate ) const fn is_null_const < T > ( p : * const T ) -> bool {
171
- // Same code as in the original.
172
- unsafe { ( p as * const u8 ) == ptr:: null ( ) }
173
- }
174
-
175
- /// A local `const fn` version of `ptr.is_null()`.
176
- #[ allow( clippy:: cmp_null) ]
177
- #[ inline( always) ]
178
- pub ( crate ) const fn is_null_mut < T > ( p : * mut T ) -> bool {
179
- // Same code as in the original.
180
- unsafe { ( p as * mut u8 ) == ptr:: null_mut ( ) }
181
- }
182
-
183
- /// A local `const fn` version of the (private) `core::intrinsics::is_aligned_and_not_null` utility
184
- /// function, recreated here for the sake of wanting to be able to do exactly the same debug
185
- /// assertions in the slice methods below.
186
- #[ inline( always) ]
187
- pub ( crate ) const fn is_aligned_and_not_null_const < T > ( _ptr : * const T ) -> bool {
188
- // Same code as in the original, just using our local `const` function to do the null check.
189
- // unsafe { !is_null_const(ptr) && ptr as usize % core::mem::align_of::<T>() == 0 }
190
-
191
- // Currently, the above code is not allowed in const contexts by the compiler even though we
192
- // have the appropriate feature flags set, so for the time being this function is a pass-through.
193
- //
194
- // IMO, this is perfectly justifiable as we only actually call the below slice methods internally
195
- // with pointers we already know are valid, and as such keeping the general-purpose debug
196
- // assertions from the original source at all is arguably completely unnecessary in the first
197
- // place.
198
- true
199
- }
200
-
201
- /// A `mut` version of the above. Of course, you can pass `mut` pointers to functions taking `const`
202
- /// ones, but what the heck, it feels more symmetrical this way.
203
- #[ inline( always) ]
204
- pub ( crate ) const fn is_aligned_and_not_null_mut < T > ( _ptr : * mut T ) -> bool {
205
- // Same code as in the original, just using our local `const` function to do the null check.
206
- // unsafe { !is_null_mut(ptr) && ptr as usize % core::mem::align_of::<T>() == 0 }
207
-
208
- // Currently, the above code is not allowed in const contexts by the compiler even though we
209
- // have the appropriate feature flags set, so for the time being this function is a pass-through.
210
- //
211
- // IMO, this is perfectly justifiable as we only actually call the below slice methods internally
212
- // with pointers we already know are valid, and as such keeping the general-purpose debug
213
- // assertions from the original source at all is arguably completely unnecessary in the first
214
- // place.
215
- true
216
- }
217
-
218
169
/// A local `const fn` version of `ptr::slice_from_raw_parts`.
219
170
#[ inline( always) ]
220
171
pub ( crate ) const fn ptr_slice_from_raw_parts < T > ( data : * const T , len : usize ) -> * const [ T ] {
221
- // Same code as in the original, just using our local `const` functions where necessary.
222
- debug_assert ! (
223
- is_aligned_and_not_null_const( data) ,
224
- "A null or unaligned pointer was passed to `staticvec::utils::ptr_slice_from_raw_parts`!"
225
- ) ;
226
172
debug_assert ! (
227
173
core:: mem:: size_of:: <T >( ) . saturating_mul( len) <= isize :: MAX as usize ,
228
174
"Attempted to create a slice covering at least half the address space!"
@@ -238,11 +184,6 @@ pub(crate) const fn ptr_slice_from_raw_parts<T>(data: *const T, len: usize) -> *
238
184
/// A local `const fn` version of `ptr::slice_from_raw_parts_mut`.
239
185
#[ inline( always) ]
240
186
pub ( crate ) const fn ptr_slice_from_raw_parts_mut < T > ( data : * mut T , len : usize ) -> * mut [ T ] {
241
- // Same code as in the original, just using our local `const` functions where necessary.
242
- debug_assert ! (
243
- is_aligned_and_not_null_mut( data) ,
244
- "A null or unaligned pointer was passed to `staticvec::utils::ptr_slice_from_raw_parts_mut`!"
245
- ) ;
246
187
debug_assert ! (
247
188
core:: mem:: size_of:: <T >( ) . saturating_mul( len) <= isize :: MAX as usize ,
248
189
"Attempted to create a slice covering at least half the address space!"
0 commit comments