summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-03-13 09:41:35 -0700
committerbors <bors@rust-lang.org>2014-03-13 09:41:35 -0700
commit3fbee34a89c478f959046bf4b4e12a70e937c374 (patch)
treede26db2adf42af2da86e7f2a21bc1adec4fe9e19 /src/libstd
parent6ca57736ccd2b9d0c7288f997b31c0391dd1dbca (diff)
parent9faa2a58f2403165eed7caefbac30b17d93f0837 (diff)
downloadrust-3fbee34a89c478f959046bf4b4e12a70e937c374.tar.gz
rust-3fbee34a89c478f959046bf4b4e12a70e937c374.zip
auto merge of #12238 : ktt3ja/rust/lifetime-error-msg, r=nikomatsakis
For the following code snippet:

```rust
struct Foo { bar: int }
fn foo1(x: &Foo) -> &int {
    &x.bar
}
```

This PR generates the following error message:

```rust
test.rs:2:1: 4:2 note: consider using an explicit lifetime parameter as shown: fn foo1<'a>(x: &'a Foo) -> &'a int
test.rs:2 fn foo1(x: &Foo) -> &int {
test.rs:3     &x.bar
test.rs:4 }
test.rs:3:5: 3:11 error: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
test.rs:3     &x.bar
              ^~~~~~
```

Currently it does not support methods.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/vec_ng.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/libstd/vec_ng.rs b/src/libstd/vec_ng.rs
index 33916d0e3bf..199fc68be47 100644
--- a/src/libstd/vec_ng.rs
+++ b/src/libstd/vec_ng.rs
@@ -399,6 +399,11 @@ impl<T> Vec<T> {
         self.insert(0, element)
     }
 
+    #[inline]
+    pub fn shift(&mut self) -> Option<T> {
+        self.remove(0)
+    }
+
     pub fn insert(&mut self, index: uint, element: T) {
         let len = self.len();
         assert!(index <= len);
@@ -421,6 +426,30 @@ impl<T> Vec<T> {
         }
     }
 
+    fn remove(&mut self, index: uint) -> Option<T> {
+        let len = self.len();
+        if index < len {
+            unsafe { // infallible
+                let ret;
+                {
+                    let slice = self.as_mut_slice();
+                    // the place we are taking from.
+                    let ptr = slice.as_mut_ptr().offset(index as int);
+                    // copy it out, unsafely having a copy of the value on
+                    // the stack and in the vector at the same time.
+                    ret = Some(ptr::read(ptr as *T));
+
+                    // Shift everything down to fill in that spot.
+                    ptr::copy_memory(ptr, &*ptr.offset(1), len - index - 1);
+                }
+                self.set_len(len - 1);
+                ret
+            }
+        } else {
+            None
+        }
+    }
+
     #[inline]
     pub fn rev_iter<'a>(&'a self) -> RevItems<'a,T> {
         self.as_slice().rev_iter()