E0277 rust type_error ai_generated true

错误[E0277]:`std::rc::Rc<()>` 无法在线程间安全发送 `Rc<()>` 不是 `Send` 的,因为 `Rc` 不是 `Send` 的

error[E0277]: `std::rc::Rc<()>` cannot be sent between threads safely `Rc<()>` is not `Send` because `Rc` is not `Send`

ID: rust/e0277-async-fn-not-send

其他格式: JSON · Markdown 中文 · English
90%修复率
85%置信度
1证据数
2023-08-15首次发现

版本兼容性

版本状态引入弃用备注
rustc 1.75 active
tokio 1.35 active

根因分析

在需要实现 `Send` 的异步块或 future 中使用了 `Rc`,通常在使用 `tokio::spawn` 生成任务时发生。

English

Using `Rc` inside an async block or future that is expected to implement `Send`, typically when spawning a task with `tokio::spawn`.

generic

官方文档

https://doc.rust-lang.org/stable/error_codes/E0277.html

解决方案

  1. 将 `Rc` 替换为 `Arc`(原子引用计数),并将 `RefCell` 替换为 `Mutex` 或 `RwLock`。
  2. 使用 `tokio::task::LocalSet` 在当前线程上生成非 Send 的 future,如果你必须保留 `Rc`。

无效尝试

常见但无效的做法:

  1. 75% 失败

    `Mutex<Rc>` still doesn't make `Rc` `Send`; `Rc` is fundamentally not thread-safe. You need `Arc` instead.

  2. 90% 失败

    Unsafe to implement `Send` for `Rc`; leads to data races and undefined behavior.