From e4edd8e9e99c40a29f9b93a1b22fee97b8888925 Mon Sep 17 00:00:00 2001 From: Tudor Lechintan Date: Wed, 27 Sep 2023 16:49:29 +0300 Subject: [PATCH] Add get_mut and try_get_mut to Resources --- CHANGELOG.md | 5 +++++ Cargo.toml | 2 +- src/resources/mod.rs | 16 ++++++++++++++++ src/resources/unsafe_resources.rs | 22 +++++++++++++++++++--- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f22c53..585cf1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`, diff --git a/Cargo.toml b/Cargo.toml index a09ccbf..f49b6c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 "] diff --git a/src/resources/mod.rs b/src/resources/mod.rs index 85f95fe..b3a3ab2 100644 --- a/src/resources/mod.rs +++ b/src/resources/mod.rs @@ -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(&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(&self) -> Option> where @@ -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(&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(&self) -> bool diff --git a/src/resources/unsafe_resources.rs b/src/resources/unsafe_resources.rs index 1612cd1..e582165 100644 --- a/src/resources/unsafe_resources.rs +++ b/src/resources/unsafe_resources.rs @@ -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. @@ -42,13 +41,21 @@ impl UnsafeResources { .unwrap_or_else(|| utils::panic_missing_res::()) } + pub unsafe fn get_mut(&mut self) -> &mut T + where + T: Resource, + { + self.try_get_mut() + .unwrap_or_else(|| utils::panic_missing_res::()) + } + pub unsafe fn try_borrow(&self) -> Option> where T: Resource, { self.resources.get(&TypeId::of::()).map(|c| { Res::new(AtomicRef::map(c.borrow(), |c| { - c.deref().downcast_ref().unwrap_unchecked() + c.downcast_ref().unwrap_unchecked() })) }) } @@ -59,11 +66,20 @@ impl UnsafeResources { { self.resources.get(&TypeId::of::()).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(&mut self) -> Option<&mut T> + where + T: Resource, + { + self.resources + .get_mut(&TypeId::of::()) + .map(|c| c.get_mut().downcast_mut().unwrap_unchecked()) + } + #[must_use] pub fn contains(&self) -> bool where