https://github.com/mintlu8/ty_map_gen
A type projecting map generator.
https://github.com/mintlu8/ty_map_gen
Last synced: about 2 months ago
JSON representation
A type projecting map generator.
- Host: GitHub
- URL: https://github.com/mintlu8/ty_map_gen
- Owner: mintlu8
- License: apache-2.0
- Created: 2024-05-17T13:25:01.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-19T03:32:16.000Z (about 2 years ago)
- Last Synced: 2025-02-28T08:03:36.667Z (over 1 year ago)
- Language: Rust
- Size: 20.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# ty_map_gen
[](https://crates.io/crates/ty_map_gen)
[](https://docs.rs/ty_map_gen/latest/ty_map_gen/)
A type projecting map generator.
## Syntax
```rust
type_map!(
/// Asset Map
#[derive(Clone, PartialEq, Eq)]
pub AssetMap where T [Asset] => Handle [Clone + Eq] as HashMap
);
```
This creates a type that projects `T` (a generic) to `Handle` (roughly):
```rust
pub struct AssetMap(HashMap>)
```
with access methods (roughly):
```rust
fn get(&self) -> Option<&Handle> where Handle: Clone + Eq {
self.0.get(TypeId::of::()).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`.
## Bounds
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.
## Methods and Implementations
By default these methods are generated by the macro:
`Default`, `new`, `is_empty`, `len`, `get`, `get_mut`, `insert`, `remove`, `clear`, `extend`.
## Double Keys
```rust
type_map!(
/// Asset Map
#[derive(Clone, PartialEq, Eq)]
pub AssetMap where (T, String) [Asset] => Handle [Clone + Eq] as HashMap
);
```
This adds another key to the map, with access methods (roughly):
```rust
fn get(&self, key: &Q) -> Option<&Handle> where Handle: Clone + Eq {
self.0.get(&(TypeId::of::(), key)).and_then(|v| v.downcast_ref())
}
```
## Performance
This crate has faster lookup (`get` and `get_mut`) than a naive implementation with `Box`
since downcasting is unchecked.
## License
License under either of
Apache License, Version 2.0 (LICENSE-APACHE or )
MIT license (LICENSE-MIT or )
at your option.
## Contribution
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.