I would like to make a fancy universal dictionary for faster prototyping, where the keys are String
s and the values are types wrapped in an AnyType
container.
Please be advised that simply adding the normal derive statement above the struct isn't really sufficient, which is why it's not included among the original contents.
My implementation looks like this:
use std::collections::HashMap;
use std::boxed::Box;
use std::rc::Rc;
use std::cell::RefCell;
use std::clone::Clone;
struct AnyType<AT> {
___value: Rc<RefCell<Box<AT>>>
}
impl<AT> AnyType<AT> {
pub fn new(value: AT) -> AnyType<AT> {
AnyType {
___value: Rc::new(RefCell::from(Box::new(value)))
}
}
}
impl<AT> Copy for AnyType<AT> {}
impl<AT> Clone for AnyType<AT> {
fn clone(&self) -> Self {
*self
}
}
struct Dict<AT> {
___self: HashMap<String, AnyType<AT>>
}
impl<AT> Dict<AT> {
pub fn new(keys: Option<Vec<String>>, values: Option<Vec<AnyType<AT>>>)
-> Result<Dict<AT>, &'static str>
{
// ...
Error:
error[E0204]: the trait `Copy` may not be implemented for this type
--> src/lib.rs:18:10
|
8 | ___value: Rc<RefCell<Box<AT>>>
| ------------------------------ this field does not implement `Copy`
...
18 | impl<AT> Copy for AnyType<AT> {}
| ^^^^