Skip to content

Commit

Permalink
Fix errors with tests, impl HasMeta for Self
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceeri committed Nov 20, 2017
1 parent 203eda5 commit 1be3ff6
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 94 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ required-features = ["serde"]
[[example]]
name = "meta"

[[example]]
name = "meta_test"

[workspace]
members = ["specs-derive"]

Binary file removed examples/.async.rs.swp
Binary file not shown.
46 changes: 3 additions & 43 deletions examples/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ extern crate shred;
extern crate shred_derive;
extern crate specs;

use specs::{Component, DenseVecStorage, DispatcherBuilder, Entities, Entity, HashMapStorage, Index,
Join, Metadata, ParJoin, ReadStorage, System, VecStorage, World, WriteStorage};
use specs::{Component, DenseVecStorage, DispatcherBuilder, Entities, Entity, HashMapStorage,
Join, ParJoin, ReadStorage, System, VecStorage, World, WriteStorage};

// -- Components --
// A component exists for 0..n
Expand All @@ -18,31 +18,7 @@ impl Component for CompInt {
// Storage is used to store all data for components of this type
// VecStorage is meant to be used for components that are in almost every entity
type Storage = VecStorage<Self>;
type Metadata = (Test, Test2, Test3);
}

#[derive(Debug, Default)]
struct Test { inc: u32 }
impl<T> Metadata<T> for Test {
fn insert(&mut self, _: Index, _: &T) {
self.inc += 1;
}
}

#[derive(Debug, Default)]
struct Test2 { inc: u32 }
impl<T> Metadata<T> for Test2 {
fn remove(&mut self, _: Index, _: &T) {
self.inc += 1;
}
}

#[derive(Debug, Default)]
struct Test3 { inc: u32 }
impl<T> Metadata<T> for Test3 {
fn get_mut(&mut self, _: Index, _: &mut T) {
self.inc += 1;
}
type Metadata = ();
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -253,20 +229,4 @@ fn main() {

dispatcher.dispatch(&w.res);
w.maintain();

{
let i = w.read::<CompInt>();
let test = i.meta::<Test>();
let test2 = i.meta::<Test2>();
let test3 = i.meta::<Test3>();

println!("{:?} {:?} {:?}", test, test2, test3);
}

{
let mut i = w.write::<CompInt>();
let mut test = i.mut_meta::<Test>();

println!("{:?}", test);
}
}
82 changes: 82 additions & 0 deletions examples/meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

extern crate specs;
#[macro_use]
extern crate specs_derive;

use specs::*;

#[derive(Default, Metadata)]
struct Other {
flagged: Flagged,
}

#[derive(Default, Metadata)]
struct TestMetadata {
flagged: Flagged,
other: Other,
}

#[derive(Default, Metadata)]
struct TupleMetadata(Flagged, Other);

#[derive(Debug, Default)]
struct TestComp;
impl Component for TestComp {
type Storage = DenseVecStorage<Self>;
type Metadata = TupleMetadata;
}

fn main() {
let mut world = World::new();
world.register::<TestComp>();

let e1 = world.create_entity()
.with(TestComp)
.build();
let _e2 = world.create_entity()
.with(TestComp)
.build();
let _e3 = world.create_entity()
.with(TestComp)
.build();
let e4 = world.create_entity()
.with(TestComp)
.build();
{
let mut storage = world.write::<TestComp>();
storage.find_mut::<Flagged>().clear_flags();
storage.get_mut(e1);
storage.get_mut(e4);
}

{
let storage = world.read::<TestComp>();

// Grab the metadata.
let flagged = storage.find::<Flagged>();

for (entity, comp, _) in (&*world.entities(), &storage, flagged).join() {
println!("({:?}, {:?}): {:?}", entity.id(), entity.gen().id(), comp);
}
}

{
let mut storage = world.write::<TestComp>();
storage.find::<Flagged>();
let _inner_flagged: &mut Flagged = storage.find_mut::<Other>().find_mut();
}

{
let mut storage = world.write::<TestComp>();

{
// Grab the metadata and clone it to avoid borrow issues
let flagged = storage.find::<Flagged>().clone();

// Iterate over all flagged components
for (entity, comp, _) in (&*world.entities(), &mut storage, flagged).join() {
println!("({:?}, {:?}): {:?}", entity.id(), entity.gen().id(), comp);
}
}
}
}
9 changes: 9 additions & 0 deletions specs-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ fn impl_metadata(ast: &DeriveInput) -> Tokens {
}
}

impl<#impl_generics> ::specs::HasMeta<Self> for #name #ty_generics #where_clause {
fn find(&self) -> &Self {
self
}
fn find_mut(&mut self) -> &mut Self {
self
}
}

#(
impl<#impl_generics_list> ::specs::HasMeta<#field_ty> for #name_list #ty_generics_list #where_clause_list {
fn find(&self) -> &#field_ty_2 {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
//!
//! impl Component for MyComp {
//! type Storage = VecStorage<Self>;
//! type Metadata = ();
//! }
//! ```
//!
Expand Down Expand Up @@ -260,7 +261,7 @@ pub use storage::{MergeError, PackedData};
/// use specs::{Entities, Join};
///
/// # use specs::{Component, VecStorage, World};
/// # struct Pos; impl Component for Pos { type Storage = VecStorage<Self>; }
/// # struct Pos; impl Component for Pos { type Storage = VecStorage<Self>; type Metadata = (); }
/// # let mut world = World::new(); world.register::<Pos>();
/// # let entities = world.entities(); let positions = world.read::<Pos>();
/// for (e, pos) in (&*entities, &positions).join() {
Expand Down
58 changes: 31 additions & 27 deletions src/storage/flagged.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

