mcpField
S
spacetimedb-client-rust

spacetimedb-client-rust

github.com/danmossa开发2155 次安装19 周前更新
代码仓库

关于 spacetimedb-client-rust

此技能为使用 SpacetimeDB SDK 实现 Rust 客户端提供指导,涵盖生成绑定、连接与订阅生命周期、表回调和 reducer 调用;服务端工作则指向其他技能。

主要功能

  1. 01生成 Rust 客户端绑定
  2. 02配置连接与订阅
  3. 03推进运行时并更新缓存
  4. 04读取订阅行并使用索引
  5. 05注册表回调并调用 reducer

适用场景

  1. 01为 SpacetimeDB 模块构建 Rust 客户端
  2. 02在客户端处理订阅与表更新
  3. 03使生成的绑定与 Rust 项目保持一致

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/danmossa/spacetimedb-skills --skill spacetimedb-client-rust

安装后在 Claude Code 中输入/spacetimedb-client-rust确认已加载。

附带文件references/clients-rust.md

详细介绍

使用此技能实现 Rust 客户端。

Rust 核心模式

生成 Rust 绑定,并从本地模块导入生成的名称:

spacetime generate --lang rust --out-dir client/src/module_bindings --module-path spacetimedb
use module_bindings::DbConnection;

let conn = DbConnection::builder()
    .with_uri("ws://localhost:3000")
    .with_database_name("my-database")
    .on_connect(|ctx, identity, _token| {
        log::info!("connected: {identity}");
        ctx.subscription_builder().subscribe_to_all_tables();
    })
    .on_connect_error(|_ctx, err| log::error!("connect error: {err}"))
    .on_disconnect(|_ctx, err| log::info!("disconnected: {err:?}"))
    .build()?;

conn.run_threaded();

运行时规则

  • 使用 run_threaded、run_async 或 frame_tick 推进连接;回调和缓存更新需要调用其中之一。
  • 通过 conn.db.<table>().iter()、.count() 和生成的唯一索引句柄读取已订阅的行。
  • 在生成的表句柄上注册 on_insert、on_delete 和 on_update 回调;on_update 需要主键。
  • 使用 conn.reducers.<reducer>(...),或在 trait 泛型回调上下文中使用 ctx.reducers()。
  • 保持生成的绑定模块名称和功能标志与本地项目一致。

参考索引

需求打开
Rust SDK APIreferences/clients-rust.md

指南

  • 使用本地项目中的生成模块名称和绑定路径。
  • 确保连接生命周期、订阅回调和 reducer 调用与 Rust SDK 参考保持一致。
  • Rust 服务模块的 schema 或 reducer 代码应交由表和 reducer 技能处理,而不是此客户端技能。

评论

支持 Markdown