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.
This commit is contained in:
Marijn Haverbeke 2011-11-21 11:59:27 +01:00
parent 12f6e868f7
commit a6b2a2cdb1

View File

@ -384,7 +384,19 @@ fn grow_set<copy T>(&v: [mutable T], index: uint, initval: T, val: T) {
Apply a function to each element of a vector and return the results
*/
fn map<copy T, U>(f: block(T) -> U, v: [const T]) -> [U] {
fn map<T, U>(f: block(T) -> U, v: [T]) -> [U] {
let result = [];
reserve(result, len(v));
for elem: T in v { result += [f(elem)]; }
ret result;
}
/*
Function: map_mut
Apply a function to each element of a mutable vector and return the results
*/
fn map_mut<copy T, U>(f: block(T) -> U, v: [const T]) -> [U] {
let result = [];
reserve(result, len(v));
for elem: T in v {