1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use super::errors::*;
use parking_lot::{MappedMutexGuard, MappedRwLockReadGuard, MappedRwLockWriteGuard};
use std::any::{Any, TypeId};
use std::ops::{Deref, DerefMut};

///
/// The type erasure trait for `restor`.
///
/// This contains an interface for interacting with a `StorageUnit`
/// wrapper, and should be ignored by the end user.
///
/// Exposed here are two types:
/// - `Borrowed` which must deref to a `dyn Any`
///   - [`Ref`] in the case of `DynamicStorage`
///   - [`MappedRwLockReadGuard`] in the case of `RwLockStorage`
///   - [`MappedMutexGuard`] in the case of `MutexStorage`
/// - `MutBorrowed` which must deref_mut to a `dyn Any`
///   - [`RefMut`] in the case of `DynamicStorage`
///   - [`MappedRwLockWriteGuard`] in the case of `MutexStorage`
///   - [`MappedMutexGuard`] in the case of `MutexStorage`
///
/// [`Ref`]: https://doc.rust-lang.org/std/cell/struct.Ref.html
/// [`RefMut`]: https://doc.rust-lang.org/std/cell/struct.RefMut.html
///
/// [`MappedRwLockReadGuard`]: https://docs.rs/parking_lot/0.8.0/parking_lot/type.MappedRwLockReadGuard.html
/// [`MappedRwLockWriteGuard`]: https://docs.rs/parking_lot/0.8.0/parking_lot/type.MappedRwLockWriteGuard.html
///
/// [`MappedMutexGuard`]: https://docs.rs/parking_lot/0.8.0/parking_lot/type.MappedMutexGuard.html
///
pub trait Unit<'a> {
    type Borrowed: Deref<Target = dyn Any> + 'a;
    type MutBorrowed: Deref<Target = dyn Any> + DerefMut + 'a;
    ///
    /// Inserts an owned piece of data into storage, returning it if
    /// it cannot be inserted.
    ///
    fn insert_any(&self, new: Box<dyn Any>) -> Option<(Box<dyn Any>, ErrorDesc)>;

    ///
    /// Waits to insert a value into the storage.
    ///
    fn waiting_insert(&self, new: Box<dyn Any>) -> Option<(Box<dyn Any>, ErrorDesc)>
    where
        Self::Borrowed: Waitable,
        Self::MutBorrowed: Waitable;

    ///
    /// Returns an immutable lock to the internal `StorageUnit<T>`
    ///
    fn storage(&'a self) -> DynamicResult<Self::Borrowed>;
    ///
    /// Returns a mutable lock to the internal `StorageUnit<T>`
    ///
    fn storage_mut(&'a self) -> DynamicResult<Self::MutBorrowed>;

    fn waiting_storage(&'a self) -> Self::Borrowed
    where
        Self::Borrowed: Waitable;
    fn waiting_storage_mut(&'a self) -> Self::MutBorrowed
    where
        Self::MutBorrowed: Waitable;

    ///
    /// Returns the `TypeId` of the type of data contained in the
    /// `StorageUnit<T>` (So the `TypeId` of `T`).
    ///
    fn id(&self) -> TypeId;
}

pub trait Waitable {}

impl<'b, T: ?Sized> Waitable for MappedMutexGuard<'b, T> {}
impl<'b, T: ?Sized> Waitable for MappedRwLockReadGuard<'b, T> {}
impl<'b, T: ?Sized> Waitable for MappedRwLockWriteGuard<'b, T> {}

#[cfg(test)]
mod tests {
    use super::Unit;
    use crate::black_box::StorageUnit;
    use crate::concurrent_black_box::RwLockUnit;
    use std::any::TypeId;

    #[test]
    fn insert() {
        let storage = RwLockUnit::new(StorageUnit::<usize>::new());
        storage.insert_any(Box::new(0usize) as _);
        storage.insert_any(Box::new(vec![1usize, 2, 3, 4]));
        assert_eq!(
            storage.inner().read().many().unwrap(),
            &[0usize, 1, 2, 3, 4]
        );
    }

    #[test]
    fn waiting_insert() {
        let storage = RwLockUnit::new(StorageUnit::<usize>::new());
        storage.waiting_insert(Box::new(0usize) as _);
        storage.waiting_insert(Box::new(vec![1usize, 2, 3, 4]));
        assert_eq!(
            storage.inner().read().many().unwrap(),
            &[0usize, 1, 2, 3, 4]
        );
    }

    #[test]
    fn id() {
        let storage = RwLockUnit::new(StorageUnit::<usize>::new());
        assert_eq!(storage.id(), TypeId::of::<usize>());
    }

    #[test]
    fn eq() {
        let storage = RwLockUnit::new(StorageUnit::<usize>::new());
        let storage_2 = RwLockUnit::new(StorageUnit::<usize>::new());
        assert_eq!(storage.id(), storage_2.id());
    }
}