Skip to content

Commit

Permalink
update unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
Nertsal committed Nov 6, 2024
1 parent 5b1a86f commit 040be86
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
11 changes: 5 additions & 6 deletions src/storage/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ impl<K: slotmap::Key, T> Default for Arena<T, K> {
}
}

unsafe impl<K: slotmap::Key, T> Storage<T> for Arena<T, K> {
impl<K: slotmap::Key, T> Storage<T> for Arena<T, K> {
type Family = ArenaFamily<K>;
type Id = K;
// fn ids(&self) -> impl Iterator<Item = Self::Id> + Clone {
// // SAFETY: `keys()` guarantees validity and uniqueness
// self.keys()
// }
fn insert(&mut self, id: Self::Id, value: T) {
self.0.insert(id, value);
}
Expand Down Expand Up @@ -75,9 +71,12 @@ impl<K: slotmap::Key> Default for ArenaIdGenerator<K> {
}
}

impl<K: slotmap::Key> IdGenerator for ArenaIdGenerator<K> {
unsafe impl<K: slotmap::Key> IdGenerator for ArenaIdGenerator<K> {
type Id = K;
fn ids(&self) -> impl Iterator<Item = Self::Id> + Clone {
// SAFETY: `keys()` guarantees uniqueness and partially validity;
// proper validity is dependent on the derived implementation of Archetype::insert
// passing the generated id's to the storages below.
self.alive.keys()
}
fn spawn(&mut self) -> Self::Id {
Expand Down
8 changes: 5 additions & 3 deletions src/storage/hashstorage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ impl<T> Default for HashStorage<T> {
}
}

unsafe impl<T> Storage<T> for HashStorage<T> {
impl<T> Storage<T> for HashStorage<T> {
type Family = HashFamily;
type Id = Id;
// fn ids(&self) -> impl Iterator<Item = Self::Id> + Clone {
// // SAFETY: `keys()` guarantees validity and uniqueness
// self.inner.keys().copied()
// }
fn insert(&mut self, id: Self::Id, value: T) {
Expand Down Expand Up @@ -80,9 +79,12 @@ impl Default for HashIdGenerator {
}
}

impl IdGenerator for HashIdGenerator {
unsafe impl IdGenerator for HashIdGenerator {
type Id = Id;
fn ids(&self) -> impl Iterator<Item = Self::Id> + Clone {
// SAFETY: `iter()` guarantees uniqueness and partially validity;
// proper validity is dependent on the derived implementation of Archetype::insert
// passing the generated id's to the storages below.
self.alive.iter().copied()
}
fn spawn(&mut self) -> Self::Id {
Expand Down
19 changes: 8 additions & 11 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ pub mod hashstorage;
// pub mod vec;

/// A storage of components.
///
/// # Safety
/// The [Storage::ids] method must return an iterator of unique and valid id's.
/// That is, they must not repeat, and must correspond to valid entities when
/// used in [Storage::get] or [Storage::get_mut] (unless removed).
///
// TODO: check safety
pub unsafe trait Storage<T>: Default {
pub trait Storage<T>: Default {
/// Type of the abstract family corresponding to the storages of this type.
type Family: StorageFamily;
/// Type of the identifier used for components/entities.
Expand Down Expand Up @@ -62,15 +55,19 @@ pub trait StorageFamily {
}

/// A generator of identifiers to use with [Storage]s.
// TODO: unsafe?
pub trait IdGenerator {
///
/// # Safety
/// The [`IdGenerator::ids`] method must return an iterator of unique and valid id's.
/// That is, they must not repeat, and must correspond to valid entities when
/// used in [`Storage::get`] or [`Storage::get_mut`] (unless removed).
///
pub unsafe trait IdGenerator {
/// The identifier type being generated.
type Id: Copy;

/// Returns the unique id's of all active entities in the storage in an arbitrary order.
///
/// **Note**: [`Clone`](trait@std::clone::Clone) is constrained for sharing between multiple fields' accessors when implementing [`get_many_unchecked_mut`](Storage::get_many_unchecked_mut).
// TODO: check Clone again
fn ids(&self) -> impl Iterator<Item = Self::Id> + Clone;
/// Generate a new available id.
fn spawn(&mut self) -> Self::Id;
Expand Down

0 comments on commit 040be86

Please sign in to comment.