https://github.com/kitlith/typemap_core
A map from a type to a value of that type, without needing alloc
https://github.com/kitlith/typemap_core
Last synced: 12 months ago
JSON representation
A map from a type to a value of that type, without needing alloc
- Host: GitHub
- URL: https://github.com/kitlith/typemap_core
- Owner: kitlith
- Created: 2021-02-02T06:19:55.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-09-14T22:34:59.000Z (over 2 years ago)
- Last Synced: 2025-06-28T13:59:26.678Z (12 months ago)
- Language: Rust
- Size: 40 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# typemap_core

[](https://crates.io/crates/typemap_core)
[](https://docs.rs/typemap_core)
*A no_std typemap with trait-based value-presence guarantees (on nightly)*
or
*A map from a type to a value of that type, without needing std/alloc*
## Context Example
```rust
use typemap_core::{typemap, Contains};
struct A;
#[derive(Debug)]
struct ContextA(u8);
// Context types are encouraged to be newtypes with From impls,
// so that when using the typemap! macro, you can use use the name
// of the newtype as a kind of readable field name.
impl From for ContextA {
fn from(val: u8) -> Self {
Self(val)
}
}
struct B;
#[derive(Debug)]
struct ContextB(u16);
impl From for ContextB {
fn from(val: u16) -> Self {
Self(val)
}
}
#[allow(unused)]
struct C {
a: A,
b: B,
}
trait ParseTrait {
fn parse(context: Ctx) -> Self;
}
impl ParseTrait for A
where
Ctx: Contains,
{
fn parse(context: Ctx) -> Self {
println!("{:?}", context.get::());
A
}
}
impl ParseTrait for B
where
Ctx: Contains,
{
fn parse(context: Ctx) -> Self {
println!("{:?}", context.get::());
B
}
}
impl ParseTrait for C
where
A: ParseTrait,
B: ParseTrait,
Ctx: Clone
{
fn parse(context: Ctx) -> Self {
Self {
a: A::parse(context.clone()),
b: B::parse(context)
}
}
}
fn main() {
let ctx = typemap!(ContextA = 0u8);
// Will panic at runtime on stable, and produce a compilation error on nightly
// C::parse(&ctx);
// context can be extended
let ctx = typemap!(ContextB = 10u16, ..ctx);
// prints:
// ContextA(0)
// ContextB(10)
C::parse(&ctx);
// and you can override earlier values of context without discarding them
let ctx = typemap!(ContextA = 5u8, ..ctx);
// prints:
// ContextA(5)
// ContextB(10)
C::parse(&ctx);
}
```
## Nightly
This crate contains the `Contains` and `ContainsMut` traits.
These traits are only implemented correctly on nightly due to missing features in stable,
When using this library, you are encouraged to (occasionally) use the nightly compiler
to catch errors in your constraints at compile-time rather than run-time,
even if you are otherwise targeting stable.
This crate will properly implement those traits on stable as soon as we find a way to do so,
but for now they are implemented for all instances of `Ty`
so that code running on stable doesn't need to cfg out all instances of requiring those traits.
However, some configurations involving references to the rest of the typemap and
overriding existing values fail to compile on nightly without the use of `-Ztrait-solver=next`.
## License
Licensed under either of
* Apache License, Version 2.0
(http://www.apache.org/licenses/LICENSE-2.0)
* MIT license
(http://opensource.org/licenses/MIT)
at your option.
## Contribution
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.