SpacetimeDB icon indicating copy to clipboard operation
SpacetimeDB copied to clipboard

Using non-primitive rust types doesn’t work

Open bfops opened this issue 9 months ago • 1 comments

If we try to use a type that isn’t a primitive rust type and it’s not a stdb table/tuple, the project won’t compile. Here is an example of using a uuid, which is defined in the uuid crate:

#[spacetimedb(table(1))]
pub struct MyStruct {
	my_uuid: uuid,
}

The reason this doesn’t compile, is that the macro generated functions for the table will want to be able to grab the schema for this type (because it knows its not a rust primitive)

error[E0425]: cannot find function `__struct_to_tuple__uuid` in this scope
  --> crates/bitcraft-mini/src/lib.rs:15:1
   |
15 | #[spacetimedb(table(1))]
   | ^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
   |
   = note: this error originates in the attribute macro `spacetimedb` (in Nightly builds, run with -Z macro-backtrace for more info)

Best solution I could think of: treat this type as a json object. The TupleDef type for this field will just be string, and when we serialize/deserialize this type we just do it using json. As long as the type supports serde serialization we should be okay.

Alternate solution: We need a way for someone to add support for this type, likely by allowing them to specify a struct ⇒ TupleDef and TupleDef ⇒ struct func (plus any other required functions for that type).

Note: Because implement TupleTypeand similar hits error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate, then is useful to list with foreign types we could implement (like chrono, uuid, ...)

bfops avatar Mar 07 '25 18:03 bfops

@jdetter declares: there is no general solution to this problem. This ticket is to figure out best practices and how to communicate them to users.

gefjon avatar Mar 12 '25 19:03 gefjon