-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip pulling resources out of components
- Loading branch information
Showing
10 changed files
with
157 additions
and
83 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//! Types for declaring and storing [`Resource`]s. | ||
use crate::component::{ComponentDescriptor, ComponentId, ComponentInfo, StorageType}; | ||
use std::any::{Any, TypeId}; | ||
use thiserror::Error; | ||
|
||
pub trait Resource: Send + Sync + 'static {} | ||
impl<T> Resource for T where T: Send + Sync + 'static {} | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Resources { | ||
resources: Vec<ComponentInfo>, | ||
resource_indices: std::collections::HashMap<TypeId, usize, fxhash::FxBuildHasher>, | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum ResourceError { | ||
#[error("A resource of type {name:?} ({type_id:?}) already exists")] | ||
ResourceAlreadyExists { | ||
type_id: TypeId, | ||
name: String, | ||
existing_id: ComponentId, | ||
}, | ||
} | ||
|
||
impl Resources { | ||
#[inline] | ||
pub fn len(&self) -> usize { | ||
self.resources.len() | ||
} | ||
|
||
#[inline] | ||
pub fn is_empty(&self) -> bool { | ||
self.resources.len() == 0 | ||
} | ||
|
||
#[inline] | ||
pub fn get_info(&self, id: ComponentId) -> Option<&ComponentInfo> { | ||
self.resources.get(id.index()) | ||
} | ||
|
||
#[inline] | ||
pub fn get_resource_id(&self, type_id: TypeId) -> Option<ComponentId> { | ||
self.resource_indices | ||
.get(&type_id) | ||
.map(|index| ComponentId::new(*index)) | ||
} | ||
|
||
#[inline] | ||
pub fn init_resource<T: Resource>(&mut self) -> ComponentId { | ||
// SAFE: The [`ComponentDescriptor`] matches the [`TypeId`] | ||
unsafe { | ||
self.get_or_insert_resource_with(TypeId::of::<T>(), || { | ||
ComponentDescriptor::new_resource::<T>(StorageType::default()) | ||
}) | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn init_non_send<T: Any>(&mut self) -> ComponentId { | ||
// SAFE: The [`ComponentDescriptor`] matches the [`TypeId`] | ||
unsafe { | ||
self.get_or_insert_resource_with(TypeId::of::<T>(), || { | ||
ComponentDescriptor::new_non_send::<T>(StorageType::default()) | ||
}) | ||
} | ||
} | ||
|
||
/// # Safety | ||
/// | ||
/// The [`ComponentDescriptor`] must match the [`TypeId`] | ||
#[inline] | ||
unsafe fn get_or_insert_resource_with( | ||
&mut self, | ||
type_id: TypeId, | ||
func: impl FnOnce() -> ComponentDescriptor, | ||
) -> ComponentId { | ||
let resources = &mut self.resources; | ||
let index = self.resource_indices.entry(type_id).or_insert_with(|| { | ||
let descriptor = func(); | ||
let index = resources.len(); | ||
resources.push(ComponentInfo::new(ComponentId::new(index), descriptor)); | ||
index | ||
}); | ||
|
||
ComponentId::new(*index) | ||
} | ||
|
||
/// # Safety | ||
/// | ||
/// `id` must be a valid [ComponentId] | ||
#[inline] | ||
pub unsafe fn get_info_unchecked(&self, id: ComponentId) -> &ComponentInfo { | ||
debug_assert!(id.index() < self.resources.len()); | ||
self.resources.get_unchecked(id.index()) | ||
} | ||
} | ||
|
||
impl<'c> IntoIterator for &'c Resources { | ||
type Item = &'c ComponentInfo; | ||
|
||
type IntoIter = std::slice::Iter<'c, ComponentInfo>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.resources.iter() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests {} |
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
Oops, something went wrong.