trait 中的 async fn

版本新特性 / Rust 1.75

// *** 以前 ***
// 带异步方法的 trait 过去需要外部宏,
// 而且每次调用都要用一个装着 future 的 Box
#[async_trait]
trait Storage {
    async fn load(&self, id: u32) -> String;
}

// *** 在 1.75 版本中 ***
trait Storage {
    async fn load(&self, id: u32) -> String;
}

struct Memory;

impl Storage for Memory {
    async fn load(&self, id: u32) -> String {
        format!("record {id}")
    }
}