Tips for implementing a trait for all types that implement another trait?

Is there a way to implement a trait for all types that implement another trait? I want a special Clone-like trait for internal use in my library, call it CloneSpecial. I would like to automatically implement it with Clone for all types that implement Clone, but to make some types that implement CloneSpecial but not Clone. Below is code analogous to my attempt at making this happen:

trait CloneSpecial {
    fn clone_special(&self) -> &Self;
}

impl<ClonableType: Clone> CloneSpecial for ClonableType {
    fn clone_special(&self) -> &Self {
        self.clone()    // Error at this line 
    }
}

The error is as follows:

mismatched types

expected reference, found type parameter

note: expected type &ClonableType
found type ClonableType
help: type parameters must be constrained to match other types
note: for more information, visit Traits: Defining Shared Behavior - The Rust Programming Language

Is what I'm attempting to do possible? If so, where am I going wrong? Thanks in advance :slight_smile:

Your method should probably return Self, not &Self.

1 Like

Ah, ok, just a dumb error on my part. Thanks for the help!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.