Skip to content

Commit acd48a2

Browse files
committed
std: Standardize (input, output) param orderings
This functions swaps the order of arguments to a few functions that previously took (output, input) parameters, but now take (input, output) parameters (in that order). The affected functions are: * ptr::copy * ptr::copy_nonoverlapping * slice::bytes::copy_memory * intrinsics::copy * intrinsics::copy_nonoverlapping Closes rust-lang#22890 [breaking-change]
1 parent 14192d6 commit acd48a2

File tree

29 files changed

+147
-107
lines changed

29 files changed

+147
-107
lines changed

src/liballoc/heap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ mod imp {
301301
libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
302302
} else {
303303
let new_ptr = allocate(size, align);
304-
ptr::copy(new_ptr, ptr, cmp::min(size, old_size));
304+
ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
305305
deallocate(ptr, old_size, align);
306306
new_ptr
307307
}

src/libcollections/btree/node.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1133,13 +1133,13 @@ impl<K, V> Node<K, V> {
11331133
#[inline]
11341134
unsafe fn insert_kv(&mut self, index: usize, key: K, val: V) -> &mut V {
11351135
ptr::copy(
1136-
self.keys_mut().as_mut_ptr().offset(index as isize + 1),
11371136
self.keys().as_ptr().offset(index as isize),
1137+
self.keys_mut().as_mut_ptr().offset(index as isize + 1),
11381138
self.len() - index
11391139
);
11401140
ptr::copy(
1141-
self.vals_mut().as_mut_ptr().offset(index as isize + 1),
11421141
self.vals().as_ptr().offset(index as isize),
1142+
self.vals_mut().as_mut_ptr().offset(index as isize + 1),
11431143
self.len() - index
11441144
);
11451145

@@ -1155,8 +1155,8 @@ impl<K, V> Node<K, V> {
11551155
#[inline]
11561156
unsafe fn insert_edge(&mut self, index: usize, edge: Node<K, V>) {
11571157
ptr::copy(
1158-
self.edges_mut().as_mut_ptr().offset(index as isize + 1),
11591158
self.edges().as_ptr().offset(index as isize),
1159+
self.edges_mut().as_mut_ptr().offset(index as isize + 1),
11601160
self.len() - index
11611161
);
11621162
ptr::write(self.edges_mut().get_unchecked_mut(index), edge);
@@ -1188,13 +1188,13 @@ impl<K, V> Node<K, V> {
11881188
let val = ptr::read(self.vals().get_unchecked(index));
11891189

11901190
ptr::copy(
1191-
self.keys_mut().as_mut_ptr().offset(index as isize),
11921191
self.keys().as_ptr().offset(index as isize + 1),
1192+
self.keys_mut().as_mut_ptr().offset(index as isize),
11931193
self.len() - index - 1
11941194
);
11951195
ptr::copy(
1196-
self.vals_mut().as_mut_ptr().offset(index as isize),
11971196
self.vals().as_ptr().offset(index as isize + 1),
1197+
self.vals_mut().as_mut_ptr().offset(index as isize),
11981198
self.len() - index - 1
11991199
);
12001200

@@ -1209,8 +1209,8 @@ impl<K, V> Node<K, V> {
12091209
let edge = ptr::read(self.edges().get_unchecked(index));
12101210

12111211
ptr::copy(
1212-
self.edges_mut().as_mut_ptr().offset(index as isize),
12131212
self.edges().as_ptr().offset(index as isize + 1),
1213+
self.edges_mut().as_mut_ptr().offset(index as isize),
12141214
// index can be == len+1, so do the +1 first to avoid underflow.
12151215
(self.len() + 1) - index
12161216
);
@@ -1237,19 +1237,19 @@ impl<K, V> Node<K, V> {
12371237
right._len = self.len() / 2;
12381238
let right_offset = self.len() - right.len();
12391239
ptr::copy_nonoverlapping(
1240-
right.keys_mut().as_mut_ptr(),
12411240
self.keys().as_ptr().offset(right_offset as isize),
1241+
right.keys_mut().as_mut_ptr(),
12421242
right.len()
12431243
);
12441244
ptr::copy_nonoverlapping(
1245-
right.vals_mut().as_mut_ptr(),
12461245
self.vals().as_ptr().offset(right_offset as isize),
1246+
right.vals_mut().as_mut_ptr(),
12471247
right.len()
12481248
);
12491249
if !self.is_leaf() {
12501250
ptr::copy_nonoverlapping(
1251-
right.edges_mut().as_mut_ptr(),
12521251
self.edges().as_ptr().offset(right_offset as isize),
1252+
right.edges_mut().as_mut_ptr(),
12531253
right.len() + 1
12541254
);
12551255
}
@@ -1278,19 +1278,19 @@ impl<K, V> Node<K, V> {
12781278
ptr::write(self.vals_mut().get_unchecked_mut(old_len), val);
12791279

12801280
ptr::copy_nonoverlapping(
1281-
self.keys_mut().as_mut_ptr().offset(old_len as isize + 1),
12821281
right.keys().as_ptr(),
1282+
self.keys_mut().as_mut_ptr().offset(old_len as isize + 1),
12831283
right.len()
12841284
);
12851285
ptr::copy_nonoverlapping(
1286-
self.vals_mut().as_mut_ptr().offset(old_len as isize + 1),
12871286
right.vals().as_ptr(),
1287+
self.vals_mut().as_mut_ptr().offset(old_len as isize + 1),
12881288
right.len()
12891289
);
12901290
if !self.is_leaf() {
12911291
ptr::copy_nonoverlapping(
1292-
self.edges_mut().as_mut_ptr().offset(old_len as isize + 1),
12931292
right.edges().as_ptr(),
1293+
self.edges_mut().as_mut_ptr().offset(old_len as isize + 1),
12941294
right.len() + 1
12951295
);
12961296
}

src/libcollections/slice.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1320,10 +1320,10 @@ fn insertion_sort<T, F>(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> O
13201320

13211321
if i != j {
13221322
let tmp = ptr::read(read_ptr);
1323-
ptr::copy(buf_v.offset(j + 1),
1324-
&*buf_v.offset(j),
1323+
ptr::copy(&*buf_v.offset(j),
1324+
buf_v.offset(j + 1),
13251325
(i - j) as usize);
1326-
ptr::copy_nonoverlapping(buf_v.offset(j), &tmp, 1);
1326+
ptr::copy_nonoverlapping(&tmp, buf_v.offset(j), 1);
13271327
mem::forget(tmp);
13281328
}
13291329
}
@@ -1396,10 +1396,10 @@ fn merge_sort<T, F>(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Order
13961396
// j + 1 could be `len` (for the last `i`), but in
13971397
// that case, `i == j` so we don't copy. The
13981398
// `.offset(j)` is always in bounds.
1399-
ptr::copy(buf_dat.offset(j + 1),
1400-
&*buf_dat.offset(j),
1399+
ptr::copy(&*buf_dat.offset(j),
1400+
buf_dat.offset(j + 1),
14011401
i - j as usize);
1402-
ptr::copy_nonoverlapping(buf_dat.offset(j), read_ptr, 1);
1402+
ptr::copy_nonoverlapping(read_ptr, buf_dat.offset(j), 1);
14031403
}
14041404
}
14051405
}
@@ -1447,11 +1447,11 @@ fn merge_sort<T, F>(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Order
14471447
if left == right_start {
14481448
// the number remaining in this run.
14491449
let elems = (right_end as usize - right as usize) / mem::size_of::<T>();
1450-
ptr::copy_nonoverlapping(out, &*right, elems);
1450+
ptr::copy_nonoverlapping(&*right, out, elems);
14511451
break;
14521452
} else if right == right_end {
14531453
let elems = (right_start as usize - left as usize) / mem::size_of::<T>();
1454-
ptr::copy_nonoverlapping(out, &*left, elems);
1454+
ptr::copy_nonoverlapping(&*left, out, elems);
14551455
break;
14561456
}
14571457

@@ -1465,7 +1465,7 @@ fn merge_sort<T, F>(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Order
14651465
} else {
14661466
step(&mut left)
14671467
};
1468-
ptr::copy_nonoverlapping(out, &*to_copy, 1);
1468+
ptr::copy_nonoverlapping(&*to_copy, out, 1);
14691469
step(&mut out);
14701470
}
14711471
}
@@ -1479,7 +1479,7 @@ fn merge_sort<T, F>(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Order
14791479
// write the result to `v` in one go, so that there are never two copies
14801480
// of the same object in `v`.
14811481
unsafe {
1482-
ptr::copy_nonoverlapping(v.as_mut_ptr(), &*buf_dat, len);
1482+
ptr::copy_nonoverlapping(&*buf_dat, v.as_mut_ptr(), len);
14831483
}
14841484

14851485
// increment the pointer, returning the old pointer.

src/libcollections/string.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,8 @@ impl String {
592592
let ch = self.char_at(idx);
593593
let next = idx + ch.len_utf8();
594594
unsafe {
595-
ptr::copy(self.vec.as_mut_ptr().offset(idx as isize),
596-
self.vec.as_ptr().offset(next as isize),
595+
ptr::copy(self.vec.as_ptr().offset(next as isize),
596+
self.vec.as_mut_ptr().offset(idx as isize),
597597
len - next);
598598
self.vec.set_len(len - (next - idx));
599599
}
@@ -622,11 +622,11 @@ impl String {
622622
let amt = ch.encode_utf8(&mut bits).unwrap();
623623

624624
unsafe {
625-
ptr::copy(self.vec.as_mut_ptr().offset((idx + amt) as isize),
626-
self.vec.as_ptr().offset(idx as isize),
625+
ptr::copy(self.vec.as_ptr().offset(idx as isize),
626+
self.vec.as_mut_ptr().offset((idx + amt) as isize),
627627
len - idx);
628-
ptr::copy(self.vec.as_mut_ptr().offset(idx as isize),
629-
bits.as_ptr(),
628+
ptr::copy(bits.as_ptr(),
629+
self.vec.as_mut_ptr().offset(idx as isize),
630630
amt);
631631
self.vec.set_len(len + amt);
632632
}

src/libcollections/vec.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,17 @@ impl<T> Vec<T> {
260260

261261
/// Creates a vector by copying the elements from a raw pointer.
262262
///
263-
/// This function will copy `elts` contiguous elements starting at `ptr` into a new allocation
264-
/// owned by the returned `Vec<T>`. The elements of the buffer are copied into the vector
265-
/// without cloning, as if `ptr::read()` were called on them.
263+
/// This function will copy `elts` contiguous elements starting at `ptr`
264+
/// into a new allocation owned by the returned `Vec<T>`. The elements of
265+
/// the buffer are copied into the vector without cloning, as if
266+
/// `ptr::read()` were called on them.
266267
#[inline]
267268
#[unstable(feature = "collections",
268269
reason = "may be better expressed via composition")]
269270
pub unsafe fn from_raw_buf(ptr: *const T, elts: usize) -> Vec<T> {
270271
let mut dst = Vec::with_capacity(elts);
271272
dst.set_len(elts);
272-
ptr::copy_nonoverlapping(dst.as_mut_ptr(), ptr, elts);
273+
ptr::copy_nonoverlapping(ptr, dst.as_mut_ptr(), elts);
273274
dst
274275
}
275276

@@ -288,8 +289,9 @@ impl<T> Vec<T> {
288289
self.cap
289290
}
290291

291-
/// Reserves capacity for at least `additional` more elements to be inserted in the given
292-
/// `Vec<T>`. The collection may reserve more space to avoid frequent reallocations.
292+
/// Reserves capacity for at least `additional` more elements to be inserted
293+
/// in the given `Vec<T>`. The collection may reserve more space to avoid
294+
/// frequent reallocations.
293295
///
294296
/// # Panics
295297
///
@@ -541,7 +543,7 @@ impl<T> Vec<T> {
541543
let p = self.as_mut_ptr().offset(index as isize);
542544
// Shift everything over to make space. (Duplicating the
543545
// `index`th element into two consecutive places.)
544-
ptr::copy(p.offset(1), &*p, len - index);
546+
ptr::copy(&*p, p.offset(1), len - index);
545547
// Write it in, overwriting the first copy of the `index`th
546548
// element.
547549
ptr::write(&mut *p, element);
@@ -579,7 +581,7 @@ impl<T> Vec<T> {
579581
ret = ptr::read(ptr);
580582

581583
// Shift everything down to fill in that spot.
582-
ptr::copy(ptr, &*ptr.offset(1), len - index - 1);
584+
ptr::copy(&*ptr.offset(1), ptr, len - index - 1);
583585
}
584586
self.set_len(len - 1);
585587
ret
@@ -721,8 +723,8 @@ impl<T> Vec<T> {
721723
let len = self.len();
722724
unsafe {
723725
ptr::copy_nonoverlapping(
724-
self.get_unchecked_mut(len),
725726
other.as_ptr(),
727+
self.get_unchecked_mut(len),
726728
other.len());
727729
}
728730

@@ -1042,8 +1044,8 @@ impl<T> Vec<T> {
10421044
other.set_len(other_len);
10431045

10441046
ptr::copy_nonoverlapping(
1045-
other.as_mut_ptr(),
10461047
self.as_ptr().offset(at as isize),
1048+
other.as_mut_ptr(),
10471049
other.len());
10481050
}
10491051
other

src/libcollections/vec_deque.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ impl<T> VecDeque<T> {
142142
debug_assert!(src + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len,
143143
self.cap);
144144
ptr::copy(
145-
self.ptr.offset(dst as isize),
146145
self.ptr.offset(src as isize),
146+
self.ptr.offset(dst as isize),
147147
len);
148148
}
149149

@@ -155,8 +155,8 @@ impl<T> VecDeque<T> {
155155
debug_assert!(src + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len,
156156
self.cap);
157157
ptr::copy_nonoverlapping(
158-
self.ptr.offset(dst as isize),
159158
self.ptr.offset(src as isize),
159+
self.ptr.offset(dst as isize),
160160
len);
161161
}
162162
}
@@ -1361,21 +1361,21 @@ impl<T> VecDeque<T> {
13611361
// `at` lies in the first half.
13621362
let amount_in_first = first_len - at;
13631363

1364-
ptr::copy_nonoverlapping(*other.ptr,
1365-
first_half.as_ptr().offset(at as isize),
1364+
ptr::copy_nonoverlapping(first_half.as_ptr().offset(at as isize),
1365+
*other.ptr,
13661366
amount_in_first);
13671367

13681368
// just take all of the second half.
1369-
ptr::copy_nonoverlapping(other.ptr.offset(amount_in_first as isize),
1370-
second_half.as_ptr(),
1369+
ptr::copy_nonoverlapping(second_half.as_ptr(),
1370+
other.ptr.offset(amount_in_first as isize),
13711371
second_len);
13721372
} else {
13731373
// `at` lies in the second half, need to factor in the elements we skipped
13741374
// in the first half.
13751375
let offset = at - first_len;
13761376
let amount_in_second = second_len - offset;
1377-
ptr::copy_nonoverlapping(*other.ptr,
1378-
second_half.as_ptr().offset(offset as isize),
1377+
ptr::copy_nonoverlapping(second_half.as_ptr().offset(offset as isize),
1378+
*other.ptr,
13791379
amount_in_second);
13801380
}
13811381
}

src/libcore/fmt/float.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
316316

317317
impl<'a> fmt::Write for Filler<'a> {
318318
fn write_str(&mut self, s: &str) -> fmt::Result {
319-
slice::bytes::copy_memory(&mut self.buf[(*self.end)..],
320-
s.as_bytes());
319+
slice::bytes::copy_memory(s.as_bytes(),
320+
&mut self.buf[(*self.end)..]);
321321
*self.end += s.len();
322322
Ok(())
323323
}

src/libcore/intrinsics.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@ extern "rust-intrinsic" {
293293
/// let mut t: T = mem::uninitialized();
294294
///
295295
/// // Perform the swap, `&mut` pointers never alias
296-
/// ptr::copy_nonoverlapping(&mut t, &*x, 1);
297-
/// ptr::copy_nonoverlapping(x, &*y, 1);
298-
/// ptr::copy_nonoverlapping(y, &t, 1);
296+
/// ptr::copy_nonoverlapping(x, &mut t, 1);
297+
/// ptr::copy_nonoverlapping(y, x, 1);
298+
/// ptr::copy_nonoverlapping(&t, y, 1);
299299
///
300300
/// // y and t now point to the same thing, but we need to completely forget `tmp`
301301
/// // because it's no longer relevant.
@@ -304,6 +304,12 @@ extern "rust-intrinsic" {
304304
/// }
305305
/// ```
306306
#[stable(feature = "rust1", since = "1.0.0")]
307+
#[cfg(not(stage0))]
308+
pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
309+
310+
/// dox
311+
#[stable(feature = "rust1", since = "1.0.0")]
312+
#[cfg(stage0)]
307313
pub fn copy_nonoverlapping<T>(dst: *mut T, src: *const T, count: usize);
308314

309315
/// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
@@ -329,12 +335,18 @@ extern "rust-intrinsic" {
329335
/// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
330336
/// let mut dst = Vec::with_capacity(elts);
331337
/// dst.set_len(elts);
332-
/// ptr::copy(dst.as_mut_ptr(), ptr, elts);
338+
/// ptr::copy(ptr, dst.as_mut_ptr(), elts);
333339
/// dst
334340
/// }
335341
/// ```
336342
///
337343
#[stable(feature = "rust1", since = "1.0.0")]
344+
#[cfg(not(stage0))]
345+
pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
346+
347+
/// dox
348+
#[stable(feature = "rust1", since = "1.0.0")]
349+
#[cfg(stage0)]
338350
pub fn copy<T>(dst: *mut T, src: *const T, count: usize);
339351

340352
/// Invokes memset on the specified pointer, setting `count * size_of::<T>()`

src/libcore/mem.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
229229
let mut t: T = uninitialized();
230230

231231
// Perform the swap, `&mut` pointers never alias
232-
ptr::copy_nonoverlapping(&mut t, &*x, 1);
233-
ptr::copy_nonoverlapping(x, &*y, 1);
234-
ptr::copy_nonoverlapping(y, &t, 1);
232+
ptr::copy_nonoverlapping(&*x, &mut t, 1);
233+
ptr::copy_nonoverlapping(&*y, x, 1);
234+
ptr::copy_nonoverlapping(&t, y, 1);
235235

236236
// y and t now point to the same thing, but we need to completely forget `t`
237237
// because it's no longer relevant.

0 commit comments

Comments
 (0)