Crates.io | ty_map_gen |
lib.rs | ty_map_gen |
version | 0.1.6 |
source | src |
created_at | 2024-05-17 13:47:57.179751 |
updated_at | 2024-05-21 08:53:17.72256 |
description | A type projecting map generator. |
homepage | |
repository | https://github.com/mintlu8/ty_map_gen |
max_upload_size | |
id | 1243256 |
size | 38,433 |
A type projecting map generator.
type_map!(
/// Asset Map
#[derive(Clone, PartialEq, Eq)]
pub AssetMap where T [Asset] => Handle<T> [Clone + Eq] as HashMap
);
This creates a type that projects T
(a generic) to Handle<T>
(roughly):
pub struct AssetMap(HashMap<TypeId, Box<dyn Any>>)
with access methods (roughly):
fn get<T: Asset + 'static>(&self) -> Option<&Handle<T>> where Handle<T>: Clone + Eq {
self.0.get(TypeId::of::<T>()).and_then(|v| v.downcast_ref())
}
Since all values stored in the map are Clone
and Eq
, they can be derived.
The as HashMap
field accepts all structs that has the same api
as HashMap
, this includes BTreeMap
and third party types
like FxHashMap
or VecMap
. To specify a custom hasher, you must
define a new type
with signature Map<Key, Value>
.
Bounds (in braces []
) are optional in the macro. The bounds on the right hand side must be object safe,
excluding Clone
, PartialEq
, Eq
, Ord
, PartialOrd
, Hash
and Serialize
,
which are special handled. Additionally only one trait from std::cmp
is allowed to be specified.
If you need Send
and Sync
this is where to add them.
Currently the right hand side uses a unique scope, therefore you must supply fully qualified trait paths.
By default these methods are generated by the macro:
Default
, new
, is_empty
, len
, get
, get_mut
, insert
, remove
, clear
, extend
.
type_map!(
/// Asset Map
#[derive(Clone, PartialEq, Eq)]
pub AssetMap where (T, String) [Asset] => Handle<T> [Clone + Eq] as HashMap
);
This adds another key to the map, with access methods (roughly):
fn get<T: Asset + 'static>(&self, key: &Q) -> Option<&Handle<T>> where Handle<T>: Clone + Eq {
self.0.get(&(TypeId::of::<T>(), key)).and_then(|v| v.downcast_ref())
}
This crate has faster lookup (get
and get_mut
) than a naive implementation with Box<dyn Any>
since downcasting is unchecked.
License under either of
Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.
Contributions are welcome!
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.