[][src]Crate resources

Latest Version Documentation License CI

This crate provides the Resources struct: a container that stores at most one value of each specific type, and allows safely and concurrently accessing any of them with interior mutability, without violating borrow rules.

It's intended to be used as an implementation of storage for data that is not associated with any specific entity in an ECS (entity component system), but still needs concurrent access by systems.

Cargo features

  • fetch - when enabled, exposes Resources::fetch() that allows retrieving up to 16 resources with a one-liner.

Example

use resources::*;

struct SomeNumber(usize);

struct SomeString(&'static str);

fn main() {
   let mut resources = Resources::new();
   resources.insert(SomeNumber(4));
   resources.insert(SomeString("Hello!"));
   let resources = resources;  // This shadows the mutable binding with an immutable one.

   {
       let mut some_number = resources.get_mut::<SomeNumber>().unwrap();
       let mut some_string = resources.get_mut::<SomeString>().unwrap();

       // Immutably borrowing a resource that's already borrowed mutably is not allowed.
       assert!(resources.get::<SomeNumber>().is_err());

       some_number.0 = 2;
       some_string.0 = "Bye!";
   }

   // The mutable borrows now went out of scope, so it's okay to borrow again however needed.
   assert_eq!(resources.get::<SomeNumber>().unwrap().0, 2);

   // Multiple immutable borrows are okay.
   let some_string1 = resources.get::<SomeString>().unwrap();
   let some_string2 = resources.get::<SomeString>().unwrap();
   assert_eq!(some_string1.0, some_string2.0);
}

Structs

CantFetch

Error that may occur when retrieving one or several of Resource from a Resources container via ::fetch().

NoSuchResource

Error indicating that no Resource of requested type is present in a Resources container.

OccupiedEntry

A view into an occupied entry in a Resources container. It is part of the Entry enum.

Ref

Immutable borrow of a Resource stored in a Resources container.

RefMut

Mutable borrow of a Resource stored in a Resources container.

Resources

A Resource container, for storing at most one resource of each specific type.

VacantEntry

A view into a vacant entry in a Resources container. It is part of the Entry enum.

Enums

CantGetResource

Errors that may occur when accessing a Resource in a Resources container via get or get_mut methods.

Entry

A view into an entry in a Resources container, which may either be vacant or occupied. This is returned by the entry method on Resources.

InvalidBorrow

Error indicating that accessing the requested Resource in a Resources container via get or get_mut methods would violate borrow rules.

Traits

Resource

Types that can be stored in Resources, automatically implemented for all applicable.