Object safety and generic methods

Hi everyone,
I am learning Rust and trying to implement a small project with it.
I am facing some issues with trait that contains generic method.
I have read your discussion [note from @cole-miller: referring to this old thread] and learn a lot. However, it is still doesn't solve my case.
My case is quite similar to your code above, except that the AwesomeTrait contains a generic method.

This is the error:

error[E0038]: the trait `AwesomeTrait` cannot be made into an object
 --> src/main.rs:8:30
  |
8 |     fn boxed_clone(&self) -> Box<dyn AwesomeTrait>;
  |                              ^^^^^^^^^^^^^^^^^^^^^ `AwesomeTrait` cannot be made into an object
  |
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
 --> src/main.rs:4:8
  |
3 | trait AwesomeTrait {
  |       ------------ this trait cannot be made into an object...
4 |     fn awesomeness<B> (&self, x: B) {
  |        ^^^^^^^^^^^ ...because method `awesomeness` has generic type parameters
  = help: consider moving `awesomeness` to another trait

Could you please help me to solve it? Thank you in advance.

Hello and welcome! I've split your post out into a new topic (with a link to the original discussion) since we generally prefer not to reactivate years-old threads here.

1 Like

Ah, I see you made a new thread already here, so closing this.

1 Like