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
Hashmap in rust is a structure which comprises of the look-up table and data in it in form of key and value pair which will be used for storing and retrieving data continuously. Hashmap needs to be explicitly imported from the rust inbuilt library collection before that can be used within the program.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With