Skip to content

Trait auto import not working for some cases (e.g. tokio AsyncWriteExt in async method) #5115

@samlh

Description

@samlh

(Edit: In this example) rustc returns a nice error if you forget to import a needed trait - it would be nice to have a suggestion in rust-analyser to add the use statement.

use tokio::io::AsyncWrite;

pub struct PlaylistWriter<F: AsyncWrite + Unpin + Send> {
    playlist_file: F,
}

impl<F: AsyncWrite + Unpin + Send> PlaylistWriter<F> {
    pub async fn start(mut playlist_file: F) -> eyre::Result<Self> {
        playlist_file.write_all(b"#EXTM3U\n").await?;

        Ok(PlaylistWriter { playlist_file })
    }
}
error[E0599]: no method named `write_all` found for type parameter `F` in the current scope
 --> src/temp.rs:9:23
  |
9 |         playlist_file.write_all(b"#EXTM3U\n").await?;
  |                       ^^^^^^^^^ method not found in `F`
  |
  = help: items from traits can only be used if the trait is in scope
  = note: the following trait is implemented but not in scope; perhaps add a `use` for it:
          `use tokio::io::util::async_write_ext::AsyncWriteExt;`

In this case, the ideal would be to suggest updating the use statement to:

use tokio::io::{AsyncWrite, AsyncWriteExt};

Metadata

Metadata

Labels

E-has-instructionsIssue has some instructions and pointers to code to get started

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions