-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce method for moving entities between worlds
- Loading branch information
Showing
6 changed files
with
116 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
# Unreleased | ||
|
||
### Added | ||
- `World::take` for moving complete entities between worlds | ||
|
||
# 0.7.5 | ||
|
||
### Changed | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use alloc::vec::Vec; | ||
use core::mem; | ||
|
||
use crate::{Archetype, DynamicBundle, TypeInfo}; | ||
|
||
/// An entity removed from a `World` | ||
pub struct TakenEntity<'a> { | ||
archetype: &'a Archetype, | ||
} | ||
|
||
impl<'a> TakenEntity<'a> { | ||
/// # Safety | ||
/// `archetype` must have a valid entity stored past its end | ||
pub(crate) unsafe fn new(archetype: &'a Archetype) -> Self { | ||
Self { archetype } | ||
} | ||
} | ||
|
||
unsafe impl<'a> DynamicBundle for TakenEntity<'a> { | ||
fn with_ids<T>(&self, f: impl FnOnce(&[core::any::TypeId]) -> T) -> T { | ||
f(self.archetype.type_ids()) | ||
} | ||
|
||
fn type_info(&self) -> Vec<crate::TypeInfo> { | ||
self.archetype.types().to_vec() | ||
} | ||
|
||
unsafe fn put(self, mut f: impl FnMut(*mut u8, TypeInfo)) { | ||
let archetype = self.archetype; | ||
mem::forget(self); // Suppress Drop | ||
for &ty in archetype.types() { | ||
let ptr = archetype | ||
.get_dynamic(ty.id(), ty.layout().size(), archetype.len()) | ||
.unwrap(); | ||
f(ptr.as_ptr(), ty) | ||
} | ||
} | ||
} | ||
|
||
impl Drop for TakenEntity<'_> { | ||
fn drop(&mut self) { | ||
for &ty in self.archetype.types() { | ||
unsafe { | ||
let ptr = self | ||
.archetype | ||
.get_dynamic(ty.id(), ty.layout().size(), self.archetype.len()) | ||
.unwrap(); | ||
ty.drop(ptr.as_ptr()); | ||
} | ||
} | ||
} | ||
} |
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