Skip to content

Commit a6b2a2c

Browse files
committed
Separate vec::map and vec::map_mut
The safe-reference checker requires a copy of each mapped-over element only when the vector is mutable. Let's not pay that cost for immutable vectors.
1 parent 12f6e86 commit a6b2a2c

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/lib/vec.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,19 @@ Function: map
384384
385385
Apply a function to each element of a vector and return the results
386386
*/
387-
fn map<copy T, U>(f: block(T) -> U, v: [const T]) -> [U] {
387+
fn map<T, U>(f: block(T) -> U, v: [T]) -> [U] {
388+
let result = [];
389+
reserve(result, len(v));
390+
for elem: T in v { result += [f(elem)]; }
391+
ret result;
392+
}
393+
394+
/*
395+
Function: map_mut
396+
397+
Apply a function to each element of a mutable vector and return the results
398+
*/
399+
fn map_mut<copy T, U>(f: block(T) -> U, v: [const T]) -> [U] {
388400
let result = [];
389401
reserve(result, len(v));
390402
for elem: T in v {

0 commit comments

Comments
 (0)