Skip to content

Commit

Permalink
feat: Entity bit interpretation
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Oct 5, 2024
1 parent 9b56662 commit f561d7c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
9 changes: 0 additions & 9 deletions benches/common/heavy_compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ component! {
velocity: Vec3,
}

#[derive(Copy, Clone)]
struct Position(Vec3);

#[derive(Copy, Clone)]
struct Rotation(Vec3);

#[derive(Copy, Clone)]
struct Velocity(Vec3);

pub struct Benchmark(World);

impl Benchmark {
Expand Down
5 changes: 2 additions & 3 deletions benches/common/simple_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ component! {
velocity: Vec3,
}

pub struct Benchmark(World, Query<(Component<Vec3>, Mutable<Vec3>)>);
pub struct Benchmark(World);

impl Benchmark {
pub fn new() -> Self {
Expand All @@ -27,8 +27,7 @@ impl Benchmark {
batch.set(velocity(), repeat(Vec3::X)).unwrap();

batch.spawn(&mut world);
let query = Query::new((velocity(), position().as_mut()));
Self(world, query)
Self(world)
}

pub fn run(&mut self) {
Expand Down
32 changes: 32 additions & 0 deletions src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ impl Entity {
pub fn kind(&self) -> EntityKind {
self.kind
}

/// Returns a bit representation of the entity
pub fn as_bits(&self) -> u64 {
let index = self.index as u64;
let gen = self.gen.get() as u64;
let kind = self.kind.bits() as u64;

(index << 32) | (gen << 16) | kind
}

/// Reconstruct an entity from its bit representation
pub fn try_from_bits(mut bits: u64) -> Option<Self> {
let kind = EntityKind::from_bits(bits as u16).unwrap();
bits >>= 16;

let gen = EntityGen::new(bits as u16).unwrap();
bits >>= 16;

let index = bits as u32;

Some(Entity { index, gen, kind })
}
}

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -300,4 +322,14 @@ mod tests {
assert_eq!(align_of::<Entity>(), 4);
assert_eq!(size_of::<Option<Entity>>(), 8);
}

#[test]
fn test_bitcasts() {
let mut store = EntityStore::new(EntityKind::COMPONENT);

let a = store.spawn("a");

let a2 = Entity::try_from_bits(a.as_bits()).unwrap();
assert_eq!(a, a2);
}
}
3 changes: 2 additions & 1 deletion src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod component;
mod component_mut;
mod copied;
mod entity_ref;
pub mod expect;
mod expect;
mod ext;
mod map;
mod maybe_mut;
Expand Down Expand Up @@ -33,6 +33,7 @@ pub use component::*;
pub use component_mut::*;
pub use copied::*;
pub use entity_ref::*;
pub use expect::*;
pub use ext::FetchExt;
pub use map::Map;
pub use maybe_mut::{MaybeMut, MutGuard};
Expand Down
5 changes: 1 addition & 4 deletions src/system/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ use crate::{
use super::{input::ExtractDyn, SystemAccess, SystemData};

/// A resource that can be shared between systems
/// The difference between this and an `Arc<Mutex<_>>` is that this will be
/// taken into consideration when multithreading in the schedule, and will as
/// such not require locks.
///
/// The implementation is an `Arc<AtomicRefCell>` and is thus cheap to clone
/// Used for safe multithreaded scheduling
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct SharedResource<T>(Arc<AtomicRefCell<T>>);

Expand Down

0 comments on commit f561d7c

Please sign in to comment.