Skip to content

More granular table traits#4775

Open
onx2 wants to merge 7 commits intoclockworklabs:masterfrom
onx2:onx2/table-traits
Open

More granular table traits#4775
onx2 wants to merge 7 commits intoclockworklabs:masterfrom
onx2:onx2/table-traits

Conversation

@onx2
Copy link
Copy Markdown

@onx2 onx2 commented Apr 10, 2026

Description of Changes

This looks like a large diff but the real changes are just sdks/rust/src/lib.rs for traits and crates/codegen/src/rust.rs for the new codegen.

Refactored the Rust SDK table trait surface into composable capabilities and updated Rust codegen to match. Discord thread

Summary

  • Added a shared TableLike trait for common table handle functionality:
    • Row
    • EventContext
    • count
    • iter
  • Added capability traits:
    • WithInsert
    • WithDelete
    • WithUpdate
  • Changed umbrella traits to compose those capabilities:
    • Table: TableLike + WithInsert + WithDelete
    • TableWithPrimaryKey: Table + WithUpdate
    • EventTable: TableLike + WithInsert
  • Updated Rust binding codegen to emit separate impl blocks for:
    • TableLike
    • WithInsert
    • WithDelete
    • WithUpdate
    • plus empty umbrella impls for Table, TableWithPrimaryKey, and EventTable
  • Updated generated-doc references for callback APIs
  • Exported the new traits through both the Rust SDK public API and spacetimedb_sdk::__codegen so generated bindings can compile correctly

API and ABI breaking changes

Yes — this is an API-breaking change in the Rust SDK trait hierarchy.

Notable changes:

  • insert callbacks are now provided via WithInsert
  • delete callbacks are now provided via WithDelete
  • update callbacks are now provided via WithUpdate
  • generated Rust bindings now implement multiple capability traits instead of placing all methods in a single Table / EventTable impl block

Expected complexity level and risk

3/5

This is a moderate refactor because it changes the Rust SDK trait model, affects Rust code generation, and requires the generated binding surface and SDK re-export surface to stay in sync.

Main interaction points:

  • sdks/rust/src/table.rs
  • sdks/rust/src/lib.rs
  • crates/codegen/src/rust.rs

Testing

  • Ran cargo check --manifest-path /Users/onx2/Documents/projects/SpacetimeDB/sdks/rust/Cargo.toml
  • Ran cargo test --manifest-path /Users/onx2/Documents/projects/SpacetimeDB/Cargo.toml -p spacetimedb-codegen test_codegen_rust
  • Regenerated Rust client bindings using the workspace CLI and verified generated impls match the new trait structure
  • Reviewer: regenerate a Rust client with the workspace CLI and confirm the generated bindings compile cleanly

@onx2 onx2 marked this pull request as draft April 10, 2026 14:11
@onx2 onx2 changed the title updates More granular table traits Apr 10, 2026
Copy link
Copy Markdown
Author

@onx2 onx2 Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change to functionality. I wanted to have access to specific traits so in bevy_stdb I can go from:

Before/after

fn bind_insert<TRow, TTable>(world: &World, table: &TTable)
where
    TRow: Send + Sync + Clone + InModule + 'static,
    RowEvent<TRow>: Send + Sync,
-   TTable: Table<
+   TTable: WithInsert<
            Row = TRow,
            EventContext = <<TRow as InModule>::Module as SpacetimeModule>::EventContext,
        >,
    TTable::EventContext: AbstractEventContext<Event = RowEvent<TRow>>,
{
    let sender = channel_sender::<InsertMessage<TRow>>(world);
    table.on_insert(move |ctx, row| {
        let _ = sender.send(InsertMessage {
            event: ctx.event().clone(),
            row: row.clone(),
        });
    });
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trait changes mean we need updated codegen too (breaking).

@onx2 onx2 marked this pull request as ready for review April 10, 2026 15:35
Comment on lines +208 to +209

impl<'ctx> __sdk::EventTable for {table_handle}<'ctx> {{}}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need these empty impls? Maybe now Table can just be removed entirely?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant