append-only-vec
Advanced tools
| { | ||
| "git": { | ||
| "sha1": "969572f1ef4c42fddaff572f07d509598042b341" | ||
| "sha1": "d991d30ecde2d25b3b7b2293eef3b28e592dfcc1" | ||
| }, | ||
| "path_in_vcs": "" | ||
| } |
+1
-1
@@ -15,3 +15,3 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO | ||
| name = "append-only-vec" | ||
| version = "0.1.4" | ||
| version = "0.1.5" | ||
| authors = ["David Roundy <daveroundy@gmail.com>"] | ||
@@ -18,0 +18,0 @@ description = "Append-only, concurrent vector" |
+33
-2
@@ -38,3 +38,3 @@ //! AppendOnlyVec | ||
| use std::cell::UnsafeCell; | ||
| use std::ops::Index; | ||
| use std::ops::{Index, IndexMut}; | ||
| use std::sync::atomic::AtomicUsize; | ||
@@ -217,3 +217,3 @@ use std::sync::atomic::Ordering; | ||
| // Copy the element value. The copy remaining in the array must not | ||
| // Copy the element value. The copy remaining in the array must not | ||
| // be used again (i.e. make sure we do not drop it) | ||
@@ -240,2 +240,3 @@ let value = unsafe { ptr.add(offset).read() }; | ||
| } | ||
| impl<T> Index<usize> for AppendOnlyVec<T> { | ||
@@ -256,2 +257,18 @@ type Output = T; | ||
| impl<T> IndexMut<usize> for AppendOnlyVec<T> { | ||
| fn index_mut(&mut self, idx: usize) -> &mut Self::Output { | ||
| assert!(idx < self.len()); // this includes the required ordering memory barrier | ||
| let (array, offset) = indices(idx); | ||
| // The ptr value below is safe, because the length check above will | ||
| // ensure that the data we want is already visible, since it used | ||
| // Ordering::Acquire on `self.count` which synchronizes with the | ||
| // Ordering::Release write in `self.push`. | ||
| let ptr = unsafe { *self.data[array as usize].get() }; | ||
| // `&mut` is safe because there can be no access to data owned by | ||
| // `self` except via `self`, and we have `&mut` on `self` | ||
| unsafe { &mut *ptr.add(offset) } | ||
| } | ||
| } | ||
| impl<T> Drop for AppendOnlyVec<T> { | ||
@@ -387,1 +404,15 @@ fn drop(&mut self) { | ||
| } | ||
| #[test] | ||
| fn test_push_then_index_mut() { | ||
| let mut v = AppendOnlyVec::<usize>::new(); | ||
| for i in 0..1024 { | ||
| v.push(i); | ||
| } | ||
| for i in 0..1024 { | ||
| v[i] += i; | ||
| } | ||
| for i in 0..1024 { | ||
| assert_eq!(v[i], 2 * i); | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet