E0277
rust
type_error
ai_generated
true
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%Fix Rate
85%Confidence
1Evidence
2023-08-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| rustc 1.75 | active | — | — | — |
| tokio 1.35 | active | — | — | — |
Root Cause
Using `Rc` inside an async block or future that is expected to implement `Send`, typically when spawning a task with `tokio::spawn`.
generic中文
在需要实现 `Send` 的异步块或 future 中使用了 `Rc`,通常在使用 `tokio::spawn` 生成任务时发生。
Official Documentation
https://doc.rust-lang.org/stable/error_codes/E0277.htmlWorkarounds
-
95% success Replace `Rc` with `Arc` (atomic reference counting) and `RefCell` with `Mutex` or `RwLock`.
Replace `Rc` with `Arc` (atomic reference counting) and `RefCell` with `Mutex` or `RwLock`.
-
85% success Use `tokio::task::LocalSet` to spawn non-Send futures on the current thread if you must keep `Rc`.
Use `tokio::task::LocalSet` to spawn non-Send futures on the current thread if you must keep `Rc`.
中文步骤
将 `Rc` 替换为 `Arc`(原子引用计数),并将 `RefCell` 替换为 `Mutex` 或 `RwLock`。
使用 `tokio::task::LocalSet` 在当前线程上生成非 Send 的 future,如果你必须保留 `Rc`。
Dead Ends
Common approaches that don't work:
-
75% fail
`Mutex<Rc>` still doesn't make `Rc` `Send`; `Rc` is fundamentally not thread-safe. You need `Arc` instead.
-
90% fail
Unsafe to implement `Send` for `Rc`; leads to data races and undefined behavior.