aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-05-15 18:57:27 +0800
committer魏曹先生 <1992414357@qq.com>2026-05-15 18:57:27 +0800
commit99b0fab4f0d711ebc958e27731e2d9bcbea6ab55 (patch)
treeee7e7c35f356605c598398059727175b6bb51ece
parent76a2cd52acee80eed2097b777270912abf734db0 (diff)
Add `modify` method to `ResourceMarker` trait
-rw-r--r--CHANGELOG.md14
-rw-r--r--mingling_core/src/asset/global_resource.rs14
2 files changed, 26 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9f2c07e..6b0be09 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -64,6 +64,20 @@ fn your_chain(_prev: Prev) -> NextProcess {
9. **\[core\]** `RenderResult` now carries new data `exit_code`
+10. **\[core\]** Added `modify` function to `ResourceMarker` for modifying a program's global resources
+
+```rust
+// Example
+ExitCode::modify::<ThisProgram>(|code| {
+ code.exit_code = 1;
+});
+
+// Equivalent to:
+this::<ThisProgram>().modify_res::<ExitCode>(|code| {
+ code.exit_code = 1;
+});
+```
+
#### **BREAKING CHANGES**:
1. **\[core\]** The signature of `exec` has been changed to `exec(self) -> i32` (previously was `exec(self)`)
diff --git a/mingling_core/src/asset/global_resource.rs b/mingling_core/src/asset/global_resource.rs
index 29d2405..8e2ce9f 100644
--- a/mingling_core/src/asset/global_resource.rs
+++ b/mingling_core/src/asset/global_resource.rs
@@ -4,7 +4,7 @@ use std::{
sync::{Arc, Mutex},
};
-use crate::{ChainProcess, Program, ProgramCollect};
+use crate::{ChainProcess, Program, ProgramCollect, this};
pub(crate) type GlobalResources = Arc<Mutex<HashMap<TypeId, Box<dyn Any + Sync + Send>>>>;
@@ -109,9 +109,12 @@ impl<ResType: 'static + Send + Sync> AsRef<ResType> for GlobalResource<ResType>
pub trait ResourceMarker {
fn res_clone(&self) -> Self;
fn res_default() -> Self;
+ fn modify<C>(f: impl FnOnce(&mut Self))
+ where
+ C: ProgramCollect<Enum = C> + 'static;
}
-impl<T: Default + Clone> ResourceMarker for T {
+impl<T: Default + Clone + Send + Sync + 'static> ResourceMarker for T {
fn res_clone(&self) -> Self {
Clone::clone(self)
}
@@ -119,4 +122,11 @@ impl<T: Default + Clone> ResourceMarker for T {
fn res_default() -> Self {
Default::default()
}
+
+ fn modify<C>(f: impl FnOnce(&mut Self))
+ where
+ C: ProgramCollect<Enum = C> + 'static,
+ {
+ this::<C>().modify_res(f);
+ }
}