Skip to content

Cloning an Arc with a Trait-Object tries to Clone the Trait-Object #15125

Closed
@reem

Description

@reem

This will compile:

use std::sync::Arc;

trait TestTrait {}

pub struct Shared {
    pub thing: Arc<Box<TestTrait + Send + Share>>
}

impl Clone for Shared {
    fn clone(&self) -> Shared {
        Shared {
            thing: self.thing.clone()
        }
    }
}

But adding a :Clone bound to the TestTrait definition like so:

use std::sync::Arc;

trait TestTrait: Clone {}

pub struct Shared {
    pub thing: Arc<Box<TestTrait + Send + Share>>
}

impl Clone for Shared {
    fn clone(&self) -> Shared {
        Shared {
            thing: self.thing.clone()
        }
    }
}

Fails with:

src/shared.rs:12:20: 12:38 error: cannot call a method whose type contains a self-type through an object
src/shared.rs:12             thing: self.thing.clone()
                                    ^~~~~~~~~~~~~~~~~~

I think this is just a name resolution issue, but I'm not sure why this happens.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions