Skip to content

Commit 2c0d4e3

Browse files
Error on enums with explicit discriminant values in SpacetimeType
SpacetimeDB's SATS encoding assigns variant tags by declaration order, ignoring Rust discriminant values. An enum like: #[derive(SpacetimeType)] enum Foo { A = 5, B = 10 } would silently encode A as tag 0 and B as tag 1, which is confusing and error-prone. Emit a compile error instead. Also removes explicit discriminant values from two internal enums (UnsubscribeFlags, IndexType) that already matched the default tag order.
1 parent 4d7db10 commit 2c0d4e3

3 files changed

Lines changed: 10 additions & 4 deletions

File tree

  • crates
    • bindings-macro/src
    • client-api-messages/src/websocket
    • lib/src/db/raw_def

crates/bindings-macro/src/sats.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ pub(crate) fn sats_type_from_derive(
6464
}
6565
syn::Data::Enum(enu) => {
6666
let variants = enu.variants.iter().map(|var| {
67+
if let Some((eq, _)) = &var.discriminant {
68+
return Err(syn::Error::new(
69+
eq.span(),
70+
"explicit discriminant values are not supported by SpacetimeDB",
71+
));
72+
}
6773
let (member, ty) = variant_data(var)?.unzip();
6874
Ok(SatsVariant {
6975
ident: &var.ident,

crates/client-api-messages/src/websocket/v2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ pub struct Unsubscribe {
8787
#[sats(crate = spacetimedb_lib)]
8888
pub enum UnsubscribeFlags {
8989
#[default]
90-
Default = 0,
90+
Default,
9191
// If set, the server will send the full set of rows to be removed from the client cache.
92-
SendDroppedRows = 1,
92+
SendDroppedRows,
9393
}
9494

9595
/// Sent by the client to perform a query at a single point in time.

crates/lib/src/db/raw_def/v8.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ impl RawSequenceDefV8 {
8383
#[sats(crate = crate)]
8484
pub enum IndexType {
8585
/// A BTree index.
86-
BTree = 0,
86+
BTree,
8787
/// A Hash index.
88-
Hash = 1,
88+
Hash,
8989
}
9090

9191
impl From<IndexType> for u8 {

0 commit comments

Comments
 (0)