-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add ChangeSet that can be collected into from iterators, and joined over for easy application to components #344
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
use hibitset::BitSet; | ||
use std::iter::FromIterator; | ||
use std::ops::AddAssign; | ||
|
||
use join::Join; | ||
use storage::{DenseVecStorage, UnprotectedStorage}; | ||
use world::{Entity, Index}; | ||
|
||
/// Change set that can be collected from an iterator, and joined on for easy application to | ||
/// components. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust | ||
/// # extern crate specs; | ||
/// # use specs::prelude::*; | ||
/// | ||
/// pub struct Health(i32); | ||
/// | ||
/// impl Component for Health { | ||
/// type Storage = DenseVecStorage<Self>; | ||
/// } | ||
/// | ||
/// # fn main() { | ||
/// # let mut world = World::new(); | ||
/// # world.register::<Health>(); | ||
/// | ||
/// let a = world.create_entity().with(Health(100)).build(); | ||
/// let b = world.create_entity().with(Health(200)).build(); | ||
/// | ||
/// let changeset = [(a, 32), (b, 12), (b, 13)] | ||
/// .iter() | ||
/// .cloned() | ||
/// .collect::<ChangeSet<i32>>(); | ||
/// for (health, modifier) in (&mut world.write::<Health>(), &changeset).join() { | ||
/// health.0 -= modifier; | ||
/// } | ||
/// # } | ||
/// ``` | ||
pub struct ChangeSet<T> { | ||
mask: BitSet, | ||
inner: DenseVecStorage<T>, | ||
} | ||
|
||
impl<T> ChangeSet<T> { | ||
/// Create a new change set | ||
pub fn new() -> Self { | ||
ChangeSet { | ||
mask: BitSet::default(), | ||
inner: DenseVecStorage::default(), | ||
} | ||
} | ||
|
||
/// Add a value to the change set. If the entity already have a value in the change set, the | ||
/// incoming value will be added to that. | ||
pub fn add(&mut self, entity: Entity, value: T) | ||
where | ||
T: AddAssign, | ||
{ | ||
if self.mask.contains(entity.id()) { | ||
unsafe { | ||
*self.inner.get_mut(entity.id()) += value; | ||
} | ||
} else { | ||
unsafe { | ||
self.inner.insert(entity.id(), value); | ||
} | ||
self.mask.add(entity.id()); | ||
} | ||
} | ||
|
||
/// Clear the changeset | ||
pub fn clear(&mut self) { | ||
for id in &self.mask { | ||
unsafe { | ||
self.inner.remove(id); | ||
} | ||
} | ||
self.mask.clear(); | ||
} | ||
} | ||
|
||
impl<T> FromIterator<(Entity, T)> for ChangeSet<T> | ||
where | ||
T: AddAssign, | ||
{ | ||
fn from_iter<I: IntoIterator<Item = (Entity, T)>>(iter: I) -> Self { | ||
let mut changeset = ChangeSet::new(); | ||
for (entity, d) in iter { | ||
changeset.add(entity, d); | ||
} | ||
changeset | ||
} | ||
} | ||
|
||
impl<T> Extend<(Entity, T)> for ChangeSet<T> | ||
where | ||
T: AddAssign, | ||
{ | ||
fn extend<I: IntoIterator<Item = (Entity, T)>>(&mut self, iter: I) { | ||
for (entity, d) in iter { | ||
self.add(entity, d); | ||
} | ||
} | ||
} | ||
|
||
impl<'a, T> Join for &'a mut ChangeSet<T> { | ||
type Type = &'a mut T; | ||
type Value = &'a mut DenseVecStorage<T>; | ||
type Mask = &'a BitSet; | ||
|
||
fn open(self) -> (Self::Mask, Self::Value) { | ||
(&self.mask, &mut self.inner) | ||
} | ||
|
||
unsafe fn get(v: &mut Self::Value, id: Index) -> Self::Type { | ||
let value: *mut Self::Value = v as *mut Self::Value; | ||
(*value).get_mut(id) | ||
} | ||
} | ||
|
||
impl<'a, T> Join for &'a ChangeSet<T> { | ||
type Type = &'a T; | ||
type Value = &'a DenseVecStorage<T>; | ||
type Mask = &'a BitSet; | ||
|
||
fn open(self) -> (Self::Mask, Self::Value) { | ||
(&self.mask, &self.inner) | ||
} | ||
|
||
unsafe fn get(value: &mut Self::Value, id: Index) -> Self::Type { | ||
value.get(id) | ||
} | ||
} | ||
|
||
impl<T> Join for ChangeSet<T> { | ||
type Type = T; | ||
type Value = DenseVecStorage<T>; | ||
type Mask = BitSet; | ||
|
||
fn open(self) -> (Self::Mask, Self::Value) { | ||
(self.mask, self.inner) | ||
} | ||
|
||
unsafe fn get(value: &mut Self::Value, id: Index) -> Self::Type { | ||
value.remove(id) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::ChangeSet; | ||
use join::Join; | ||
use storage::DenseVecStorage; | ||
use world::{Component, World}; | ||
|
||
pub struct Health(i32); | ||
|
||
impl Component for Health { | ||
type Storage = DenseVecStorage<Self>; | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
let mut world = World::new(); | ||
world.register::<Health>(); | ||
|
||
let a = world.create_entity().with(Health(100)).build(); | ||
let b = world.create_entity().with(Health(200)).build(); | ||
let c = world.create_entity().with(Health(300)).build(); | ||
|
||
let changeset = [(a, 32), (b, 12), (b, 13)] | ||
.iter() | ||
.cloned() | ||
.collect::<ChangeSet<i32>>(); | ||
for (health, modifier) in (&mut world.write::<Health>(), &changeset).join() { | ||
health.0 -= modifier; | ||
} | ||
let healths = world.read::<Health>(); | ||
assert_eq!(68, healths.get(a).unwrap().0); | ||
assert_eq!(175, healths.get(b).unwrap().0); | ||
assert_eq!(300, healths.get(c).unwrap().0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is taken from the Join on &mut Storage, it's required for the compiler to understand that v is mutable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It fails to validate the lifetime without it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The basic issue is that it fails to match &mut on the function signature to &'a for the Join implementation, and it's not possible to set &'a on the function signature because it would not match the trait then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, right.