Skip to content

Commit 9d4aab8

Browse files
ericktgraydon
authored andcommitted
libcore: add vec::{mut_view, const_view}.
1 parent a762c72 commit 9d4aab8

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/libcore/vec.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export init;
2727
export last;
2828
export last_opt;
2929
export slice;
30-
export view;
30+
export view, mut_view, const_view;
3131
export split;
3232
export splitn;
3333
export rsplit;
@@ -325,6 +325,30 @@ pure fn view<T>(v: &[T], start: uint, end: uint) -> &[T] {
325325
}
326326
}
327327

328+
/// Return a slice that points into another slice.
329+
pure fn mut_view<T>(v: &[mut T], start: uint, end: uint) -> &[mut T] {
330+
assert (start <= end);
331+
assert (end <= len(v));
332+
do unpack_slice(v) |p, _len| {
333+
unsafe {
334+
::unsafe::reinterpret_cast(
335+
(ptr::offset(p, start), (end - start) * sys::size_of::<T>()))
336+
}
337+
}
338+
}
339+
340+
/// Return a slice that points into another slice.
341+
pure fn const_view<T>(v: &[const T], start: uint, end: uint) -> &[const T] {
342+
assert (start <= end);
343+
assert (end <= len(v));
344+
do unpack_slice(v) |p, _len| {
345+
unsafe {
346+
::unsafe::reinterpret_cast(
347+
(ptr::offset(p, start), (end - start) * sys::size_of::<T>()))
348+
}
349+
}
350+
}
351+
328352
/// Split the vector `v` by applying each element against the predicate `f`.
329353
fn split<T: copy>(v: &[T], f: fn(T) -> bool) -> ~[~[T]] {
330354
let ln = len(v);

0 commit comments

Comments
 (0)