Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HashMap when keys are Objects that override toHash and/or opEquals #2293

Open
n8sh opened this issue Apr 8, 2019 · 0 comments
Open

HashMap when keys are Objects that override toHash and/or opEquals #2293

n8sh opened this issue Apr 8, 2019 · 0 comments

Comments

@n8sh
Copy link
Contributor

n8sh commented Apr 8, 2019

Example of failure:
https://run.dlang.io/is/MQZ4Ye

/+dub.sdl:
dependency "vibe-d" version="~>0.8.5"
+/
void main()
{
    import vibe.utils.hashmap;
    import vibe.internal.allocator;

    final class Integer : Object {
        public const int value;

        this(int x) @nogc nothrow pure @safe { value = x; }

        override bool opEquals(Object rhs) const @nogc nothrow pure @safe {
            if (auto r = cast(Integer) rhs)
                return value == r.value;
            return false;
        }

        override size_t toHash() const @nogc nothrow pure @safe {
            return value;
        }
    }

    auto hashMap = HashMap!(Object, int)(vibeThreadAllocator());
    foreach (x; [2, 4, 8, 16])
        hashMap[new Integer(x)] = x;
    foreach (x; [2, 4, 8, 16])
        assert(hashMap[new Integer(x)] == x);
}

Related PR #2292.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant