@@ -5,13 +5,13 @@ use core::mem::{size_of, MaybeUninit};
5
5
use crate :: StaticVec ;
6
6
7
7
#[ inline( always) ]
8
- pub ( crate ) fn runtime_zst_handler < T > ( dest : * const T , origin : * const T ) -> usize {
8
+ pub ( crate ) fn runtime_distance_between < T > ( dest : * const T , origin : * const T ) -> usize {
9
9
// At runtime we can handle ZSTs with usize casts, as is done throughout the crate.
10
10
( dest as usize ) . wrapping_sub ( origin as usize )
11
11
}
12
12
13
13
#[ inline( always) ]
14
- pub ( crate ) const fn compiletime_zst_handler < T > ( _dest : * const T , _origin : * const T ) -> usize {
14
+ pub ( crate ) const fn compiletime_distance_between < T > ( _dest : * const T , _origin : * const T ) -> usize {
15
15
assert ! (
16
16
size_of:: <T >( ) == 0 ,
17
17
"`compiletime_zst_handler` called on a non-ZST!"
@@ -28,7 +28,13 @@ pub(crate) const fn distance_between<T>(dest: *const T, origin: *const T) -> usi
28
28
match size_of :: < T > ( ) {
29
29
// This function cannot safely be used on ZST pointers at compile time (which would not be
30
30
// useful in any scenario I can currently think of anyways).
31
- 0 => unsafe { const_eval_select ( ( dest, origin) , compiletime_zst_handler, runtime_zst_handler) } ,
31
+ 0 => unsafe {
32
+ const_eval_select (
33
+ ( dest, origin) ,
34
+ compiletime_distance_between,
35
+ runtime_distance_between,
36
+ )
37
+ } ,
32
38
// For all other sizes of `T` though, `offset_from` works just fine.
33
39
_ => unsafe { dest. offset_from ( origin) as usize } ,
34
40
}
@@ -102,11 +108,31 @@ where T: Copy {
102
108
}
103
109
}
104
110
111
+ #[ inline( always) ]
112
+ pub ( crate ) const fn compiletime_zst_ptr_add < T > ( ptr : * const T , count : usize ) -> * const T {
113
+ ( ptr as * const u8 ) . wrapping_add ( count) as * const T
114
+ }
115
+
116
+ #[ inline( always) ]
117
+ pub ( crate ) const fn compiletime_zst_ptr_add_mut < T > ( ptr : * mut T , count : usize ) -> * mut T {
118
+ ( ptr as * mut u8 ) . wrapping_add ( count) as * mut T
119
+ }
120
+
121
+ #[ inline( always) ]
122
+ pub ( crate ) fn runtime_zst_ptr_add < T > ( ptr : * const T , count : usize ) -> * const T {
123
+ ( ptr as usize + count) as * const T
124
+ }
125
+
126
+ #[ inline( always) ]
127
+ pub ( crate ) fn runtime_zst_ptr_add_mut < T > ( ptr : * mut T , count : usize ) -> * mut T {
128
+ ( ptr as usize + count) as * mut T
129
+ }
130
+
105
131
/// An internal convenience function for incrementing immutable ZST pointers by usize offsets.
106
132
#[ inline( always) ]
107
133
pub ( crate ) const fn zst_ptr_add < T > ( ptr : * const T , count : usize ) -> * const T {
108
134
debug_assert ! ( size_of:: <T >( ) == 0 , "`zst_ptr_add` called on a non-ZST!" ) ;
109
- ( ptr as * const u8 ) . wrapping_add ( count) as * const T
135
+ unsafe { const_eval_select ( ( ptr , count) , compiletime_zst_ptr_add , runtime_zst_ptr_add ) }
110
136
}
111
137
112
138
/// An internal convenience function for incrementing mutable ZST pointers by usize offsets.
@@ -116,7 +142,13 @@ pub(crate) const fn zst_ptr_add_mut<T>(ptr: *mut T, count: usize) -> *mut T {
116
142
size_of:: <T >( ) == 0 ,
117
143
"`zst_ptr_add_mut` called on a non-ZST!"
118
144
) ;
119
- ( ptr as * mut u8 ) . wrapping_add ( count) as * mut T
145
+ unsafe {
146
+ const_eval_select (
147
+ ( ptr, count) ,
148
+ compiletime_zst_ptr_add_mut,
149
+ runtime_zst_ptr_add_mut,
150
+ )
151
+ }
120
152
}
121
153
122
154
/// A version of the default `partial_cmp` implementation with a more flexible function signature.
0 commit comments