@@ -51,7 +51,18 @@ pub fn with<T,R>(
51
51
#[ inline( always) ]
52
52
pub fn swap < T > ( x : & mut T , y : & mut T ) {
53
53
unsafe {
54
- swap_ptr ( ptr:: to_mut_unsafe_ptr ( x) , ptr:: to_mut_unsafe_ptr ( y) ) ;
54
+ // Give ourselves some scratch space to work with
55
+ let mut tmp: T = intrinsics:: uninit ( ) ;
56
+ let t: * mut T = & mut tmp;
57
+
58
+ // Perform the swap, `&mut` pointers never alias
59
+ ptr:: copy_nonoverlapping_memory ( t, x, 1 ) ;
60
+ ptr:: copy_nonoverlapping_memory ( x, y, 1 ) ;
61
+ ptr:: copy_nonoverlapping_memory ( y, t, 1 ) ;
62
+
63
+ // y and t now point to the same thing, but we need to completely forget `tmp`
64
+ // because it's no longer relevant.
65
+ cast:: forget ( tmp) ;
55
66
}
56
67
}
57
68
@@ -63,7 +74,7 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
63
74
pub unsafe fn swap_ptr < T > ( x : * mut T , y : * mut T ) {
64
75
// Give ourselves some scratch space to work with
65
76
let mut tmp: T = intrinsics:: uninit ( ) ;
66
- let t = ptr :: to_mut_unsafe_ptr ( & mut tmp) ;
77
+ let t: * mut T = & mut tmp;
67
78
68
79
// Perform the swap
69
80
ptr:: copy_memory ( t, x, 1 ) ;
0 commit comments