Skip to content

Commit f97040a

Browse files
committed
std::vec: remove unnecessary count parameter on {bytes,
raw}::copy_memory. Slices carry their length with them, so we can just use that information.
1 parent 8f6df87 commit f97040a

File tree

9 files changed

+33
-52
lines changed

9 files changed

+33
-52
lines changed

src/libextra/uuid.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl Uuid {
175175
pub fn new_v4() -> Uuid {
176176
let ub = rand::task_rng().gen_vec(16);
177177
let mut uuid = Uuid{ bytes: [0, .. 16] };
178-
vec::bytes::copy_memory(uuid.bytes, ub, 16);
178+
vec::bytes::copy_memory(uuid.bytes, ub);
179179
uuid.set_variant(VariantRFC4122);
180180
uuid.set_version(Version4Random);
181181
uuid
@@ -202,7 +202,7 @@ impl Uuid {
202202
fields.data1 = to_be32(d1 as i32) as u32;
203203
fields.data2 = to_be16(d2 as i16) as u16;
204204
fields.data3 = to_be16(d3 as i16) as u16;
205-
vec::bytes::copy_memory(fields.data4, d4, 8);
205+
vec::bytes::copy_memory(fields.data4, d4);
206206

207207
unsafe {
208208
transmute(fields)
@@ -220,7 +220,7 @@ impl Uuid {
220220

221221
let mut uuid = Uuid{ bytes: [0, .. 16] };
222222
unsafe {
223-
vec::raw::copy_memory(uuid.bytes, b, 16);
223+
vec::raw::copy_memory(uuid.bytes, b);
224224
}
225225
Some(uuid)
226226
}
@@ -442,11 +442,7 @@ impl Zero for Uuid {
442442

443443
impl Clone for Uuid {
444444
/// Returns a copy of the UUID
445-
fn clone(&self) -> Uuid {
446-
let mut clone = Uuid{ bytes: [0, .. 16] };
447-
vec::bytes::copy_memory(clone.bytes, self.bytes, 16);
448-
clone
449-
}
445+
fn clone(&self) -> Uuid { *self }
450446
}
451447

452448
impl FromStr for Uuid {
@@ -509,7 +505,7 @@ impl rand::Rand for Uuid {
509505
fn rand<R: rand::Rng>(rng: &mut R) -> Uuid {
510506
let ub = rng.gen_vec(16);
511507
let mut uuid = Uuid{ bytes: [0, .. 16] };
512-
vec::bytes::copy_memory(uuid.bytes, ub, 16);
508+
vec::bytes::copy_memory(uuid.bytes, ub);
513509
uuid.set_variant(VariantRFC4122);
514510
uuid.set_version(Version4Random);
515511
uuid

src/librustc/util/sha2.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,14 @@ impl FixedBuffer for FixedBuffer64 {
136136
if input.len() >= buffer_remaining {
137137
copy_memory(
138138
self.buffer.mut_slice(self.buffer_idx, size),
139-
input.slice_to(buffer_remaining),
140-
buffer_remaining);
139+
input.slice_to(buffer_remaining));
141140
self.buffer_idx = 0;
142141
func(self.buffer);
143142
i += buffer_remaining;
144143
} else {
145144
copy_memory(
146145
self.buffer.mut_slice(self.buffer_idx, self.buffer_idx + input.len()),
147-
input,
148-
input.len());
146+
input);
149147
self.buffer_idx += input.len();
150148
return;
151149
}
@@ -164,8 +162,7 @@ impl FixedBuffer for FixedBuffer64 {
164162
let input_remaining = input.len() - i;
165163
copy_memory(
166164
self.buffer.mut_slice(0, input_remaining),
167-
input.slice_from(i),
168-
input.len() - i);
165+
input.slice_from(i));
169166
self.buffer_idx += input_remaining;
170167
}
171168

src/librustpkg/sha1.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,14 @@ impl FixedBuffer for FixedBuffer64 {
149149
if input.len() >= buffer_remaining {
150150
copy_memory(
151151
self.buffer.mut_slice(self.buffer_idx, size),
152-
input.slice_to(buffer_remaining),
153-
buffer_remaining);
152+
input.slice_to(buffer_remaining));
154153
self.buffer_idx = 0;
155154
func(self.buffer);
156155
i += buffer_remaining;
157156
} else {
158157
copy_memory(
159158
self.buffer.mut_slice(self.buffer_idx, self.buffer_idx + input.len()),
160-
input,
161-
input.len());
159+
input);
162160
self.buffer_idx += input.len();
163161
return;
164162
}
@@ -177,8 +175,7 @@ impl FixedBuffer for FixedBuffer64 {
177175
let input_remaining = input.len() - i;
178176
copy_memory(
179177
self.buffer.mut_slice(0, input_remaining),
180-
input.slice_from(i),
181-
input.len() - i);
178+
input.slice_from(i));
182179
self.buffer_idx += input_remaining;
183180
}
184181

src/libstd/c_str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl<'a> ToCStr for &'a [u8] {
293293
unsafe fn with_c_str<T>(v: &[u8], checked: bool, f: |*libc::c_char| -> T) -> T {
294294
if v.len() < BUF_LEN {
295295
let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit();
296-
vec::bytes::copy_memory(buf, v, v.len());
296+
vec::bytes::copy_memory(buf, v);
297297
buf[v.len()] = 0;
298298

299299
buf.as_mut_buf(|buf, _| {

src/libstd/io/buffered.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl<R: Reader> Reader for BufferedReader<R> {
122122
return None;
123123
}
124124
let nread = num::min(available.len(), buf.len());
125-
vec::bytes::copy_memory(buf, available, nread);
125+
vec::bytes::copy_memory(buf, available.slice_to(nread));
126126
nread
127127
};
128128
self.pos += nread;
@@ -185,7 +185,7 @@ impl<W: Writer> Writer for BufferedWriter<W> {
185185
self.inner.write(buf);
186186
} else {
187187
let dst = self.buf.mut_slice_from(self.pos);
188-
vec::bytes::copy_memory(dst, buf, buf.len());
188+
vec::bytes::copy_memory(dst, buf);
189189
self.pos += buf.len();
190190
}
191191
}

src/libstd/io/comm_adapters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<P: GenericPort<~[u8]>> Reader for PortReader<P> {
5757
let dst = buf.mut_slice_from(num_read);
5858
let src = prev.slice_from(self.pos);
5959
let count = cmp::min(dst.len(), src.len());
60-
bytes::copy_memory(dst, src, count);
60+
bytes::copy_memory(dst, src.slice_to(count));
6161
num_read += count;
6262
self.pos += count;
6363
},

src/libstd/io/mem.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ impl Writer for MemWriter {
5858

5959
// Do the necessary writes
6060
if left.len() > 0 {
61-
vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos),
62-
left, left.len());
61+
vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos), left);
6362
}
6463
if right.len() > 0 {
6564
self.buf.push_all(right);
@@ -116,7 +115,7 @@ impl Reader for MemReader {
116115
let input = self.buf.slice(self.pos, self.pos + write_len);
117116
let output = buf.mut_slice(0, write_len);
118117
assert_eq!(input.len(), output.len());
119-
vec::bytes::copy_memory(output, input, write_len);
118+
vec::bytes::copy_memory(output, input);
120119
}
121120
self.pos += write_len;
122121
assert!(self.pos <= self.buf.len());
@@ -175,8 +174,7 @@ impl<'a> Writer for BufWriter<'a> {
175174
return;
176175
}
177176

178-
vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos),
179-
buf, buf.len());
177+
vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos), buf);
180178
self.pos += buf.len();
181179
}
182180
}
@@ -222,7 +220,7 @@ impl<'a> Reader for BufReader<'a> {
222220
let input = self.buf.slice(self.pos, self.pos + write_len);
223221
let output = buf.mut_slice(0, write_len);
224222
assert_eq!(input.len(), output.len());
225-
vec::bytes::copy_memory(output, input, write_len);
223+
vec::bytes::copy_memory(output, input);
226224
}
227225
self.pos += write_len;
228226
assert!(self.pos <= self.buf.len());

