about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorErick Tryzelaar <erick.tryzelaar@gmail.com>2013-03-02 21:49:50 -0800
committerErick Tryzelaar <erick.tryzelaar@gmail.com>2013-03-05 19:37:04 -0800
commit359bb3e10bb022aabc5bfc60e48d3dfffc2ee62c (patch)
tree9d07692401079d56e4ac81a34fd908f615b0e223 /src
parent431e756fd72df1c092e71f6e605e82385a9c6881 (diff)
downloadrust-359bb3e10bb022aabc5bfc60e48d3dfffc2ee62c.tar.gz
rust-359bb3e10bb022aabc5bfc60e48d3dfffc2ee62c.zip
core: convert vec::{head,head_opt} to return references
Diffstat (limited to 'src')
-rw-r--r--src/libcore/vec.rs52
-rw-r--r--src/librust/rust.rc12
-rw-r--r--src/librustdoc/config.rs2
-rw-r--r--src/librustdoc/desc_to_brief_pass.rs24
-rw-r--r--src/librustdoc/unindent_pass.rs2
-rw-r--r--src/test/run-pass/zip-same-length.rs7
6 files changed, 68 insertions, 31 deletions
diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs
index ab5f04ace79..20aadd79d12 100644
--- a/src/libcore/vec.rs
+++ b/src/libcore/vec.rs
@@ -211,7 +211,16 @@ pub pure fn build_sized_opt<A>(size: Option<uint>,
 // Accessors
 
 /// Returns the first element of a vector
-pub pure fn head<T:Copy>(v: &[const T]) -> T { v[0] }
+pub pure fn head<T>(v: &r/[T]) -> &r/T {
+    if v.len() == 0 { fail!(~"last_unsafe: empty vector") }
+    &v[0]
+}
+
+/// Returns `Some(x)` where `x` is the first element of the slice `v`,
+/// or `None` if the vector is empty.
+pub pure fn head_opt<T>(v: &r/[T]) -> Option<&r/T> {
+    if v.len() == 0 { None } else { Some(&v[0]) }
+}
 
 /// Returns a vector containing all but the first element of a slice
 pub pure fn tail<T:Copy>(v: &[const T]) -> ~[T] {
@@ -1692,7 +1701,6 @@ impl<T> Container for &[const T] {
 }
 
 pub trait CopyableVector<T> {
-    pure fn head(&self) -> T;
     pure fn init(&self) -> ~[T];
     pure fn last(&self) -> T;
     pure fn slice(&self, start: uint, end: uint) -> ~[T];
@@ -1701,10 +1709,6 @@ pub trait CopyableVector<T> {
 
 /// Extension methods for vectors
 impl<T:Copy> CopyableVector<T> for &[const T] {
-    /// Returns the first element of a vector
-    #[inline]
-    pure fn head(&self) -> T { head(*self) }
-
     /// Returns all but the last elemnt of a vector
     #[inline]
     pure fn init(&self) -> ~[T] { init(*self) }
@@ -1726,7 +1730,9 @@ impl<T:Copy> CopyableVector<T> for &[const T] {
 
 pub trait ImmutableVector<T> {
     pure fn view(&self, start: uint, end: uint) -> &self/[T];
-    pure fn foldr<U:Copy>(&self, z: U, p: fn(t: &T, u: U) -> U) -> U;
+    pure fn head(&self) -> &self/T;
+    pure fn head_opt(&self) -> Option<&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];
     fn map_r<U>(&self, f: fn(x: &T) -> U) -> ~[U];
@@ -1743,6 +1749,14 @@ impl<T> ImmutableVector<T> for &[T] {
         slice(*self, start, end)
     }
 
+    /// Returns the first element of a vector, failing if the vector is empty.
+    #[inline]
+    pure fn head(&self) -> &self/T { head(*self) }
+
+    /// Returns the first element of a vector
+    #[inline]
+    pure fn head_opt(&self) -> Option<&self/T> { head_opt(*self) }
+
     /// Reduce a vector from right to left
     #[inline]
     pure fn foldr<U:Copy>(&self, z: U, p: fn(t: &T, u: U) -> U) -> U {
@@ -2570,8 +2584,28 @@ mod tests {
 
     #[test]
     fn test_head() {
-        let a = ~[11, 12];
-        assert (head(a) == 11);
+        let mut a = ~[11];
+        assert a.head() == &11;
+        a = ~[11, 12];
+        assert a.head() == &11;
+    }
+
+    #[test]
+    #[should_fail]
+    #[ignore(cfg(windows))]
+    fn test_head_empty() {
+        let a: ~[int] = ~[];
+        a.head();
+    }
+
+    #[test]
+    fn test_head_opt() {
+        let mut a = ~[];
+        assert a.head_opt() == None;
+        a = ~[11];
+        assert a.head_opt().unwrap() == &11;
+        a = ~[11, 12];
+        assert a.head_opt().unwrap() == &11;
     }
 
     #[test]
diff --git a/src/librust/rust.rc b/src/librust/rust.rc
index 37e0f874b40..235ed6412a3 100644
--- a/src/librust/rust.rc
+++ b/src/librust/rust.rc
@@ -130,7 +130,7 @@ fn cmd_help(args: &[~str]) -> ValidUsage {
                     UsgExec(commandline) => {
                         let words = str::words(commandline);
                         let (prog, args) = (words.head(), words.tail());
-                        run::run_program(prog, args);
+                        run::run_program(*prog, args);
                     }
                 }
                 Valid
@@ -186,7 +186,10 @@ fn do_command(command: &Command, args: &[~str]) -> ValidUsage {
         Exec(commandline) => {
             let words = str::words(commandline);
             let (prog, prog_args) = (words.head(), words.tail());
-            let exitstatus = run::run_program(prog, prog_args + args);
+            let exitstatus = run::run_program(
+                *prog,
+                vec::append(vec::from_slice(prog_args), args)
+            );
             os::set_exit_status(exitstatus);
             Valid
         }
@@ -221,11 +224,12 @@ fn usage() {
 }
 
 pub fn main() {
-    let args = os::args().tail();
+    let os_args = os::args();
+    let args = os_args.tail();
 
     if !args.is_empty() {
         for commands.each |command| {
-            if command.cmd == args.head() {
+            if command.cmd == *args.head() {
                 let result = do_command(command, args.tail());
                 if result.is_valid() { return; }
             }
diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs
index 187ddcc0152..58316110b64 100644
--- a/src/librustdoc/config.rs
+++ b/src/librustdoc/config.rs
@@ -132,7 +132,7 @@ pub fn parse_config_(
     match getopts::getopts(args, opts) {
         result::Ok(matches) => {
             if matches.free.len() == 1 {
-                let input_crate = Path(vec::head(matches.free));
+                let input_crate = Path(copy *matches.free.head());
                 config_from_opts(&input_crate, &matches, program_output)
             } else if matches.free.is_empty() {
                 result::Err(~"no crates specified")
diff --git a/src/librustdoc/desc_to_brief_pass.rs b/src/librustdoc/desc_to_brief_pass.rs
index 963715796e6..3a4cd9e1379 100644
--- a/src/librustdoc/desc_to_brief_pass.rs
+++ b/src/librustdoc/desc_to_brief_pass.rs
@@ -144,14 +144,14 @@ fn parse_desc(desc: ~str) -> Option<~str> {
 fn first_sentence(s: ~str) -> Option<~str> {
     let paras = paragraphs(s);
     if !paras.is_empty() {
-        let first_para = vec::head(paras);
-        Some(str::replace(first_sentence_(first_para), ~"\n", ~" "))
+        let first_para = paras.head();
+        Some(str::replace(first_sentence_(*first_para), ~"\n", ~" "))
     } else {
         None
     }
 }
 
-fn first_sentence_(s: ~str) -> ~str {
+fn first_sentence_(s: &str) -> ~str {
     let mut dotcount = 0;
     // The index of the character following a single dot. This allows
     // Things like [0..1) to appear in the brief description
@@ -169,16 +169,16 @@ fn first_sentence_(s: ~str) -> ~str {
         }
     };
     match idx {
-      Some(idx) if idx > 2u => {
-        str::slice(s, 0u, idx - 1u)
-      }
-      _ => {
-        if str::ends_with(s, ~".") {
-            str::slice(s, 0u, str::len(s))
-        } else {
-            copy s
+        Some(idx) if idx > 2u => {
+            str::from_slice(str::view(s, 0, idx - 1))
+        }
+        _ => {
+            if str::ends_with(s, ~".") {
+                str::from_slice(s)
+            } else {
+                str::from_slice(s)
+            }
         }
-      }
     }
 }
 
diff --git a/src/librustdoc/unindent_pass.rs b/src/librustdoc/unindent_pass.rs
index d5b9756faa5..5fafcf392be 100644
--- a/src/librustdoc/unindent_pass.rs
+++ b/src/librustdoc/unindent_pass.rs
@@ -78,7 +78,7 @@ fn unindent(s: &str) -> ~str {
     };
 
     if !lines.is_empty() {
-        let unindented = ~[str::trim(vec::head(lines))]
+        let unindented = ~[lines.head().trim()]
             + do vec::tail(lines).map |line| {
             if str::is_whitespace(*line) {
                 copy *line
diff --git a/src/test/run-pass/zip-same-length.rs b/src/test/run-pass/zip-same-length.rs
index 1ade9e8246f..29b58cd6431 100644
--- a/src/test/run-pass/zip-same-length.rs
+++ b/src/test/run-pass/zip-same-length.rs
@@ -10,7 +10,6 @@
 
 // In this case, the code should compile and should
 // succeed at runtime
-use core::vec::{head, last, same_length, zip};
 
 fn enum_chars(start: u8, end: u8) -> ~[char] {
     assert start < end;
@@ -33,8 +32,8 @@ pub fn main() {
     let chars = enum_chars(a, j);
     let ints = enum_uints(k, l);
 
-    let ps = zip(chars, ints);
+    let ps = vec::zip(chars, ints);
 
-    assert (head(ps) == ('a', 1u));
-    assert (last(ps) == (j as char, 10u));
+    assert (ps.head() == &('a', 1u));
+    assert (ps.last() == (j as char, 10u));
 }