dtolnay / async-trait

Type erasure for async trait methods
Apache License 2.0
1.81k stars 84 forks source link

`where Self: Sized` does not seem to be supported #165

Closed Ploppz closed 3 years ago

Ploppz commented 3 years ago

I get compiler error:

error[E0038]: the trait `jobs::Job` cannot be made into an object
  --> src/jobs/mod.rs:24:14
   |
24 |     async fn revive(
   |              ^^^^^^ `jobs::Job` 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/jobs/mod.rs:24:14
   |
20 | pub trait Job: Send + Sync + 'static {
   |           --- this trait cannot be made into an object...
...
24 |     async fn revive(
   |              ^^^^^^ ...because associated function `revive` has no `self` parameter
help: consider turning `revive` into a method by giving it a `&self` argument
   |
24 |     async fn revive(&self,
   |                     ^^^^^^
help: alternatively, consider constraining `revive` so it does not apply to trait objects
   |
24 |     async fn revive where Self: Sized(
   |                     ^^^^^^^^^^^^^^^^^

But when I try the last suggestion, I get:

error: expected one of `(` or `<`, found keyword `where`
  --> src/jobs/mod.rs:24:21
   |
20 | pub trait Job: Send + Sync + 'static {
   |                                      - while parsing this item list starting here
...
24 |     async fn revive where Self: Sized(
   |                     ^^^^^ expected one of `(` or `<`
...
32 | }
   | - the item list ends here
taiki-e commented 3 years ago

Seems the problem is the suggestion by rustc omits the inputs ((&self, ...)) and output (-> ...) of the function. Try adding where Self: Sized after the output of the function without removing inputs and output of the functions.

Also, the object safety issue is explained in the documentation. See that for more.

Ploppz commented 3 years ago

Ah I see, thanks. Not a probem with async-trait then.