plugins/languages/rust/skills/async/SKILL.md
Rust 异步编程规范 — Tokio 1.x runtime、`async fn` in traits(stable)、async closures(Rust 1.85+ stable)、结构化并发(JoinSet / select! / spawn)、channel(mpsc / oneshot / broadcast)、tower middleware、axum 0.8 web、Pin / Send / Sync、跨 await 借用、死锁排查。编写异步服务、网络 IO、并发任务、async 死锁分析时加载。触发短语:async fn、tokio、axum、并发、死锁、Future、spawn、await、async trait、stream。
npx skillsauth add lazygophers/ccplugin rust-asyncInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
前置:rust-core、rust-memory。
#[tokio::main] // 默认 multi-thread
async fn main() -> anyhow::Result<()> { Ok(()) }
#[tokio::main(flavor = "current_thread")] // 单线程,最低开销
#[tokio::main(flavor = "multi_thread", worker_threads = 4)]
#[tokio::test] // 测试入口
阻塞操作必须 tokio::task::spawn_blocking 隔离;纯 CPU 密集考虑独立线程池或 rayon。
async fn in traits(Rust 1.75+ stable)trait Repository: Send + Sync {
async fn find(&self, id: u64) -> anyhow::Result<Option<User>>;
async fn save(&self, u: &User) -> anyhow::Result<()>;
}
禁用 #[async_trait] 宏(除非要 dyn Trait 对象安全且 RPITIT 还不够)。
返回 impl Future + Send 自动推断;跨线程需 Send bound 时显式标注。
let fetch = async |url: String| -> anyhow::Result<String> {
reqwest::get(&url).await?.text().await.map_err(Into::into)
};
let body = fetch("https://example.com".into()).await?;
三个 trait:AsyncFn / AsyncFnMut / AsyncFnOnce,可在中间件、handler、迭代器适配中替代手写 |x| async move { ... }。
use tokio::task::JoinSet;
let mut set = JoinSet::new();
for url in urls { set.spawn(async move { fetch(url).await }); }
let mut out = Vec::new();
while let Some(res) = set.join_next().await { out.push(res?); }
JoinSet:动态任务集合,drop 自动取消。tokio::spawn:长生命周期后台任务。select!:竞争 / 超时 / 取消。futures::join_all(无错误短路、无取消)。tokio::select! {
res = work() => handle(res),
_ = tokio::time::sleep(Duration::from_secs(5)) => bail!("timeout"),
}
| Channel | 拓扑 | 场景 |
|---------|------|------|
| mpsc | 多生产-单消费 | 队列、worker pool |
| oneshot | 单值响应 | RPC 应答 |
| broadcast | 广播 | 事件总线 |
| watch | 最新值 | 配置 / 状态 |
use axum::{Router, Json, extract::{State, Path}, routing::{get, post}};
use tower_http::trace::TraceLayer;
async fn create(State(r): State<Arc<dyn Repository>>, Json(req): Json<CreateReq>)
-> Result<Json<User>, AppError>
{
Ok(Json(r.save(&req.into()).await?))
}
fn app(r: Arc<dyn Repository>) -> Router {
Router::new()
.route("/users", post(create))
.route("/users/{id}", get(get_user))
.layer(TraceLayer::new_for_http())
.with_state(r)
}
axum 0.8 路由占位符是 {id} 而非 :id。
Pin;自引用 / 手写 Future 才需要。tokio::spawn 的 Future 必须 Send + 'static → 跨 .await 不得持有非 Send(如 Rc、std::sync::MutexGuard)。tokio::sync::Mutex 才能跨 await,否则 std::sync::Mutex 必须在 await 前 drop。let value = { let g = mtx.lock().unwrap(); g.clone() }; // 释放后再 await
remote(value).await;
| AI 倾向 | 正确做法 |
|---------|---------|
| #[async_trait] | stable async fn in trait |
| block_on 嵌套 | 永远不要在 async 上下文 block_on |
| futures::join_all | JoinSet(支持取消、错误) |
| Arc<Mutex<T>> 共享 | 优先 channel / actor |
| 同步 IO 在 async fn | spawn_blocking |
| 持非 Send 跨 await | await 前 drop 或换 tokio::sync |
tokio 1.xasync fn 不用 #[async_trait]JoinSetselect!{param} 语法tools
--- name: trellisx-workspace description: 维护 `.trellis/task.md` 任务看板 —— trellis 缺的跨任务总览。**一个表格, 一行一个任务**, 列为 id/名称/描述/状态/阶段/进度/worktree (状态/阶段中文显示)。在 task create/start/阶段切换/archive 后**及时更新**对应行; 并**自动清理超 7 天的已完成行**防膨胀。保持看板与 task.json 实时一致。 when_to_use: 维护 / 创建 / 更新 `.trellis/task.md` 任务看板时; task 生命周期任一节点 (create/start/阶段推进/archive) 之后同步看板时; 用户问"当前有哪些任务 / 任务进度 / 任务看板"时。被 trellisx-flow 与 trellisx-apply 注入的流程引用。 user-invocable: true argument-hint: [show|update|sync|cleanup ...] [task id] arguments:
testing
强制以 Trellis task 闭环处理用户指定的请求 (自判新建/并入 → plan→exec→check→finish 全程不跳步)。**仅用户显式主动调用** (/trellisx-flow 或明确要求"强制走 task 处理这个"); **禁止自动 / 被动 / 推断式调用** —— 不要因为某个请求"看起来该建 task"就自动触发本 skill, 那是 apply 注入的 no_task 倾向的职责。
testing
把 强推task + subtask拆分 + worktree隔离 + 闭环收尾 四维度增量注入当前项目 .trellis/ (workflow.md 的 no_task/planning/in_progress 块 + spec 背书文档 + trellis 生命周期 hook worktree 自动化)。强推 task 与闭环为纯 prompt 软约束 (非平台 hook 硬拦截)。**纯增量追加, 绝不替换 trellis 原生文本** (no_task 分类+征同意/check/finish/前缀全保留)。幂等 (marker 包裹)。
development
Claude Code 会话历史整理 — 扫 ~/.claude/projects/**/*.jsonl 全部 session transcripts, 提取学习增量 (用户校正/决策/踩坑/L0 规则) → 全局记忆库 ~/.cortex/.wiki/memory/. 默认 --apply 落盘 (--dry-run opt-in 仅出 JSON plan 预览). 与 cortex-extract (L4-inbox 内部) 互补.