summary refs log tree commit diff
path: root/src/libstd/vec.rs
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2014-01-15 14:39:08 -0500
committerNiko Matsakis <niko@alum.mit.edu>2014-01-15 18:34:38 -0500
commit419ac4a1b899ba88fb360b4c71c08f3610564cd4 (patch)
treea67114bd33e84818930d054f29ac81b726a88198 /src/libstd/vec.rs
parent149fc76698318f8f7cdfaa37a818e347721764e7 (diff)
downloadrust-419ac4a1b899ba88fb360b4c71c08f3610564cd4.tar.gz
rust-419ac4a1b899ba88fb360b4c71c08f3610564cd4.zip
Issue #3511 - Rationalize temporary lifetimes.
Major changes:

- Define temporary scopes in a syntax-based way that basically defaults
  to the innermost statement or conditional block, except for in
  a `let` initializer, where we default to the innermost block. Rules
  are documented in the code, but not in the manual (yet).
  See new test run-pass/cleanup-value-scopes.rs for examples.
- Refactors Datum to better define cleanup roles.
- Refactor cleanup scopes to not be tied to basic blocks, permitting
  us to have a very large number of scopes (one per AST node).
- Introduce nascent documentation in trans/doc.rs covering datums and
  cleanup in a more comprehensive way.
Diffstat (limited to 'src/libstd/vec.rs')
-rw-r--r--src/libstd/vec.rs31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index 797582e57f4..fd7640de126 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -168,7 +168,7 @@ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> ~[T] {
         let mut v = with_capacity(n_elts);
         let p = v.as_mut_ptr();
         let mut i = 0u;
-        (|| {
+        (|| { // FIXME what if we fail in the middle of this loop?
             while i < n_elts {
                 intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i as int)), t.clone());
                 i += 1u;
@@ -239,6 +239,25 @@ pub fn build<A>(size: Option<uint>, builder: |push: |v: A||) -> ~[A] {
     vec
 }
 
+/**
+ * Converts a pointer to A into a slice of length 1 (without copying).
+ */
+pub fn ref_slice<'a, A>(s: &'a A) -> &'a [A] {
+    unsafe {
+        cast::transmute(Slice { data: s, len: 1 })
+    }
+}
+
+/**
+ * Converts a pointer to A into a slice of length 1 (without copying).
+ */
+pub fn mut_ref_slice<'a, A>(s: &'a mut A) -> &'a mut [A] {
+    unsafe {
+        let ptr: *A = cast::transmute(s);
+        cast::transmute(Slice { data: ptr, len: 1 })
+    }
+}
+
 /// An iterator over the slices of a vector separated by elements that
 /// match a predicate function.
 pub struct SplitIterator<'a, T> {
@@ -2175,6 +2194,9 @@ pub trait MutableVector<'a, T> {
     /// Returns an iterator that allows modifying each value
     fn mut_iter(self) -> VecMutIterator<'a, T>;
 
+    /// Returns a mutable pointer to the last item in the vector.
+    fn mut_last(self) -> &'a mut T;
+
     /// Returns a reversed iterator that allows modifying each value
     fn mut_rev_iter(self) -> MutRevIterator<'a, T>;
 
@@ -2438,6 +2460,13 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
     }
 
     #[inline]
+    fn mut_last(self) -> &'a mut T {
+        let len = self.len();
+        if len == 0 { fail!("mut_last: empty vector") }
+        &mut self[len - 1]
+    }
+
+    #[inline]
     fn mut_rev_iter(self) -> MutRevIterator<'a, T> {
         self.mut_iter().invert()
     }