]> git.lizzy.rs Git - rust.git/commitdiff
std: minor simplification to sync::deque.
authorHuon Wilson <dbau.pp+github@gmail.com>
Fri, 23 May 2014 15:07:05 +0000 (01:07 +1000)
committerHuon Wilson <dbau.pp+github@gmail.com>
Sat, 24 May 2014 11:44:37 +0000 (21:44 +1000)
src/libstd/sync/deque.rs

index a3fdc4d3eaff7e3d4b891794574b65d8a10ff912..46d6129ded8b81207207b45165e237504d9d387e 100644 (file)
@@ -54,7 +54,7 @@
 use iter::{range, Iterator};
 use kinds::Send;
 use kinds::marker;
-use mem::{forget, min_align_of, size_of, transmute};
+use mem::{forget, min_align_of, size_of, transmute, overwrite};
 use ops::Drop;
 use option::{Option, Some, None};
 use owned::Box;
@@ -371,20 +371,20 @@ fn size(&self) -> int { 1 << self.log_size }
     // Apparently LLVM cannot optimize (foo % (1 << bar)) into this implicitly
     fn mask(&self) -> int { (1 << self.log_size) - 1 }
 
+    unsafe fn elem(&self, i: int) -> *T { self.storage.offset(i & self.mask()) }
+
     // This does not protect against loading duplicate values of the same cell,
     // nor does this clear out the contents contained within. Hence, this is a
     // very unsafe method which the caller needs to treat specially in case a
     // race is lost.
     unsafe fn get(&self, i: int) -> T {
-        ptr::read(self.storage.offset(i & self.mask()))
+        ptr::read(self.elem(i))
     }
 
     // Unsafe because this unsafely overwrites possibly uninitialized or
     // initialized data.
     unsafe fn put(&self, i: int, t: T) {
-        let ptr = self.storage.offset(i & self.mask());
-        ptr::copy_nonoverlapping_memory(ptr as *mut T, &t as *T, 1);
-        forget(t);
+        overwrite(self.elem(i) as *mut T, t);
     }
 
     // Again, unsafe because this has incredibly dubious ownership violations.