Skip to content

Commit

Permalink
Merge #306
Browse files Browse the repository at this point in the history
306: Immutable Restricted Storage r=Aceeri a=Aceeri

fixes #295

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/slide-rs/specs/306)
<!-- Reviewable:end -->
  • Loading branch information
bors[bot] committed Dec 12, 2017
2 parents d77a982 + 9e384ec commit 8408ef9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/storage/flagged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use world::EntityIndex;
/// }
///
/// // Or alternatively:
/// for (entity, (mut entry, mut restrict)) in (&*entities, &mut comps.restrict()).join() {
/// for (entity, (mut entry, mut restrict)) in (&*entities, &mut comps.restrict_mut()).join() {
/// if true { // check whether this component should be modified.
/// let mut comp = restrict.get_mut_unchecked(&mut entry);
/// // ...
Expand Down
40 changes: 36 additions & 4 deletions src/storage/restrict.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::{Borrow, BorrowMut};
use std::fmt;
use std::marker::PhantomData;
use std::ops::DerefMut;
use std::ops::{Deref, DerefMut};

use hibitset::BitSet;

Expand Down Expand Up @@ -37,7 +37,7 @@ pub enum ParallelRestriction {}
/// fn run(&mut self, (entities, mut some_comps): Self::SystemData) {
/// for (entity, (mut entry, restricted)) in (
/// &*entities,
/// &mut some_comps.restrict()
/// &mut some_comps.restrict_mut()
/// ).join() {
/// // Check if the reference is fine to mutate.
/// if restricted.get_unchecked(&entry).0 < 5 {
Expand Down Expand Up @@ -70,6 +70,15 @@ where
{
}

unsafe impl<'rf, 'st: 'rf, B, T, R> ParJoin
for &'rf RestrictedStorage<'rf, 'st, B, T, R, ParallelRestriction>
where
T: Component,
R: Borrow<T::Storage> + 'rf,
B: Borrow<BitSet> + 'rf,
{
}

impl<'rf, 'st, B, T, R, RT> RestrictedStorage<'rf, 'st, B, T, R, RT>
where
T: Component,
Expand Down Expand Up @@ -177,6 +186,29 @@ where
}
}


impl<'st, T, D> Storage<'st, T, D>
where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
{
/// Builds an immutable `RestrictedStorage` out of a `Storage`. Allows deferred
/// unchecked access to the entity's component.
///
/// This is returned as a `ParallelRestriction` version since you can only get
/// immutable components with this which is safe for parallel by default.
pub fn restrict<'rf>(
&'rf self,
) -> RestrictedStorage<'rf, 'st, &BitSet, T, &T::Storage, ParallelRestriction> {
RestrictedStorage {
bitset: &self.data.mask,
data: &self.data.inner,
entities: &self.entities,
phantom: PhantomData,
}
}
}

impl<'st, T, D> Storage<'st, T, D>
where
T: Component,
Expand All @@ -185,7 +217,7 @@ where
/// Builds a mutable `RestrictedStorage` out of a `Storage`. Allows restricted
/// access to the inner components without allowing invalidating the
/// bitset for iteration in `Join`.
pub fn restrict<'rf>(
pub fn restrict_mut<'rf>(
&'rf mut self,
) -> RestrictedStorage<'rf, 'st, &BitSet, T, &mut T::Storage, NormalRestriction> {
let (mask, data) = self.data.open_mut();
Expand All @@ -200,7 +232,7 @@ where
/// Builds a mutable, parallel `RestrictedStorage`,
/// does not allow mutably getting other components
/// aside from the current iteration.
pub fn par_restrict<'rf>(
pub fn par_restrict_mut<'rf>(
&'rf mut self,
) -> RestrictedStorage<'rf, 'st, &BitSet, T, &mut T::Storage, ParallelRestriction> {
let (mask, data) = self.data.open_mut();
Expand Down
8 changes: 4 additions & 4 deletions src/storage/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ mod test {
components.insert(c);
}

for (entry, restricted) in (&mut s1.restrict()).join() {
for (entry, restricted) in (&mut s1.restrict_mut()).join() {
let c1 = { restricted.get_unchecked(&entry).0 };

let c2 = { restricted.get_mut_unchecked(&entry).0 };
Expand Down Expand Up @@ -537,7 +537,7 @@ mod test {
let components2 = Mutex::new(Vec::new());
let components2_mut = Mutex::new(Vec::new());

(&mut s1.par_restrict())
(&mut s1.par_restrict_mut())
.par_join()
.for_each(|(entry, restricted)| {
let (mut components2, mut components2_mut) =
Expand Down Expand Up @@ -654,7 +654,7 @@ mod test {
s1.insert(Entity::new(i, Generation::new(1)), (i + 10).into());
s2.insert(Entity::new(i, Generation::new(1)), (i + 10).into());
}
for ((s1_entry, _), (_, s2_restricted)) in (&mut s1.restrict(), &mut s2.restrict()).join() {
for ((s1_entry, _), (_, s2_restricted)) in (&mut s1.restrict_mut(), &mut s2.restrict_mut()).join() {
// verify that the assert fails if the storage is not the original.
s2_restricted.get_unchecked(&s1_entry);
}
Expand All @@ -677,7 +677,7 @@ mod test {
s1.insert(Entity::new(i, Generation::new(1)), (i + 10).into());
s2.insert(Entity::new(i, Generation::new(1)), (i + 10).into());
}
(&mut s1.par_restrict(), &mut s2.par_restrict())
(&mut s1.par_restrict_mut(), &mut s2.par_restrict_mut())
.par_join()
.for_each(|((s1_entry, _), (_, s2_restricted))| {
// verify that the assert fails if the storage is not the original.
Expand Down

0 comments on commit 8408ef9

Please sign in to comment.