2 releases

0.0.2 Sep 11, 2020
0.0.1 Aug 23, 2020

#1891 in Rust patterns

30 downloads per month

MIT/Apache

39KB
863 lines

Repository: A repository for all kinds of entities.

Repository provides storage for multiple data types, it provides its own kind of index handle called EntityId and its own kind of typed handle called EntityPtr<T>. With these handles it allows the values resident in the same Repository to reference each other easily.

The data behind the EntityPtr<T> handle can be accessed when you have a corresponding reference to the whole repository.

The data behind the EntityId handle can be accessed when you know its type and have a corresponding reference to the whole repository.

Usage example:

// create a repository.
let mut repo = Repo::new();

// insert and retrieve a pointer handle.
let a = repo.insert(42i32);
assert_eq!(42i32, *a.get_ref(&repo).unwrap());

// insert and retrieve a index handle.
let b = repo.insert_for_id(42i32);

// downcast from index handle to pointer handle.
let b_ptr = b.cast_ptr::<i32>(&repo).unwrap();
assert_eq!(42i32, *b_ptr.get_ref(&repo).unwrap());

// downcasting fails when type is incorrect.
let b_wrong_ptr = b.cast_ptr::<u32>(&repo);
assert_eq!(None, b_wrong_ptr);

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1.2–1.7MB
~32K SLoC