1

I want to make a custom type that accepts any object implementing a trait that extends PartialEq.

Playground

// In the external crate

// Actual Type:-
// [webdriver::command::ExtensionCommand](https://docs.rs/webdriver/0.40.2/webdriver/command/enum.WebDriverExtensionCommand.html)
pub trait Foo: Send + Clone + PartialEq {}

// Actual Type:-
// [webdriver::command::WebDriverCommand](https://docs.rs/webdriver/0.40.2/webdriver/command/enum.WebDriverCommand.html)
pub struct Bar<T: Foo> {
    foo: T,
}

// In my crate
type BarTwo = Bar<Box<dyn Foo>>;

I can not change the trait or struct because these types are not in my crate. I can not use generic types for BarTwo.

Output

error[E0038]: the trait `Foo` cannot be made into an object
  --> src/lib.rs:12:23
   |
4  | pub trait Foo : Send + Clone + PartialEq{}
   |           ---                  --------- ...because it uses `Self` as a type parameter in this
   |           |
   |           this trait cannot be made into an object...
...
12 | type BarTwo = Bar<Box<dyn Foo>>;
   |                       ^^^^^^^ the trait `Foo` cannot be made into an object
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366

0 Answers0