Heya, I just published resman
0.7.0.
resman
is a combination of an anymap, and runtime managed resource borrowing.
Features:
Borrows are managed through interior mutability at runtime
let mut resources = Resources::new();
resources.insert(1u32);
resources.insert(2u64);
let resources = &resources; // immutable ref
let mut my_u32 = resources.borrow_mut::<u32>();
let mut my_u64 = resources.borrow_mut::<u64>();
*my_u32 += 1;
*my_u64 += 1;
User friendly Debug impl
Enable the "debug"
feature for values' debug strings to be printed:
#[derive(Debug)]
pub struct MyStruct {
pub a: u32,
}
let mut resources = Resources::default();
resources.insert(1u32);
resources.insert(2u64);
resources.insert(true);
resources.insert(MyStruct { a: 3 });
println!("{:#?}", resources);
output:
{
u32: 1,
bool: true,
u64: 2,
my_crate::MyStruct: MyStruct {
a: 3,
},
}
The keys are always type names, and therefore are always printed.
"fn_res" feature allows different functions to be grouped together as Box<dyn FnRes>
As long as the functions / closures have the same return type1, and only have &T
or &mut T
parameters, they can be grouped together:
use resman::{FnRes, IntoFnRes, Resources};
/// Borrows `u32` mutably, and `u64` immutably.
fn f1(a: &mut u32, b: &u64) -> u64 {
*a += 1;
*a as u64 + *b
}
/// Borrows `u32` immutably, and `u64` mutably.
fn f2(a: &u32, b: &mut u64) -> u64 {
*b += 1;
*a as u64 + *b
}
let functions = [
f1.into_fn_res(),
f2.into_fn_res(),
(|a: &u32, b: &u64| *a as u64 + *b).into_fn_res(),
];
let mut resources = Resources::default();
resources.insert(0u32);
resources.insert(0u64);
let sum = functions
.iter()
.fold(0, |sum, fn_res| sum + fn_res.call(&resources));
assert_eq!(5, sum); // 1 + 2 + 2
1The generated code behind the scenes has a limit of 7 parameters.
Links:
-
crate: https://crates.io/crates/resman
-
repo: GitHub - azriel91/resman: Runtime managed resource borrowing.
-
docs.rs: resman - Rust
Hope you find it useful.