11use core:: cmp:: { Ordering , PartialOrd } ;
22use core:: intrinsics;
33use core:: mem:: MaybeUninit ;
4- use core:: ptr;
54
65use crate :: StaticVec ;
76
3534 // function is called from, so these are safe hints to give to the
3635 // optimizer.
3736 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 ( ) ) ;
4042 }
4143 while i > 0 {
4244 unsafe {
@@ -103,7 +105,7 @@ pub(crate) fn quicksort_internal<T: Copy + PartialOrd>(
103105 // We call this function from exactly one place where `low` and `high` are known to be within an
104106 // appropriate range before getting passed into it, so there's no need to check them again here.
105107 // 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 ( ) ) } ;
107109 loop {
108110 let mut i = low;
109111 let mut j = high;
@@ -164,65 +166,9 @@ pub(crate) union Repr<T> {
164166 pub ( crate ) raw : FatPtr < T > ,
165167}
166168
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-
218169/// A local `const fn` version of `ptr::slice_from_raw_parts`.
219170#[ inline( always) ]
220171pub ( 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- ) ;
226172 debug_assert ! (
227173 core:: mem:: size_of:: <T >( ) . saturating_mul( len) <= isize :: MAX as usize ,
228174 "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) -> *
238184/// A local `const fn` version of `ptr::slice_from_raw_parts_mut`.
239185#[ inline( always) ]
240186pub ( 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- ) ;
246187 debug_assert ! (
247188 core:: mem:: size_of:: <T >( ) . saturating_mul( len) <= isize :: MAX as usize ,
248189 "Attempted to create a slice covering at least half the address space!"
0 commit comments