Matt
Matt

Reputation: 4049

Is it possible to create a `HashMap` keyed by something of type `*const Any`?

This code

use std::any::Any;
use std::collections::HashMap;

fn main() {
    let x: HashMap<*const Any, i32> = HashMap::new();
}

Gives me the following error:

error: the trait `core::marker::Sized` is not implemented for the type `core::any::Any` [E0277]

let x: HashMap<*const Any, i32> = HashMap::new();
                                  ^~~~~~~~~~~~

First of all, I don't understand why it is complaining about core::any::Any, when the keys are of type *const core::any::Any. Shouldn't *const _ be sized regardless of what it is pointing to? To test this, I tried:

use std::any::Any;
use std::mem::size_of;

fn main() {
    println!("size_of(*const Any) = {}", size_of::<*const Any>());
}

Which, as expected, produces:

size_of(*const Any) = 16

Upvotes: 8

Views: 1069

Answers (1)

Matt
Matt

Reputation: 4049

It's not the prettiest solution, but here is what I came up with:

use std::any::Any;
use std::collections::HashMap;
use std::hash::{Hasher, Hash};
use std::cmp;

struct Wrapper {
    v: *const Any,
}

impl Wrapper {
    fn get_addr(&self) -> usize {
        self.v as *const usize as usize
    }
}

impl Hash for Wrapper {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.get_addr().hash(state)
    }
}

impl cmp::PartialEq for Wrapper {
    fn eq(&self, other: &Self) -> bool {
        self.get_addr() == other.get_addr()
    }
}

impl cmp::Eq for Wrapper {}

fn main() {
    let x: HashMap<Wrapper, i32> = HashMap::new();
}

Upvotes: 1

Related Questions