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
118
119
120
121
122
123
124
125
126
127
128
use shine_stdext::unnamedstore::Store as InnerStore;
use shred::{Read, ResourceId, SystemData, Write};
use std::ops::{Deref, DerefMut};

pub use shine_stdext::unnamedstore::{Index, ReadGuard, WriteGuard};

/// A thin wrapper for [InnerStore](InnerStore) to make it more ergonomic to the world.
pub struct Store<D> {
    inner: InnerStore<D>,
}

impl<D> Store<D> {
    pub fn new() -> Store<D> {
        Store {
            inner: InnerStore::new(),
        }
    }

    /// Creates a new store with memory allocated for at least capacity items
    pub fn new_with_capacity(page_size: usize, capacity: usize) -> Store<D> {
        Store {
            inner: InnerStore::new_with_capacity(page_size, capacity),
        }
    }

    pub fn read(&self) -> ReadGuard<'_, D> {
        self.inner.try_read().unwrap()
    }

    pub fn write(&mut self) -> WriteGuard<'_, D> {
        self.inner.try_write().unwrap()
    }
}

impl<D> Default for Store<D> {
    fn default() -> Self {
        Store::new()
    }
}

/// Grant immutable access to a (unnamed) store inside a System
pub struct ReadStore<'a, D>
where
    D: 'static,
{
    inner: Read<'a, Store<D>>,
}

impl<'a, D> Deref for ReadStore<'a, D>
where
    D: 'static,
{
    type Target = Store<D>;

    fn deref(&self) -> &Self::Target {
        self.inner.deref()
    }
}

impl<'a, D> SystemData<'a> for ReadStore<'a, D>
where
    D: 'static,
{
    fn setup(_: &mut shred::World) {}

    fn fetch(res: &'a shred::World) -> Self {
        ReadStore {
            inner: res.fetch::<Store<D>>().into(),
        }
    }

    fn reads() -> Vec<ResourceId> {
        vec![ResourceId::new::<Store<D>>()]
    }

    fn writes() -> Vec<ResourceId> {
        vec![]
    }
}

/// Grant mutable access to a (unnamed) store inside a System
pub struct WriteStore<'a, D>
where
    D: 'static,
{
    inner: Write<'a, Store<D>>,
}

impl<'a, D> Deref for WriteStore<'a, D>
where
    D: 'static,
{
    type Target = Store<D>;

    fn deref(&self) -> &Self::Target {
        self.inner.deref()
    }
}

impl<'a, D> DerefMut for WriteStore<'a, D>
where
    D: 'static,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.inner.deref_mut()
    }
}

impl<'a, D> SystemData<'a> for WriteStore<'a, D>
where
    D: 'static,
{
    fn setup(_: &mut shred::World) {}

    fn fetch(res: &'a shred::World) -> Self {
        WriteStore {
            inner: res.fetch_mut::<Store<D>>().into(),
        }
    }

    fn reads() -> Vec<ResourceId> {
        vec![]
    }

    fn writes() -> Vec<ResourceId> {
        vec![ResourceId::new::<Store<D>>()]
    }
}