dtolnay / async-trait

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

mut self doesn't work with feature support_old_nightly #80

Closed ndmitchell closed 4 years ago

ndmitchell commented 4 years ago

Given:

// main.rs
use async_trait::async_trait;

#[async_trait]
trait Trait {
    async fn operation(mut self);
}

struct Struct {
    child: String,
}

#[async_trait]
impl Trait for Struct {
    async fn operation(mut self) {
        let _ = &mut self.child;
    }
}

fn main() {
    println!("Hello, world!");
}

# Cargo.toml
[package]
name = "async-trait"
version = "0.1.0"
edition = "2018"

[dependencies]
async-trait = {version = "0.1.27", features = ["support_old_nightly"]}

I get the error using the latest Rust nightly:

error[E0596]: cannot borrow `self.child` as mutable, as `self` is not declared as mutable
  --> src\main.rs:15:17
   |
14 |     async fn operation(mut self) {
   |                            ---- help: consider changing this to be mutable: `mut self`
15 |         let _ = &mut self.child;
   |                 ^^^^^^^^^^^^^^^ cannot borrow as mutable

The support_old_nightly feature seems to be the issue.

dtolnay commented 4 years ago

The support_old_nightly feature is an alternative implementation of async-trait that produces worse error messages but supports some older toolchains dating back to before stabilization of async/await that the default implementation does not support. At this point I don't think anyone should still be targeting those old nightly toolchains so I think I'll go ahead and remove the support_old_nightly implementation in the next release of async-trait.