如何告诉编译器在 async_trait 方法中等待后未使用值?

How to tell the compiler a value is not used after awaiting in an async_trait method?

在下面的示例代码中,一个非发送值 Vec<T> 被移动到 returns 其他的函数中。在这一点上,我不再关心那个向量。返回的对象没有存储对它的引用,它不再存在。

但是,当我在下一行 .await 时,出现错误“捕获的值不是 Send”。它不是,但是因为它应该在 vector_as_string 退出时被销毁,所以它不需要在未来重新启动时跨线程发送它,因为该变量再也不会被使用。

use async_trait::async_trait;

async fn write_value(value: Vec<u8>) {
    println!("something")
}

fn vector_as_string<T>(vec: Vec<T>) -> Vec<u8> {
    Vec::new()
}

#[async_trait]
trait Writer {
    async fn write_array<T>(&mut self, value: Vec<T>);
}

pub struct WriterImplementation {}

#[async_trait]
impl Writer for WriterImplementation {
    async fn write_array<T>(&mut self, value: Vec<T>) {
        let string = vector_as_string(value);

        write_value(string).await
    }
}

#[tokio::main]
async fn main() {
    println!("Hi");
}

依赖关系:

[dependencies]
tokio = { version = "1.9.0", features = ["full"]}
async-trait = "0.1.51"

错误:

error: future cannot be sent between threads safely
  --> src/main.rs:20:55
   |
20 |       async fn write_array<T>(&mut self, value: Vec<T>) {
   |  _______________________________________________________^
21 | |         let string = vector_as_string(value);
22 | |
23 | |         write_value(string).await
24 | |     }
   | |_____^ future created by async block is not `Send`
   |
note: captured value is not `Send`
  --> src/main.rs:20:40
   |
20 |     async fn write_array<T>(&mut self, value: Vec<T>) {
   |                                        ^^^^^ has type `Vec<T>` which is not `Send`
   = note: required for the cast to the object type `dyn Future<Output = ()> + Send`
help: consider further restricting this bound
   |
20 |     async fn write_array<T + std::marker::Send>(&mut self, value: Vec<T>) {
   |                            ^^^^^^^^^^^^^^^^^^^

按照它的建议添加 T: Send 允许它编译,但是为什么 T 需要是 Send 如果我们没有持有任何 T等待,因为它已经被移动了?

来自async_trait documentation

Async fns get transformed into methods that return Pin<Box<dyn Future + Send + 'async>> and delegate to a private async freestanding function.

Not all async traits need futures that are dyn Future + Send. To avoid having Send and Sync bounds placed on the async trait methods, invoke the async trait macro as #[async_trait(?Send)] on both the trait and the impl blocks.

适用于您的情况:

#[async_trait(?Send)]
trait Writer {
    async fn write_array<T>(&mut self, value: Vec<T>);
}

#[async_trait(?Send)]
impl Writer for WriterImplementation {
    async fn write_array<T>(&mut self, value: Vec<T>) {
        let string = vector_as_string(value);

        write_value(string).await
    }
}