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
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`.
官方文档
https://doc.rust-lang.org/stable/error_codes/E0277.html解决方案
-
将 `Rc` 替换为 `Arc`(原子引用计数),并将 `RefCell` 替换为 `Mutex` 或 `RwLock`。
-
使用 `tokio::task::LocalSet` 在当前线程上生成非 Send 的 future,如果你必须保留 `Rc`。
无效尝试
常见但无效的做法:
-
75% 失败
`Mutex<Rc>` still doesn't make `Rc` `Send`; `Rc` is fundamentally not thread-safe. You need `Arc` instead.
-
90% 失败
Unsafe to implement `Send` for `Rc`; leads to data races and undefined behavior.