src/libstd/vec.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,16 +1631,15 @@ impl<T> OwnedVector<T> for ~[T] {
16311631
{
16321632
let first_slice = self.slice(0, 1);
16331633
let last_slice = self.slice(next_ln, ln);
1634-
raw::copy_memory(cast::transmute(last_slice), first_slice, 1);
1634+
raw::copy_memory(cast::transmute(last_slice), first_slice);
16351635
}
16361636

16371637
// Memcopy everything to the left one element
16381638
{
16391639
let init_slice = self.slice(0, next_ln);
16401640
let tail_slice = self.slice(1, ln);
16411641
raw::copy_memory(cast::transmute(init_slice),
1642-
tail_slice,
1643-
next_ln);
1642+
tail_slice);
16441643
}
16451644

16461645
// Set the new length. Now the vector is back to normal
@@ -2312,18 +2311,14 @@ pub mod raw {
23122311
/**
23132312
* Copies data from one vector to another.
23142313
*
2315-
* Copies `count` bytes from `src` to `dst`. The source and destination
2316-
* may overlap.
2314+
* Copies `src` to `dst`. The source and destination may overlap.
23172315
*/
23182316
#[inline]
2319-
pub unsafe fn copy_memory<T>(dst: &mut [T], src: &[T],
2320-
count: uint) {
2321-
assert!(dst.len() >= count);
2322-
assert!(src.len() >= count);
2323-
2324-
dst.as_mut_buf(|p_dst, _len_dst| {
2325-
src.as_imm_buf(|p_src, _len_src| {
2326-
ptr::copy_memory(p_dst, p_src, count)
2317+
pub unsafe fn copy_memory<T>(dst: &mut [T], src: &[T]) {
2318+
dst.as_mut_buf(|p_dst, len_dst| {
2319+
src.as_imm_buf(|p_src, len_src| {
2320+
assert!(len_dst >= len_src)
2321+
ptr::copy_memory(p_dst, p_src, len_src)
23272322
})
23282323
})
23292324
}
@@ -2419,13 +2414,12 @@ pub mod bytes {
24192414
/**
24202415
* Copies data from one vector to another.
24212416
*
2422-
* Copies `count` bytes from `src` to `dst`. The source and destination
2423-
* may overlap.
2417+
* Copies `src` to `dst`. The source and destination may overlap.
24242418
*/
24252419
#[inline]
2426-
pub fn copy_memory(dst: &mut [u8], src: &[u8], count: uint) {
2420+
pub fn copy_memory(dst: &mut [u8], src: &[u8]) {
24272421
// Bound checks are done at vec::raw::copy_memory.
2428-
unsafe { vec::raw::copy_memory(dst, src, count) }
2422+
unsafe { vec::raw::copy_memory(dst, src) }
24292423
}
24302424

24312425
/**
@@ -3651,7 +3645,7 @@ mod tests {
36513645
unsafe {
36523646
let mut a = [1, 2, 3, 4];
36533647
let b = [1, 2, 3, 4, 5];
3654-
raw::copy_memory(a, b, 5);
3648+
raw::copy_memory(a, b);
36553649
}
36563650
}
36573651

src/test/bench/shootout-fasta-redux.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,10 @@ impl RepeatFasta {
9595
let mut buf = vec::from_elem(alu_len + LINE_LEN, 0u8);
9696
let alu: &[u8] = self.alu.as_bytes();
9797

98-
copy_memory(buf, alu, alu_len);
98+
copy_memory(buf, alu);
9999
let buf_len = buf.len();
100100
copy_memory(buf.mut_slice(alu_len, buf_len),
101-
alu,
102-
LINE_LEN);
101+
alu.slice_to(LINE_LEN));
103102

104103
let mut pos = 0;
105104
let mut bytes;

0 commit comments

Comments
 (0)