heaparray/traits/
array_ref.rs

1use super::LabelledArray;
2// use core::sync::atomic::Ordering;
3
4/// A reference to an array, whose clone points to the same data.
5///
6/// Allows for idiomatic cloning of array references:
7///
8/// ```rust
9/// use heaparray::naive_rc::*;
10/// let array_ref = FpRcArray::new(10, |_| 0);
11/// let another_ref = ArrayRef::clone(&array_ref);
12///
13/// assert!(array_ref.len() == another_ref.len());
14/// for i in 0..another_ref.len() {
15///     let r1 = &array_ref[i] as *const i32;
16///     let r2 = &another_ref[i] as *const i32;
17///     assert!(r1 == r2);
18/// }
19/// ```
20pub trait ArrayRef: Clone + Sized {
21    /// Clones the array reference. Internally just calls its `.clone()`
22    /// method.
23    fn clone(ptr: &Self) -> Self {
24        ptr.clone()
25    }
26}
27
28/// Array with optional label struct stored next to the data that can
29/// be conditionally mutated.
30pub trait LabelledArrayRefMut<E, L>: LabelledArray<E, L> {
31    /// Get mutable reference to the label.
32    fn get_label_mut(&mut self) -> Option<&mut L>;
33}
34
35/*
36/// Atomically modified array reference.
37///
38/// Guarrantees that all operations on the
39/// array reference are atomic (i.e. all changes to the internal array pointer).
40/// Additionally, guarrantees that all reads of the value of the internal pointer
41/// are done with atomic loads.
42///
43/// For more details on the expected behavior of these methods, see the
44/// documentation for `core::sync::atomic::AtomicPtr`.
45pub trait AtomicArrayRef: Sized {
46    fn as_ref(&self) -> usize;
47    /// Returns the previous value, and also the struct you passed in if the value
48    /// wasn't updated
49    fn compare_and_swap(
50        &self,
51        current: usize,
52        new: Self,
53        order: Ordering,
54    ) -> Result<usize, (Self, usize)>;
55    /// Returns the previous value, and also the struct you passed in if the value
56    /// wasn't updated
57    fn compare_exchange(
58        &self,
59        current: usize,
60        new: Self,
61        success: Ordering,
62        failure: Ordering,
63    ) -> Result<usize, (Self, usize)>;
64    /// Swaps in the passed-in reference if the internal reference matches `current`.
65    /// Returns the previous value, and also the struct you passed in if the value
66    /// wasn't updated
67    fn compare_exchange_weak(
68        &self,
69        current: usize,
70        new: Self,
71        success: Ordering,
72        failure: Ordering,
73    ) -> Result<usize, (Self, usize)>;
74    /// Swaps in the specified array reference and returns the previous value
75    fn swap(&self, ptr: Self, order: Ordering) -> Self;
76}*/