提交 ad025102 编写于 作者: P Patrick Walton

libcore: Implement a memory-safe "each_val" for data in aliasable, mutable locations

上级 0c2e6fda
......@@ -17,7 +17,7 @@
pub use vec::{ImmutableEqVector, ImmutableCopyableVector};
pub use vec::{MutableVector, MutableCopyableVector};
pub use iter::{BaseIter, ExtendedIter, EqIter, CopyableIter};
pub use iter::{CopyableOrderedIter, Times};
pub use iter::{CopyableOrderedIter, CopyableNonstrictIter, Times};
pub use num::Num;
pub use ptr::Ptr;
pub use to_str::ToStr;
......
......@@ -46,6 +46,13 @@ pub trait CopyableOrderedIter<A:Copy Ord> {
pure fn max() -> A;
}
pub trait CopyableNonstrictIter<A:Copy> {
// Like "each", but copies out the value. If the receiver is mutated while
// iterating over it, the semantics must not be memory-unsafe but are
// otherwise undefined.
pure fn each_val(&const self, f: &fn(A) -> bool);
}
// A trait for sequences that can be by imperatively pushing elements
// onto them.
pub trait Buildable<A> {
......
......@@ -2020,6 +2020,37 @@ impl<A: Copy Ord> &[A]: iter::CopyableOrderedIter<A> {
pure fn min() -> A { iter::min(&self) }
pure fn max() -> A { iter::max(&self) }
}
impl<A:Copy> &[A] : iter::CopyableNonstrictIter<A> {
pure fn each_val(&const self, f: fn(A) -> bool) {
let mut i = 0;
while i < self.len() {
if !f(copy self[i]) { break; }
i += 1;
}
}
}
impl<A:Copy> ~[A] : iter::CopyableNonstrictIter<A> {
pure fn each_val(&const self, f: fn(A) -> bool) {
let mut i = 0;
while i < self.len() {
if !f(copy self[i]) { break; }
i += 1;
}
}
}
impl<A:Copy> @[A] : iter::CopyableNonstrictIter<A> {
pure fn each_val(&const self, f: fn(A) -> bool) {
let mut i = 0;
while i < self.len() {
if !f(copy self[i]) { break; }
i += 1;
}
}
}
// ___________________________________________________________________________
#[cfg(test)]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册