فهرست منبع

Make use of .expect() over .unwrap()

It is generally considered better to do so, due to the more descriptive error messages.

- Remove warnings from the minimal.rs test
ticki 8 سال پیش
والد
کامیت
8b438f1823
4فایلهای تغییر یافته به همراه15 افزوده شده و 8 حذف شده
  1. 3 1
      src/allocator.rs
  2. 7 3
      src/bookkeeper.rs
  3. 2 1
      src/breaker.rs
  4. 3 3
      tests/minimal.rs

+ 3 - 1
src/allocator.rs

@@ -70,7 +70,9 @@ fn local_init() -> LocalAllocator {
         .alloc(4 * bookkeeper::EXTRA_ELEMENTS * mem::size_of::<Block>(), mem::align_of::<Block>());
 
     unsafe {
-        THREAD_ALLOCATOR.register_thread_destructor(dtor).unwrap();
+        // Register the thread destructor on the current thread.
+        THREAD_ALLOCATOR.register_thread_destructor(dtor)
+            .expect("Unable to register a thread destructor.");
 
         LocalAllocator {
             inner: Bookkeeper::new(Vec::from_raw_parts(initial_segment, 0)),

+ 7 - 3
src/bookkeeper.rs

@@ -303,7 +303,7 @@ pub trait Allocator: ops::DerefMut<Target = Bookkeeper> {
                         Some((n, b))
                     } else {
                         // Put the split block back together and place it back in its spot.
-                        a.merge_right(&mut b).unwrap();
+                        a.merge_right(&mut b).expect("Unable to merge block right.");
                         *i = a;
                         None
                     }
@@ -528,7 +528,8 @@ pub trait Allocator: ops::DerefMut<Target = Bookkeeper> {
                 log!(self;ind, "Merging {:?} to the right.", block);
 
                 // We'll merge it with the block at the end of the range.
-                block.merge_right(&mut self.remove_at(ind.end)).unwrap();
+                block.merge_right(&mut self.remove_at(ind.end))
+                    .expect("Unable to merge block right, to the end of the range.");
                 // Merge succeeded.
 
                 // Place the excessive block back.
@@ -580,7 +581,10 @@ pub trait Allocator: ops::DerefMut<Target = Bookkeeper> {
 
         // Try to merge it with the block to the right.
         if ind.end < self.pool.len() && block.left_to(&self.pool[ind.end]) {
-            block.merge_right(&mut self.remove_at(ind.end)).unwrap();
+            // Merge the block with the rightmost block in the range.
+            block.merge_right(&mut self.remove_at(ind.end))
+                .expect("Unable to merge block right to the block at the end of the range");
+
             // The merging succeeded. We proceed to try to close in the possible gap.
             if ind.start != 0 && self.pool[ind.start - 1].merge_right(&mut block).is_ok() {
                 self.check();

+ 2 - 1
src/breaker.rs

@@ -51,7 +51,8 @@ impl Breaker for Sbrk {
 
         // Use SBRK to allocate extra data segment. The alignment is used as precursor for our
         // allocated block. This ensures that it is properly memory aligned to the requested value.
-        let (alignment_block, rest) = Block::brk(brk_size).align(align).unwrap();
+        let (alignment_block, rest) = Block::brk(brk_size).align(align)
+            .expect("Unable to align block to the requested align.");
 
         // Split the block to leave the excessive space.
         let (res, excessive) = rest.split(size);

+ 3 - 3
tests/minimal.rs

@@ -2,9 +2,9 @@ extern crate ralloc;
 
 #[test]
 fn minimal() {
-    let mut a = Box::new(1);
-    let mut b = Box::new(2);
-    let mut c = Box::new(3);
+    let a = Box::new(1);
+    let b = Box::new(2);
+    let c = Box::new(3);
 
     assert_eq!(*a, 1);
     assert_eq!(*b, 2);