about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorErick Tryzelaar <erick.tryzelaar@gmail.com>2013-03-03 08:06:31 -0800
committerErick Tryzelaar <erick.tryzelaar@gmail.com>2013-03-05 19:37:04 -0800
commitd60747a24839c50165b0e2ea35592a7cb008f69b (patch)
tree13fd07a9dc857a6e931156c1defaf5ad810bd302 /src
parent5ae06ae9dea2f1dac157193b702f640e2216a5a9 (diff)
downloadrust-d60747a24839c50165b0e2ea35592a7cb008f69b.tar.gz
rust-d60747a24839c50165b0e2ea35592a7cb008f69b.zip
core: convert vec::{init,initn} to return references
Diffstat (limited to 'src')
-rw-r--r--src/libcore/vec.rs70
-rw-r--r--src/librustc/metadata/decoder.rs8
-rw-r--r--src/librustc/middle/ty.rs2
3 files changed, 54 insertions, 26 deletions
diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs
index 98316066fd1..2884d448610 100644
--- a/src/libcore/vec.rs
+++ b/src/libcore/vec.rs
@@ -229,9 +229,11 @@ pub pure fn tail<T>(v: &r/[T]) -> &r/[T] { slice(v, 1, v.len()) }
 pub pure fn tailn<T>(v: &r/[T], n: uint) -> &r/[T] { slice(v, n, v.len()) }
 
 /// Returns a vector containing all but the last element of a slice
