1

Updated: Minimum reproducible example is available at the Rust Playground. Please be advised that simply adding the normal derive statement at the stop isn't really sufficient, which is why it's not included in the original contents.

I would like to make a fancy universal dictionary in Rust for faster prototyping, where the keys are Strings and the values are wrapped in an AnyType container. 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>
    {
        // ...

any ideas on how I might be able to maneuver the compiler's error? It says:

error[E0204]: the trait `Copy` may not be implemented for this type
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Apr 28 '20 at 19:49
  • Please [edit] your question and paste the exact and entire error that you're getting — that will help us to understand what the problem is so we can help best. Sometimes trying to interpret an error message is tricky and it's actually a different part of the error message that's important. Please use the message from running the compiler directly, not the message produced by an IDE, which might be trying to interpret the error for you. – Shepmaster Apr 28 '20 at 19:49
  • TL;DR the duplicates: you cannot do this. – Shepmaster Apr 28 '20 at 19:52
  • 2
    Some suggestions: [Derive](https://doc.rust-lang.org/rust-by-example/trait/derive.html) `Clone` with `#[derive(Clone)]` instead of implementing it manually. Also, remove the underscores from the identifiers, it's neither required nor idiomatic. You can also remove the `Box` in the `AnyType` struct, since `Rc` is already a reference type; `Rc>>` is a double indirection. – Aloso Apr 28 '20 at 20:22

0 Answers0