Skip to content

Commit

Permalink
Add get_mut and try_get_mut to Resources
Browse files Browse the repository at this point in the history
  • Loading branch information
LechintanTudor committed Sep 27, 2023
1 parent 5109999 commit e4edd8e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.11.1 (2023-09-27)
## Added
- Added `get_mut` and `try_get_mut` functions for getting mutable references to
resources from a `&mut Resources`.

# 0.11.0 (2023-07-16)
## Added
- Added functions for running systems that only borrow data from `World`,
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sparsey"
version = "0.11.0"
version = "0.11.1"
edition = "2021"
description = "Sparse set-based entity component system"
authors = ["Tudor-Cristian Lechințan <[email protected]>"]
Expand Down
16 changes: 16 additions & 0 deletions src/resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ impl Resources {
unsafe { self.resources.borrow_mut() }
}

/// Returns a mutable reference to a resource. Panics if the resource does not exist.
pub fn get_mut<T>(&mut self) -> &mut T
where
T: Resource,
{
unsafe { self.resources.get_mut() }
}

/// Borrows a view over a resource if that resource exists.
pub fn try_borrow<T>(&self) -> Option<Res<T>>
where
Expand All @@ -66,6 +74,14 @@ impl Resources {
unsafe { self.resources.try_borrow_mut() }
}

/// Returns a mutable reference to a resource if that resource exist.
pub fn try_get_mut<T>(&mut self) -> Option<&mut T>
where
T: Resource,
{
unsafe { self.resources.try_get_mut() }
}

/// Returns `true` if the stoage contains a resource with type `T`.
#[must_use]
pub fn contains<T>(&self) -> bool
Expand Down
22 changes: 19 additions & 3 deletions src/resources/unsafe_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
use rustc_hash::FxHashMap;
use std::any::TypeId;
use std::fmt;
use std::ops::{Deref, DerefMut};

/// Unsafe resource storage. Unsafe because it can store `!Send` and `!Sync` resources while being
/// `Send + Sync` itself.
Expand Down Expand Up @@ -42,13 +41,21 @@ impl UnsafeResources {
.unwrap_or_else(|| utils::panic_missing_res::<T>())
}

pub unsafe fn get_mut<T>(&mut self) -> &mut T
where
T: Resource,
{
self.try_get_mut()
.unwrap_or_else(|| utils::panic_missing_res::<T>())
}

pub unsafe fn try_borrow<T>(&self) -> Option<Res<T>>
where
T: Resource,
{
self.resources.get(&TypeId::of::<T>()).map(|c| {
Res::new(AtomicRef::map(c.borrow(), |c| {
c.deref().downcast_ref().unwrap_unchecked()
c.downcast_ref().unwrap_unchecked()
}))
})
}
Expand All @@ -59,11 +66,20 @@ impl UnsafeResources {
{
self.resources.get(&TypeId::of::<T>()).map(|c| {
ResMut::new(AtomicRefMut::map(c.borrow_mut(), |c| {
c.deref_mut().downcast_mut().unwrap_unchecked()
c.downcast_mut().unwrap_unchecked()
}))
})
}

pub unsafe fn try_get_mut<T>(&mut self) -> Option<&mut T>
where
T: Resource,
{
self.resources
.get_mut(&TypeId::of::<T>())
.map(|c| c.get_mut().downcast_mut().unwrap_unchecked())
}

#[must_use]
pub fn contains<T>(&self) -> bool
where
Expand Down

0 comments on commit e4edd8e

Please sign in to comment.