#data #share #lock


Runtime managed resource borrowing

5 releases (breaking)

0.5.0 Aug 8, 2021
0.4.0 Aug 1, 2021
0.3.0 Jun 28, 2021
0.2.0 Jun 27, 2021
0.1.0 Apr 11, 2021

#3 in #share

26 downloads per month


515 lines

🗂️ resman

Crates.io CI Coverage Status

Runtime managed resource borrowing.

This library provides a map that can store one of any type, as well as mutable borrows to each type at the same time.

Note: This implementation is extracted from shred, with the following differences:

  • Uses downcast-rs instead of mopa for downcasting types.
  • Adds Debug and PartialEq implementations for borrow types when the resource type implements those traits.
  • Returns None instead of panicking for try_borrow* functions when the resource is already borrowed.


Add the following to Cargo.toml

resman = "0.5.0"

# or
resman = { version = "0.5.0", features = ["debug"] }

In code:

use resman::Resources;

struct A(u32);
struct B(u32);

fn main() {
    let mut resources = Resources::default();


    // We can validly have two mutable borrows from the `Resources` map!
    let mut a = resources.borrow_mut::<A>();
    let mut b = resources.borrow_mut::<B>();
    a.0 = 2;
    b.0 = 3;

    // We need to explicitly drop the A and B borrows, because they are runtime
    // managed borrows, and rustc doesn't know to drop them before the immutable
    // borrows after this.

    // Multiple immutable borrows to the same resource are valid.
    let a_0 = resources.borrow::<A>();
    let _a_1 = resources.borrow::<A>();
    let b = resources.borrow::<B>();

    println!("A: {}", a_0.0);
    println!("B: {}", b.0);

    // Trying to mutably borrow a resource that is already borrowed (immutably
    // or mutably) returns `None`.
    let a_try_borrow_mut = resources.try_borrow_mut::<A>();
    let exists = if a_try_borrow_mut.is_some() {
    } else {
    println!("a_try_borrow_mut: {}", exists); // prints "None"


  • "debug":

    The Debug implementation for Resources will use the Debug implementation for the values when printed. This requires that all Resources to also implement Debug.

    Given the following:

    let mut resources = Resources::default();
    println!("{:?}", resources);

    Without "debug" feature:

    {TypeId { t: 12849923012446332737 }: ".."}

    With "debug" feature:

    {TypeId { t: 12849923012446332737 }: 1}

See Also

  • anymap: Map of any type, without multiple mutable borrows.
  • shred: Contains Resources type, plus a task dispatcher.


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.