Giter Club home page Giter Club logo

rust-typemap's Introduction

TypeMap

A typesafe store keyed by types and containing different types of values.

It provides functionality similar to AnyMap, but is more flexible because it allows for key-value pairs, rather than enforcing that keys and values are the same type.

Key-value associations are defined through the Key trait, which uses an associated type parameter and trait coherence rules to enforce the invariants of TypeMap.

Example

extern crate typemap;
use typemap::{TypeMap, Key};

struct KeyType;

#[derive(Debug, PartialEq)]
struct Value(i32);

impl Key for KeyType { type Value = Value; }

#[test] fn test_pairing() {
    let mut map = TypeMap::new();
    map.insert::<KeyType>(Value(42));
    assert_eq!(*map.get::<KeyType>().unwrap(), Value(42));
}

rust-typemap's People

Contributors

alex-gulyas avatar andor44 avatar blaenk avatar durka avatar jnicklas avatar lraccomando avatar michaelsproul avatar nucleartide avatar oleduc avatar reem avatar ryman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rust-typemap's Issues

Is this crate dead?

@reem it looks like there are several unanswered issues and pull requests, with the last commit dating back 2 years. Would you mind clarifying the status of this crate, whether it's recommended to use it, whether it compiles on recent rust?

If you don't have time to maintain this, are you looking for other maintainers? You could check out the rust bus.

Thanks in advance.

Not compile on beta 2

Compiling typemap v0.2.1
/Users/APoltarjonoks/.cargo/registry/src/github.com-1ecc6299db9ec823/typemap-0.2.1/src/lib.rs:171:1: 171:42 error: the trait core::marker::Sized is not implemented for the type CloneAny [E0277]
/Users/APoltarjonoks/.cargo/registry/src/github.com-1ecc6299db9ec823/typemap-0.2.1/src/lib.rs:171 unsafe impl UnsafeAnyExt for CloneAny { }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/APoltarjonoks/.cargo/registry/src/github.com-1ecc6299db9ec823/typemap-0.2.1/src/lib.rs:171:1: 171:42 note: CloneAny does not have a constant size known at compile-time
/Users/APoltarjonoks/.cargo/registry/src/github.com-1ecc6299db9ec823/typemap-0.2.1/src/lib.rs:171 unsafe impl UnsafeAnyExt for CloneAny { }

Breaking under rust nightly

src/lib.rs:40:9: 40:65 error: mismatched types: expected `bool`, found `core::option::Option<Box<core::any::Any+'static>>` (expected bool, found enum core::option::Option)
src/lib.rs:40         self.data.insert(TypeId::of::<K>(), box val as Box<Any>)
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/lib.rs:66:9: 66:45 error: mismatched types: expected `bool`, found `core::option::Option<Box<core::any::Any+'static>>` (expected bool, found enum core::option::Option)
src/lib.rs:66         self.data.remove(&TypeId::of::<K>())
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors
Build failed, waiting for other jobs to finish...
Could not compile `typemap`.

To learn more, run the command again with --verbose.
cargo 0.0.1-pre-nightly (537b43c 2014-11-06 00:02:30 +0000)

typemap is broken with latest releases of rust

error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions.
   --> src/lib.rs:185:18
    |
185 | impl<'a, K: Key, A: ?Sized + UnsafeAnyExt + 'a = UnsafeAny> Entry<'a, K, A> {
    |                  ^
    |
    = note: #[deny(invalid_type_param_default)] on by default
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>

Does not build on latest nightly w/ RFC 509

After rust-lang/rust@400c3a0d implemented RFC 509
HashMap::VacantEntry now takes an additional type parameter Sized? Q: ToOwned<K>
The idea being that HashMap#entry() now accepts the key by reference (&'a Q instead of K).
Then VacantEntry calls Q#to_owned() -> K to store it in the map.

I've been trying to fix this; but I'm stumped. In TypeMap: the caller does not generate the TypeId, instead it is generated internally by TypeMap#entry().

Since no reference to this TypeId would live past the call to TypeMap#entry() this invalidates the Entry<'a, K, V> we're trying return.


The best I've been able to do so far is add a helper method that generates the TypeId and returns it to the caller so it lives long enough; then the caller passes it into TypeMap#entry()... but it's a huge loss for ergonomics.

Specified package has no binaries

When I try to install:

$ cargo install typemap
    Updating registry `https://github.com/rust-lang/crates.io-index`
 Downloading typemap v0.3.3
error: specified package has no binaries

Did I get the crates.io package name right? Is there something I'm doing wrong?

Unsoundness due to multidispatch

The test below panics when added to the test suite, I think associated types are required.

I tried patching it up, but I couldn't figure out the syntax to add a static bound for the associated type (can't find any tests for it in rustc either).

#[test] fn test_entry_multi() {
    impl Assoc<u32> for Key {}

    let mut map = TypeMap::new();
    map.insert::<Key, Value>(Value);
    if let Occupied(_) = map.entry::<Key, u32>() {
        panic!("Unsound")
    }
}

Map equality testing

Is there any way to have Eq and PartialEq impl for TypeMaps which support Eq and PartialEq for keys and values?

Yank version with "*" deps

An old version of typemap has dependencies specified with "*" version requirement (phantom = "*", unsafe-any = "*").

Crates with such vague dependency versions became unusable, because * allows picking too-new semver-incompatible dependencies.
It also breaks Cargo's minimal-versions feature, because * requirement allows picking very very old dependencies that aren't compatible with Rust 1.0.

Because this old version of typemap is unusable and may cause problems for Cargo, I suggest yanking it:

cargo yank --vers 0.0.3

add LICENSE text

MIT requires text license to be present along with source. Please add it & publish new version.

Thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.