@@ -14,6 +14,7 @@ use cast;
14
14
use option:: { Option , Some , None } ;
15
15
use sys;
16
16
use unstable:: intrinsics;
17
+ use util:: swap;
17
18
18
19
#[ cfg( not( test) ) ] use cmp:: { Eq , Ord } ;
19
20
use uint;
@@ -177,9 +178,9 @@ pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
177
178
let t: * mut T = & mut tmp;
178
179
179
180
// Perform the swap
180
- copy_memory ( t, x, 1 ) ;
181
- copy_memory ( x, y, 1 ) ;
182
- copy_memory ( y, t, 1 ) ;
181
+ copy_nonoverlapping_memory ( t, x, 1 ) ;
182
+ copy_memory ( x, y, 1 ) ; // `x` and `y` may overlap
183
+ copy_nonoverlapping_memory ( y, t, 1 ) ;
183
184
184
185
// y and t now point to the same thing, but we need to completely forget `tmp`
185
186
// because it's no longer relevant.
@@ -192,7 +193,7 @@ pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
192
193
*/
193
194
#[ inline]
194
195
pub unsafe fn replace_ptr < T > ( dest : * mut T , mut src : T ) -> T {
195
- swap_ptr ( dest, & mut src) ;
196
+ swap ( cast :: transmute ( dest) , & mut src) ; // cannot overlap
196
197
src
197
198
}
198
199
@@ -202,8 +203,7 @@ pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
202
203
#[ inline( always) ]
203
204
pub unsafe fn read_ptr < T > ( src : * mut T ) -> T {
204
205
let mut tmp: T = intrinsics:: uninit ( ) ;
205
- let t: * mut T = & mut tmp;
206
- copy_memory ( t, src, 1 ) ;
206
+ copy_nonoverlapping_memory ( & mut tmp, src, 1 ) ;
207
207
tmp
208
208
}
209
209
0 commit comments