Skip to content

derive(Clone) has trouble correctly understanding generics. #44151

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

Closed
dawid2193487 opened this issue Aug 29, 2017 · 1 comment
Closed

derive(Clone) has trouble correctly understanding generics. #44151

dawid2193487 opened this issue Aug 29, 2017 · 1 comment

Comments

@dawid2193487
Copy link

When attempting to derive Clone on a struct, Clone requires itself to implemented on all generic type arguments, even though that's not needed

I tried this code:

use std::sync::{ Arc, Mutex };

trait Entity { }

struct Tile { }
impl Entity for Tile { }

#[derive(Clone)]
struct EntityStore<T: Entity> {
    entities: Arc<Mutex<Vec<Arc<T>>>>
}

#[derive(Clone)]
struct World {
    tiles: EntityStore<Tile>
}

I expected to see this happen: EntityStore wraps T in an Arc, so no matter what T is, it should be cloneable.

Instead, this happened: Rust complains that Tile isn't Clone, even though it being Clone is not needed.

error[E0277]: the trait bound `Tile: std::clone::Clone` is not satisfied
  --> src/main.rs:15:5
   |
15 |     tiles: EntityStore<Tile>
   |     ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `Tile`
   |
   = note: required because of the requirements on the impl of `std::clone::Clone` for `EntityStore<Tile>`
   = note: required by `std::clone::Clone::clone`

When I don't make EntityStore generic, it works with no issue

use std::sync::{ Arc, Mutex };

trait Entity { }

struct Tile { }
impl Entity for Tile { }

#[derive(Clone)]
struct EntityStore {
    entities: Arc<Mutex<Vec<Arc<Tile>>>>
}

#[derive(Clone)]
struct World {
    tiles: EntityStore
}

Meta

rustc --version --verbose: n/a, using current playgrounds stable

Backtrace: n/a

@durka
Copy link
Contributor

durka commented Aug 29, 2017

Duplicate of #26925. Known issue for quite a long time and there are serious roadblocks to fixing it. In the meantime, a #[derive(Clone)] with customizable bounds can be implemented as a custom derive (or maybe someone has done that already).

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

3 participants