-pub pure fn init<T:Copy>(v: &[const T]) -> ~[T] {
-    assert len(v) != 0u;
-    slice(v, 0u, len(v) - 1u).to_vec()
+pub pure fn init<T>(v: &r/[T]) -> &r/[T] { slice(v, 0, v.len() - 1) }
+
+/// Returns a vector containing all but the last `n' elements of a slice
+pub pure fn initn<T>(v: &r/[T], n: uint) -> &r/[T] {
+    slice(v, 0, v.len() - n)
 }
 
 /// Returns the last element of the slice `v`, failing if the slice is empty.
@@ -1694,17 +1696,12 @@ impl<T> Container for &[const T] {
 }
 
 pub trait CopyableVector<T> {
-    pure fn init(&self) -> ~[T];
     pure fn last(&self) -> T;
     pure fn slice(&self, start: uint, end: uint) -> ~[T];
 }
 
 /// Extension methods for vectors
-impl<T:Copy> CopyableVector<T> for &[const T] {
-    /// Returns all but the last elemnt of a vector
-    #[inline]
-    pure fn init(&self) -> ~[T] { init(*self) }
-
+impl<T: Copy> CopyableVector<T> for &[const T] {
     /// Returns the last element of a `v`, failing if the vector is empty.
     #[inline]
     pure fn last(&self) -> T { last(*self) }
@@ -1722,6 +1719,8 @@ pub trait ImmutableVector<T> {
     pure fn head_opt(&self) -> Option<&self/T>;
     pure fn tail(&self) -> &self/[T];
     pure fn tailn(&self, n: uint) -> &self/[T];
+    pure fn init(&self) -> &self/[T];
+    pure fn initn(&self, n: uint) -> &self/[T];
     pure fn foldr<U: Copy>(&self, z: U, p: fn(t: &T, u: U) -> U) -> U;
     pure fn map<U>(&self, f: fn(t: &T) -> U) -> ~[U];
     pure fn mapi<U>(&self, f: fn(uint, t: &T) -> U) -> ~[U];
@@ -1755,6 +1754,14 @@ impl<T> ImmutableVector<T> for &[T] {
     #[inline]
     pure fn tailn(&self, n: uint) -> &self/[T] { tailn(*self, n) }
 
+    /// Returns all but the last elemnt of a vector
+    #[inline]
+    pure fn init(&self) -> &self/[T] { init(*self) }
+
+    /// Returns all but the last `n' elemnts of a vector
+    #[inline]
+    pure fn initn(&self, n: uint) -> &self/[T] { initn(*self, n) }
+
     /// Reduce a vector from right to left
     #[inline]
     pure fn foldr<U:Copy>(&self, z: U, p: fn(t: &T, u: U) -> U) -> U {
@@ -2639,6 +2646,38 @@ mod tests {
     }
 
     #[test]
+    fn test_init() {
+        let mut a = ~[11];
+        assert a.init() == &[];
+        a = ~[11, 12];
+        assert a.init() == &[11];
+    }
+
+    #[init]
+    #[should_fail]
+    #[ignore(cfg(windows))]
+    fn test_init_empty() {
+        let a: ~[int] = ~[];
+        a.init();
+    }
+
+    #[test]
+    fn test_initn() {
+        let mut a = ~[11, 12, 13];
+        assert a.initn(0) == &[11, 12, 13];
+        a = ~[11, 12, 13];
+        assert a.initn(2) == &[11];
+    }
+
+    #[init]
+    #[should_fail]
+    #[ignore(cfg(windows))]
+    fn test_initn_empty() {
+        let a: ~[int] = ~[];
+        a.initn(2);
+    }
+
+    #[test]
     fn test_last() {
         let mut n = last_opt(~[]);
         assert (n.is_none());
@@ -3318,12 +3357,6 @@ mod tests {
     }
 
     #[test]
-    fn test_init() {
-        let v = init(~[1, 2, 3]);
-        assert v == ~[1, 2];
-    }
-
-    #[test]
     fn test_split() {
         fn f(x: &int) -> bool { *x == 3 }
 
@@ -3388,13 +3421,6 @@ mod tests {
     }
 
     #[test]
-    #[should_fail]
-    #[ignore(cfg(windows))]
-    fn test_init_empty() {
-        init::<int>(~[]);
-    }
-
-    #[test]
     fn test_concat() {
         assert concat(~[~[1], ~[2,3]]) == ~[1, 2, 3];
     }
diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs
index aa18c1eb450..0bf1fc38704 100644
--- a/src/librustc/metadata/decoder.rs
+++ b/src/librustc/metadata/decoder.rs
@@ -558,7 +558,10 @@ pub fn maybe_get_item_ast(intr: @ident_interner, cdata: cmd, tcx: ty::ctxt,
                        -> csearch::found_ast {
     debug!("Looking up item: %d", id);
     let item_doc = lookup_item(id, cdata.data);
-    let path = vec::init(item_path(intr, item_doc));
+    let path = {
+        let item_path = item_path(intr, item_doc);
+        vec::from_slice(item_path.init())
+    };
     match decode_inlined_item(cdata, tcx, path, item_doc) {
       Some(ref ii) => csearch::found((/*bad*/copy *ii)),
       None => {
@@ -566,8 +569,7 @@ pub fn maybe_get_item_ast(intr: @ident_interner, cdata: cmd, tcx: ty::ctxt,
           Some(did) => {
             let did = translate_def_id(cdata, did);
             let parent_item = lookup_item(did.node, cdata.data);
-            match decode_inlined_item(cdata, tcx, path,
-                                               parent_item) {
+            match decode_inlined_item(cdata, tcx, path, parent_item) {
               Some(ref ii) => csearch::found_parent(did, (/*bad*/copy *ii)),
               None => csearch::not_found
             }
diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs
index 644b2c51454..12a2011ad25 100644
--- a/src/librustc/middle/ty.rs
+++ b/src/librustc/middle/ty.rs
@@ -3815,7 +3815,7 @@ pub fn item_path(cx: ctxt, id: ast::def_id) -> ast_map::path {
           }
 
           ast_map::node_variant(ref variant, _, path) => {
-            vec::append_one(vec::init(*path),
+            vec::append_one(vec::from_slice(vec::init(*path)),
                             ast_map::path_name((*variant).node.name))
           }