pub trait SpacetimeType {
// Required method
fn make_type<S>(typespace: &mut S) -> AlgebraicType
where S: TypespaceBuilder;
}Expand description
This trait makes types self-describing, allowing them to automatically register their structure with SpacetimeDB. This is used to tell SpacetimeDB about the structure of a module’s tables and reducers.
Deriving this trait also derives Serialize, Deserialize,
and Debug. (There are currently no trait bounds on SpacetimeType documenting this fact.)
Serialize and Deserialize are used to convert Rust data structures to other formats, suitable for storing on disk or passing over the network. Debug is simply for debugging convenience.
Any Rust type implementing SpacetimeType can be used as a table column or reducer argument. A derive macro is provided, and can be used on both structs and enums:
#[derive(SpacetimeType)]
struct Location {
x: u64,
y: u64
}
#[derive(SpacetimeType)]
struct PlasticCrate {
count: u32,
}
#[derive(SpacetimeType)]
struct AppleCrate {
variety: String,
count: u32,
freshness: u32,
}
#[derive(SpacetimeType)]
enum FruitCrate {
Apples(AppleCrate),
Plastic(PlasticCrate),
}The fields of the struct/enum must also implement SpacetimeType.
Any type annotated with #[table(..)] automatically derives SpacetimeType.
SpacetimeType is implemented for many of the primitive types in the standard library:
boolu8,u16,u32,u64,u128i8,i16,i32,i64,i128f32,f64
And common data structures:
Stringand&str, utf-8 string data(), the unit typeOption<T> where T: SpacetimeTypeVec<T> where T: SpacetimeType
(Storing collections in rows of a database table is a form of denormalization.)
Do not manually implement this trait unless you are VERY sure you know what you’re doing.
Implementations must be consistent with Deerialize<'de> for T, Serialize for T and Serialize, Deserialize for AlgebraicValue.
Implementations that are inconsistent across these traits may result in data loss.
N.B.: It’s SpacetimeType, not SpaceTimeType.
Required Methods§
Sourcefn make_type<S>(typespace: &mut S) -> AlgebraicTypewhere
S: TypespaceBuilder,
fn make_type<S>(typespace: &mut S) -> AlgebraicTypewhere
S: TypespaceBuilder,
Returns an AlgebraicType representing the type for Self in SATS
and in the typing context in typespace. This is used by the
automatic type registration system in Rust modules.
The resulting AlgebraicType may contain Refs that only make sense
within the context of this particular typespace.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.