use hibitset::BitSet;

use {Index, Join, Metadata};
use {Index, Join, HasMeta, Metadata};
use world::EntityIndex;

/// Wrapper storage that stores modifications to components in a bitset.
Expand All @@ -21,11 +21,6 @@ use world::EntityIndex;
///
/// use specs::{Component, Entities, Flagged, Join, System, DenseVecStorage, WriteStorage};
///
/// #[derive(Metadata)]
/// pub struct CompMetadata {
/// pub flagged: Flagged,
/// }
///
/// pub struct Comp(u32);
/// impl Component for Comp {
/// type Storage = DenseVecStorage<Self>;
Expand Down Expand Up @@ -70,7 +65,7 @@ use world::EntityIndex;
/// }
///
/// // To iterate over the flagged/modified components:
/// for flagged_comp in (comps.meta().flagged).join() {
/// for (flagged_comp, _) in (&comps, comps.find::<Flagged>()).join() {
/// // ...
/// }
///
Expand All @@ -85,24 +80,6 @@ pub struct Flagged {
mask: BitSet,
}

impl<T> Metadata<T> for Flagged {
fn clean<F>(&mut self, _: &F)
where
F: Fn(Index) -> bool
{
self.mask.clear();
}
fn get_mut(&mut self, id: Index, _: &mut T) {
self.mask.add(id);
}
fn insert(&mut self, id: Index, _: &T) {
self.mask.add(id);
}
fn remove(&mut self, id: Index, _: &T) {
self.mask.remove(id);
}
}

impl Flagged {
/// Whether the component that belongs to the given entity was flagged or not.
pub fn flagged<E: EntityIndex>(&self, entity: E) -> bool {
Expand All @@ -125,14 +102,41 @@ impl Flagged {
}
}

impl<T> Metadata<T> for Flagged {
fn clean<F>(&mut self, _: &F)
where
F: Fn(Index) -> bool
{
self.mask.clear();
}
fn get_mut(&mut self, id: Index, _: &mut T) {
self.mask.add(id);
}
fn insert(&mut self, id: Index, _: &T) {
self.mask.add(id);
}
fn remove(&mut self, id: Index, _: &T) {
self.mask.remove(id);
}
}

impl HasMeta<Self> for Flagged {
fn find(&self) -> &Self {
self
}
fn find_mut(&mut self) -> &mut Self {
self
}
}

impl<'a> Join for &'a Flagged {
type Type = ();
type Value = ();
type Mask = &'a BitSet;
fn open(self) -> (Self::Mask, Self::Value) {
(&self.mask, ())
}
unsafe fn get(v: &mut Self::Value, id: Index) -> Self::Type {
unsafe fn get(_: &mut Self::Value, _: Index) -> Self::Type {
()
}
}
Expand All @@ -144,7 +148,7 @@ impl Join for Flagged {
fn open(self) -> (Self::Mask, Self::Value) {
(self.mask, ())
}
unsafe fn get(v: &mut Self::Value, id: Index) -> Self::Type {
unsafe fn get(_: &mut Self::Value, _: Index) -> Self::Type {
()
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/storage/meta.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@

use std::ops::{Deref, DerefMut};

use hibitset::{BitSet, BitSetAnd, BitSetLike};

use storage::{MaskedStorage, Storage, WrappedStorage, UnprotectedStorage};
use std::marker::PhantomData;

use Index;

Expand Down Expand Up @@ -36,4 +32,5 @@ pub trait HasMeta<M> {
}

impl<T> Metadata<T> for () { }
impl<T> Metadata<T> for PhantomData<T> { }

12 changes: 9 additions & 3 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ impl<T: Component> Drop for MaskedStorage<T> {
}
}

/// Wraps the Component's `Storage` and `Metadata` together.
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
/// Associates the component's storage with its metadata.
pub struct WrappedStorage<T: Component> {
inner: T::Storage,
meta: T::Metadata,
Expand Down Expand Up @@ -251,14 +251,17 @@ where
self.data.mask.clone()
}

/*
pub fn meta(&self) -> &T::Metadata {
&self.data.wrapped.meta
}
*/

pub fn find<M>(&self) -> &M
where T::Metadata: HasMeta<M>,
{
self.meta().find()
self.data.wrapped.meta.find()
//self.meta().find()
}
}

Expand Down Expand Up @@ -471,14 +474,17 @@ where
}
}

/*
pub fn meta_mut(&mut self) -> &mut T::Metadata {
&mut self.data.wrapped.meta
}
*/

pub fn find_mut<M>(&mut self) -> &mut M
where T::Metadata: HasMeta<M>,
{
self.meta_mut().find_mut()
self.data.wrapped.meta.find_mut()
//self.meta_mut().find_mut()
}
}

Expand Down
Loading

0 comments on commit 1be3ff6

Please sign in to comment.