about summary refs log tree commit diff
path: root/library/alloc/src/raw_vec
diff options
context:
space:
mode:
authorblitzerr <rusty.blitzerr@gmail.com>2020-09-20 20:14:44 -0700
committerblitzerr <rusty.blitzerr@gmail.com>2020-09-21 16:43:36 -0700
commitd9d02fa168016b5b5b2033a2964a723f447f94b0 (patch)
treee649c2f8137da4515a01a4e606d641f7765cd18f /library/alloc/src/raw_vec
parentfb1dc34a831688f8eca89ea22ea2eb39e881d729 (diff)
downloadrust-d9d02fa168016b5b5b2033a2964a723f447f94b0.tar.gz
rust-d9d02fa168016b5b5b2033a2964a723f447f94b0.zip
Changing the alloc() to accept &self instead of &mut self
Diffstat (limited to 'library/alloc/src/raw_vec')
-rw-r--r--library/alloc/src/raw_vec/tests.rs15
1 files changed, 8 insertions, 7 deletions
diff --git a/library/alloc/src/raw_vec/tests.rs b/library/alloc/src/raw_vec/tests.rs
index cadd913aa6b..f348710d61a 100644
--- a/library/alloc/src/raw_vec/tests.rs
+++ b/library/alloc/src/raw_vec/tests.rs
@@ -1,4 +1,5 @@
 use super::*;
+use std::cell::Cell;
 
 #[test]
 fn allocator_param() {
@@ -17,17 +18,17 @@ fn allocator_param() {
     // A dumb allocator that consumes a fixed amount of fuel
     // before allocation attempts start failing.
     struct BoundedAlloc {
-        fuel: usize,
+        fuel: Cell<usize>,
     }
     unsafe impl AllocRef for BoundedAlloc {
-        fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
+        fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
             let size = layout.size();
-            if size > self.fuel {
+            if size > self.fuel.get() {
                 return Err(AllocErr);
             }
             match Global.alloc(layout) {
                 ok @ Ok(_) => {
-                    self.fuel -= size;
+                    self.fuel.update(|old| old - size);
                     ok
                 }
                 err @ Err(_) => err,
@@ -38,11 +39,11 @@ fn allocator_param() {
         }
     }
 
-    let a = BoundedAlloc { fuel: 500 };
+    let a = BoundedAlloc { fuel: Cell::new(500) };
     let mut v: RawVec<u8, _> = RawVec::with_capacity_in(50, a);
-    assert_eq!(v.alloc.fuel, 450);
+    assert_eq!(v.alloc.fuel.get(), 450);
     v.reserve(50, 150); // (causes a realloc, thus using 50 + 150 = 200 units of fuel)
-    assert_eq!(v.alloc.fuel, 250);
+    assert_eq!(v.alloc.fuel.get(), 250);
 }
 
 #[test]