Skip to content

Commit a6239d3

Browse files
committed
---
yaml --- r: 12872 b: refs/heads/master c: c568cf6 h: refs/heads/master v: v3
1 parent 0cc2636 commit a6239d3

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 0eed37da290aa040bc57191da97bd3d322c1203d
2+
refs/heads/master: c568cf6099b94d50ff7ca28e60d376ebe2cc9255
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/src/libcore/vec.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export init;
2222
export last;
2323
export last_opt;
2424
export slice;
25+
export view;
2526
export split;
2627
export splitn;
2728
export rsplit;
@@ -254,6 +255,18 @@ fn slice<T: copy>(v: [const T], start: uint, end: uint) -> [T] {
254255
ret result;
255256
}
256257

258+
#[doc = "Return a slice that points into another slice."]
259+
fn view<T: copy>(v: [const T]/&, start: uint, end: uint) -> [T]/&a {
260+
assert (start <= end);
261+
assert (end <= len(v));
262+
unpack_slice(v) {|p, _len|
263+
unsafe {
264+
::unsafe::reinterpret_cast(
265+
(ptr::offset(p, start), (end - start) * sys::size_of::<T>()))
266+
}
267+
}
268+
}
269+
257270
#[doc = "
258271
Split the vector `v` by applying each element against the predicate `f`.
259272
"]
@@ -2029,6 +2042,15 @@ mod tests {
20292042
reserve(v, 10u);
20302043
assert capacity(v) == 10u;
20312044
}
2045+
2046+
#[test]
2047+
fn test_view() {
2048+
let v = [1, 2, 3, 4, 5];
2049+
let v = view(v, 1u, 3u);
2050+
assert(len(v) == 2u);
2051+
assert(v[0] == 2);
2052+
assert(v[1] == 3);
2053+
}
20322054
}
20332055

20342056
// Local Variables:

0 commit comments

Comments
 (0)