Explorar el Código

Remove grow method

ticki hace 8 años
padre
commit
f5baadbaf1
Se han modificado 3 ficheros con 24 adiciones y 11 borrados
  1. 17 6
      src/bookkeeper.rs
  2. 2 0
      src/micro.rs
  3. 5 5
      src/vec.rs

+ 17 - 6
src/bookkeeper.rs

@@ -540,7 +540,7 @@ impl Bookkeeper {
             let res = self.pool.push(block);
 
             // Make some assertions.
-            debug_assert!(res.is_ok(), "Push failed (buffer filled).");
+            debug_assert!(res.is_ok(), "Push failed (buffer full).");
         }
         self.check();
     }
@@ -734,8 +734,23 @@ impl Bookkeeper {
                 self.pool.len() - ind
             })
         };
-        log!(self.pool;ind, "moving {}", n);
+
+        // Log the operation.
+        log!(self.pool;ind, "Moving {}", n);
+
         unsafe {
+            // TODO clean this mess up.
+
+            if ind + n == self.pool.len() {
+                // We will move a block into reserved memory but outside of the vec's bounds. For
+                // that reason, we push an uninitialized element to extend the length, which will
+                // be assigned in the memcpy.
+                let res = self.pool.push(mem::uninitialized());
+
+                // Just some assertions...
+                debug_assert!(res.is_ok(), "Push failed (buffer full).");
+            }
+
             // Memmove the elements.
             ptr::copy(self.pool.get_unchecked(ind) as *const Block,
                       self.pool.get_unchecked_mut(ind + 1) as *mut Block, n);
@@ -743,10 +758,6 @@ impl Bookkeeper {
             // Set the element.
             *self.pool.get_unchecked_mut(ind) = block;
         }
-        if ind + n == self.pool.len() {
-            // We moved a block into reserved memory but outside of the vec's bounds
-            self.pool.grow();
-        }
 
         // Check consistency.
         self.check();

+ 2 - 0
src/micro.rs

@@ -1,5 +1,7 @@
 //! Micro slots for caching small allocations.
 
+// TODO needs tests and documentation.
+
 use prelude::*;
 
 use core::{marker, mem};

+ 5 - 5
src/vec.rs

@@ -108,17 +108,17 @@ impl<T: Leak> Vec<T> {
     /// Truncate this vector.
     ///
     /// This is O(1).
+    ///
+    /// # Panics
+    ///
+    /// Panics on out-of-bound.
     pub fn truncate(&mut self, len: usize) {
+        // Bound check.
         assert!(len <= self.len, "Out of bound.");
 
         self.len = len;
     }
 
-    pub fn grow(&mut self) {
-        if self.len < self.cap {
-            self.len += 1;
-        }
-    }
     /// Check the validity of a block with respect to the vector.
     ///
     /// Blocks not passing this checks might lead to logic errors when used as buffer for the