Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std: add an external iterator for mutating vec elements #6964

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2427,6 +2427,7 @@ impl<T:Eq> OwnedEqVector<T> for ~[T] {
#[allow(missing_doc)]
pub trait MutableVector<'self, T> {
fn mut_slice(self, start: uint, end: uint) -> &'self mut [T];
fn mut_iter(self) -> MutVecIterator<'self, T>;

unsafe fn unsafe_mut_ref(&self, index: uint) -> *mut T;
unsafe fn unsafe_set(&self, index: uint, val: T);
Expand All @@ -2438,6 +2439,15 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
mut_slice(self, start, end)
}

#[inline]
fn mut_iter(self) -> MutVecIterator<'self, T> {
unsafe {
let p = vec::raw::to_mut_ptr(self);
MutVecIterator{ptr: p, end: p.offset(self.len()),
lifetime: cast::transmute(p)}
}
}

#[inline(always)]
unsafe fn unsafe_mut_ref(&self, index: uint) -> *mut T {
let pair_ptr: &(*mut T, uint) = transmute(self);
Expand Down Expand Up @@ -2962,6 +2972,30 @@ impl<'self, T> Iterator<&'self T> for VecIterator<'self, T> {
}
}

/// An external iterator for vectors with the possibility of mutating
/// elements. (use with the std::iterator module)
pub struct MutVecIterator<'self, T> {
priv ptr: *mut T,
priv end: *mut T,
priv lifetime: &'self mut T // FIXME: #5922
}

// could be implemented with &[T] with .slice(), but this avoids bounds checks
impl<'self, T> Iterator<&'self mut T> for MutVecIterator<'self, T> {
#[inline]
fn next(&mut self) -> Option<&'self mut T> {
unsafe {
if self.ptr == self.end {
None
} else {
let old = self.ptr;
self.ptr = self.ptr.offset(1);
Some(cast::transmute(old))
}
}
}
}

#[cfg(test)]
mod tests {
use option::{None, Option, Some};
Expand Down Expand Up @@ -4669,6 +4703,16 @@ mod tests {
}
}

#[test]
fn test_mut_iterator() {
use iterator::*;
let mut xs = [1, 2, 3, 4, 5];
for xs.mut_iter().advance |x| {
*x += 1;
}
assert_eq!(xs, [2, 3, 4, 5, 6])
}

#[test]
fn test_reverse_part() {
let mut values = [1,2,3,4,5];
Expand Down
19 changes: 19 additions & 0 deletions src/test/compile-fail/vec-mut-iter-borrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::iterator::*;

fn main() {
let mut xs = ~[1, 2, 3, 4];

for xs.mut_iter().advance |x| {
xs.push(1) //~ ERROR cannot borrow `xs` as mutable
}
}