spacetimedb_sats/
typespace.rs

1use std::any::TypeId;
2use std::ops::{Index, IndexMut};
3use std::rc::Rc;
4use std::sync::Arc;
5
6use crate::algebraic_type::AlgebraicType;
7use crate::algebraic_type_ref::AlgebraicTypeRef;
8use crate::WithTypespace;
9
10/// An error that occurs when attempting to resolve a type.
11#[derive(thiserror::Error, Debug, PartialOrd, Ord, PartialEq, Eq)]
12pub enum TypeRefError {
13    // TODO: ideally this should give some useful type name or path.
14    // Figure out if we can provide that even though it's not encoded in SATS.
15    #[error("Found recursive type reference {0}")]
16    RecursiveTypeRef(AlgebraicTypeRef),
17
18    #[error("Type reference {0} out of bounds")]
19    InvalidTypeRef(AlgebraicTypeRef),
20}
21
22/// A `Typespace` represents the typing context in SATS.
23///
24/// That is, this is the `Δ` or `Γ` you'll see in type theory literature.
25///
26/// We use (sort of) [deBrujin indices](https://en.wikipedia.org/wiki/De_Bruijn_index)
27/// to represent our type variables.
28/// Notably however, these are given for the entire module
29/// and there are no universal quantifiers (i.e., `Δ, α ⊢ τ | Δ ⊢ ∀ α. τ`)
30/// nor are there type lambdas (i.e., `Λτ. v`).
31/// See [System F], the second-order lambda calculus, for more on `∀` and `Λ`.
32///
33/// There are however recursive types in SATs,
34/// e.g., `&0 = { Cons({ v: U8, t: &0 }), Nil }` represents a basic cons list
35/// where `&0` is the type reference at index `0`.
36///
37/// [System F]: https://en.wikipedia.org/wiki/System_F
38#[derive(Clone, SpacetimeType)]
39#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
40#[sats(crate = crate)]
41pub struct Typespace {
42    /// The types in our typing context that can be referred to with [`AlgebraicTypeRef`]s.
43    pub types: Vec<AlgebraicType>,
44}
45
46impl std::fmt::Debug for Typespace {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        f.write_str("Typespace ")?;
49        f.debug_list().entries(&self.types).finish()
50    }
51}
52
53impl Default for Typespace {
54    fn default() -> Self {
55        Self::new(Vec::new())
56    }
57}
58
59impl Index<AlgebraicTypeRef> for Typespace {
60    type Output = AlgebraicType;
61
62    fn index(&self, index: AlgebraicTypeRef) -> &Self::Output {
63        &self.types[index.idx()]
64    }
65}
66impl IndexMut<AlgebraicTypeRef> for Typespace {
67    fn index_mut(&mut self, index: AlgebraicTypeRef) -> &mut Self::Output {
68        &mut self.types[index.idx()]
69    }
70}
71
72impl Typespace {
73    pub const EMPTY: &'static Typespace = &Self::new(Vec::new());
74
75    /// Returns a context ([`Typespace`]) with the given `types`.
76    pub const fn new(types: Vec<AlgebraicType>) -> Self {
77        Self { types }
78    }
79
80    /// Returns the [`AlgebraicType`] referred to by `r` within this context.
81    pub fn get(&self, r: AlgebraicTypeRef) -> Option<&AlgebraicType> {
82        self.types.get(r.idx())
83    }
84
85    /// Returns a mutable reference to the [`AlgebraicType`] referred to by `r` within this context.
86    pub fn get_mut(&mut self, r: AlgebraicTypeRef) -> Option<&mut AlgebraicType> {
87        self.types.get_mut(r.idx())
88    }
89
90    /// Inserts an `AlgebraicType` into the typespace
91    /// and returns an `AlgebraicTypeRef` that refers to the inserted `AlgebraicType`.
92    ///
93    /// This allows for self referential,
94    /// recursive or other complex types to be declared in the typespace.
95    ///
96    /// You can also use this to later change the meaning of the returned `AlgebraicTypeRef`
97    /// when you cannot provide the full definition of the type yet.
98    ///
99    /// Panics if the number of type references exceeds an `u32`.
100    pub fn add(&mut self, ty: AlgebraicType) -> AlgebraicTypeRef {
101        let index = self
102            .types
103            .len()
104            .try_into()
105            .expect("ran out of space for `AlgebraicTypeRef`s");
106
107        self.types.push(ty);
108        AlgebraicTypeRef(index)
109    }
110
111    /// Returns `ty` combined with the context `self`.
112    pub const fn with_type<'a, T: ?Sized>(&'a self, ty: &'a T) -> WithTypespace<'a, T> {
113        WithTypespace::new(self, ty)
114    }
115
116    /// Returns the `AlgebraicType` that `r` resolves to in the context of the `Typespace`.
117    ///
118    /// Panics if `r` is not known by the `Typespace`.
119    ///
120    /// Note, this is not recursive.
121    /// To resolve all nested refs, call `resolve_refs()` on the result.
122    pub fn resolve(&self, r: AlgebraicTypeRef) -> WithTypespace<'_, AlgebraicType> {
123        self.with_type(&self[r])
124    }
125
126    /// Inlines all type references in `ty` recursively using the current typeset.
127    pub fn inline_typerefs_in_type(&mut self, ty: &mut AlgebraicType) -> Result<(), TypeRefError> {
128        match ty {
129            AlgebraicType::Sum(sum_ty) => {
130                for variant in &mut *sum_ty.variants {
131                    self.inline_typerefs_in_type(&mut variant.algebraic_type)?;
132                }
133            }
134            AlgebraicType::Product(product_ty) => {
135                for element in &mut *product_ty.elements {
136                    self.inline_typerefs_in_type(&mut element.algebraic_type)?;
137                }
138            }
139            AlgebraicType::Array(array_ty) => {
140                self.inline_typerefs_in_type(&mut array_ty.elem_ty)?;
141            }
142            AlgebraicType::Ref(r) => {
143                // Lazily resolve any nested references first.
144                let resolved_ty = self.inline_typerefs_in_ref(*r)?;
145                // Now we can clone the fully-resolved type.
146                *ty = resolved_ty.clone();
147            }
148            _ => {}
149        }
150        Ok(())
151    }
152
153    /// Inlines all nested references behind the current [`AlgebraicTypeRef`] recursively using the current typeset.
154    ///
155    /// Returns the fully-resolved type or an error if the type reference is invalid or self-referential.
156    fn inline_typerefs_in_ref(&mut self, r: AlgebraicTypeRef) -> Result<&AlgebraicType, TypeRefError> {
157        let resolved_ty = match self.get_mut(r) {
158            None => return Err(TypeRefError::InvalidTypeRef(r)),
159            // If we encountered a type reference, that means one of the parent calls
160            // to `inline_typerefs_in_ref(r)` swapped its definition out,
161            // i.e. the type referred to by `r` is recursive.
162            // Note that it doesn't necessarily need to be the current call,
163            // e.g. A -> B -> A dependency also forms a recursive cycle.
164            // Our database can't handle recursive types, so return an error.
165            // TODO: support recursive types in the future.
166            Some(AlgebraicType::Ref(_)) => return Err(TypeRefError::RecursiveTypeRef(r)),
167            Some(resolved_ty) => resolved_ty,
168        };
169        // First, swap the type with a reference.
170        // This allows us to:
171        // 1. Recurse into each type mutably while holding a mutable
172        //    reference to the typespace as well, without cloning.
173        // 2. Easily detect self-references at arbitrary depth without
174        //    having to keep a separate `seen: HashSet<_>` or something.
175        let mut resolved_ty = std::mem::replace(resolved_ty, AlgebraicType::Ref(r));
176        // Next, recurse into the type and inline any nested type references.
177        self.inline_typerefs_in_type(&mut resolved_ty)?;
178        // Resolve the place again, since we couldn't hold the mutable reference across the call above.
179        let place = &mut self[r];
180        // Now we can put the fully-resolved type back and return that place.
181        *place = resolved_ty;
182        Ok(place)
183    }
184
185    /// Inlines all type references in the typespace recursively.
186    ///
187    /// Errors out if any type reference is invalid or self-referential.
188    pub fn inline_all_typerefs(&mut self) -> Result<(), TypeRefError> {
189        // We need to use indices here to allow mutable reference on each iteration.
190        for r in 0..self.types.len() as u32 {
191            self.inline_typerefs_in_ref(AlgebraicTypeRef(r))?;
192        }
193        Ok(())
194    }
195
196    /// Iterate over types in the typespace with their references.
197    pub fn refs_with_types(&self) -> impl Iterator<Item = (AlgebraicTypeRef, &AlgebraicType)> {
198        self.types
199            .iter()
200            .enumerate()
201            .map(|(idx, ty)| (AlgebraicTypeRef(idx as _), ty))
202    }
203
204    /// Check that the entire typespace is valid for generating a `SpacetimeDB` client module.
205    /// See also the `spacetimedb_schema` crate, which layers additional validation on top
206    /// of these checks.
207    ///
208    /// All types in the typespace must either satisfy
209    /// [`is_valid_for_client_type_definition`](AlgebraicType::is_valid_for_client_type_definition) or
210    /// [`is_valid_for_client_type_use`](AlgebraicType::is_valid_for_client_type_use).
211    /// (Only the types that are `valid_for_client_type_definition` will have types generated in
212    /// the client, but the other types are allowed for the convenience of module binding codegen.)
213    pub fn is_valid_for_client_code_generation(&self) -> bool {
214        self.types
215            .iter()
216            .all(|ty| ty.is_valid_for_client_type_definition() || ty.is_valid_for_client_type_use())
217    }
218}
219
220impl FromIterator<AlgebraicType> for Typespace {
221    fn from_iter<T: IntoIterator<Item = AlgebraicType>>(iter: T) -> Self {
222        Self {
223            types: iter.into_iter().collect(),
224        }
225    }
226}
227
228/// A trait for Rust types that can be represented as an [`AlgebraicType`]
229/// with an empty typing context.
230///
231/// The returned `AlgebraicType` must have no free variables,
232/// that is, no `AlgebraicTypeRef`s in its tree at all.
233pub trait GroundSpacetimeType {
234    /// Returns the `AlgebraicType` representation of `Self`.
235    fn get_type() -> AlgebraicType;
236}
237
238/// This trait makes types self-describing, allowing them to automatically register their structure
239/// with SpacetimeDB. This is used to tell SpacetimeDB about the structure of a module's tables and
240/// reducers.
241///
242/// Deriving this trait also derives [`Serialize`](crate::ser::Serialize), [`Deserialize`](crate::de::Deserialize),
243/// and [`Debug`](std::fmt::Debug). (There are currently no trait bounds on `SpacetimeType` documenting this fact.)
244/// `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.
245///
246/// 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:
247///
248/// ```rust
249/// # use spacetimedb_sats::SpacetimeType;
250///
251/// #[derive(SpacetimeType)]
252/// # #[sats(crate = spacetimedb_sats)]
253/// struct Location {
254///     x: u64,
255///     y: u64
256/// }
257///
258/// #[derive(SpacetimeType)]
259/// # #[sats(crate = spacetimedb_sats)]
260/// struct PlasticCrate {
261///     count: u32,
262/// }
263///
264/// #[derive(SpacetimeType)]
265/// # #[sats(crate = spacetimedb_sats)]
266/// struct AppleCrate {
267///     variety: String,
268///     count: u32,
269///     freshness: u32,
270/// }
271///
272/// #[derive(SpacetimeType)]
273/// # #[sats(crate = spacetimedb_sats)]
274/// enum FruitCrate {
275///     Apples(AppleCrate),
276///     Plastic(PlasticCrate),
277/// }
278/// ```
279///
280/// The fields of the struct/enum must also implement `SpacetimeType`.
281///
282/// Any type annotated with `#[table(..)]` automatically derives `SpacetimeType`.
283///
284/// SpacetimeType is implemented for many of the primitive types in the standard library:
285///
286/// - `bool`
287/// - `u8`, `u16`, `u32`, `u64`, `u128`
288/// - `i8`, `i16`, `i32`, `i64`, `i128`
289/// - `f32`, `f64`
290///
291/// And common data structures:
292///
293/// - `String` and `&str`, utf-8 string data
294/// - `()`, the unit type
295/// - `Option<T> where T: SpacetimeType`
296/// - `Result<T, E> where T: SpacetimeType, E: SpacetimeType`
297/// - `Vec<T> where T: SpacetimeType`
298///
299/// (Storing collections in rows of a database table is a form of [denormalization](https://en.wikipedia.org/wiki/Denormalization).)
300///
301/// Do not manually implement this trait unless you are VERY sure you know what you're doing.
302/// Implementations must be consistent with `Deerialize<'de> for T`, `Serialize for T` and `Serialize, Deserialize for AlgebraicValue`.
303/// Implementations that are inconsistent across these traits may result in data loss.
304///
305/// N.B.: It's `SpacetimeType`, not `SpaceTimeType`.
306// TODO: we might want to have a note about what to do if you're trying to use a type from another crate in your table.
307// keep this note in sync with the ones on spacetimedb::rt::{ReducerArg, TableColumn}
308#[diagnostic::on_unimplemented(note = "if you own the type, try adding `#[derive(SpacetimeType)]` to its definition")]
309pub trait SpacetimeType {
310    /// Returns an `AlgebraicType` representing the type for `Self` in SATS
311    /// and in the typing context in `typespace`. This is used by the
312    /// automatic type registration system in Rust modules.
313    ///
314    /// The resulting `AlgebraicType` may contain `Ref`s that only make sense
315    /// within the context of this particular `typespace`.
316    fn make_type<S: TypespaceBuilder>(typespace: &mut S) -> AlgebraicType;
317}
318
319use ethnum::{i256, u256};
320use smallvec::SmallVec;
321pub use spacetimedb_bindings_macro::SpacetimeType;
322
323/// A trait for types that can build a [`Typespace`].
324pub trait TypespaceBuilder {
325    /// Returns and adds a representation of type `T: 'static` as an [`AlgebraicType`]
326    /// with an optional `name` to the typing context in `self`.
327    fn add(
328        &mut self,
329        typeid: TypeId,
330        name: Option<&'static str>,
331        make_ty: impl FnOnce(&mut Self) -> AlgebraicType,
332    ) -> AlgebraicType;
333
334    fn add_type<T: SpacetimeType>(&mut self) -> AlgebraicType
335    where
336        Self: Sized,
337    {
338        T::make_type(self)
339    }
340}
341
342/// Implements [`SpacetimeType`] for a type in a simplified manner.
343///
344/// An example:
345/// ```ignore
346/// struct Foo<'a, T>(&'a T, u8);
347/// impl_st!(
348/// //     Type parameters      Impl type
349/// //            v                 v
350/// //   --------------------  ----------
351///     ['a, T: SpacetimeType] Foo<'a, T>,
352/// //  The `make_type` implementation where `ts: impl TypespaceBuilder`
353/// //  and the expression right of `=>` is an `AlgebraicType`.
354///     ts => AlgebraicType::product([T::make_type(ts), AlgebraicType::U8])
355/// );
356/// ```
357#[macro_export]
358macro_rules! impl_st {
359    ([ $($generic_wrapped:ident $($other_generics:tt)*)? ] $rty:ty, $stty:expr) => {
360        impl<$($generic_wrapped $($other_generics)*)?> $crate::GroundSpacetimeType for $rty
361            $(where $generic_wrapped: $crate::GroundSpacetimeType)?
362        {
363            fn get_type() -> $crate::AlgebraicType {
364                $stty
365            }
366        }
367
368        impl_st!([ $($generic $($other_generics)*)? ] $rty, _ts => $stty);
369    };
370    ([ $($generic_wrapped:ident $($other_generics:tt)*)? ] $rty:ty, $ts:ident => $stty:expr) => {
371        impl<$($generic_wrapped $($other_generics)*)?> $crate::SpacetimeType for $rty
372            $(where $generic_wrapped: $crate::SpacetimeType)?
373        {
374            fn make_type<S: $crate::typespace::TypespaceBuilder>($ts: &mut S) -> $crate::AlgebraicType {
375                $stty
376            }
377        }
378    };
379}
380
381macro_rules! impl_primitives {
382    ($($t:ty => $x:ident,)*) => {
383        $(impl_st!([] $t, AlgebraicType::$x);)*
384    };
385}
386
387impl_primitives! {
388    bool => Bool,
389    u8 => U8,
390    i8 => I8,
391    u16 => U16,
392    i16 => I16,
393    u32 => U32,
394    i32 => I32,
395    u64 => U64,
396    i64 => I64,
397    u128 => U128,
398    i128 => I128,
399    u256 => U256,
400    i256 => I256,
401    f32 => F32,
402    f64 => F64,
403    String => String,
404}
405
406impl_st!([](), AlgebraicType::unit());
407impl_st!([] str, AlgebraicType::String);
408impl_st!([T] [T], ts => AlgebraicType::array(T::make_type(ts)));
409impl_st!([T: ?Sized] &T, ts => T::make_type(ts));
410impl_st!([T: ?Sized] Box<T>, ts => T::make_type(ts));
411impl_st!([T: ?Sized] Rc<T>, ts => T::make_type(ts));
412impl_st!([T: ?Sized] Arc<T>, ts => T::make_type(ts));
413impl_st!([T] Vec<T>, ts => <[T]>::make_type(ts));
414impl_st!([T, const N: usize] SmallVec<[T; N]>, ts => <[T]>::make_type(ts));
415impl_st!([T] Option<T>, ts => AlgebraicType::option(T::make_type(ts)));
416
417impl_st!([] spacetimedb_primitives::ArgId, AlgebraicType::U64);
418impl_st!([] spacetimedb_primitives::ColId, AlgebraicType::U16);
419impl_st!([] spacetimedb_primitives::TableId, AlgebraicType::U32);
420impl_st!([] spacetimedb_primitives::ViewId, AlgebraicType::U32);
421impl_st!([] spacetimedb_primitives::IndexId, AlgebraicType::U32);
422impl_st!([] spacetimedb_primitives::SequenceId, AlgebraicType::U32);
423impl_st!([] spacetimedb_primitives::ConstraintId, AlgebraicType::U32);
424impl_st!([] spacetimedb_primitives::ScheduleId, AlgebraicType::U32);
425
426impl_st!([] spacetimedb_primitives::ColList, ts => AlgebraicType::array(spacetimedb_primitives::ColId::make_type(ts)));
427impl_st!([] spacetimedb_primitives::ColSet, ts => AlgebraicType::array(spacetimedb_primitives::ColId::make_type(ts)));
428
429impl_st!([] bytes::Bytes, AlgebraicType::bytes());
430
431#[cfg(feature = "bytestring")]
432impl_st!([] bytestring::ByteString, AlgebraicType::String);
433
434impl<T, E> SpacetimeType for Result<T, E>
435where
436    T: SpacetimeType,
437    E: SpacetimeType,
438{
439    fn make_type<S: TypespaceBuilder>(typespace: &mut S) -> AlgebraicType {
440        AlgebraicType::result(T::make_type(typespace), E::make_type(typespace))
441    }
442}
443
444#[cfg(test)]
445mod tests {
446    use crate::proptest::generate_typespace_valid_for_codegen;
447    use proptest::prelude::*;
448
449    use super::*;
450
451    proptest! {
452        #![proptest_config(ProptestConfig::with_cases(512))]
453        #[test]
454        fn is_valid_for_client_code_generation(typespace in generate_typespace_valid_for_codegen(5)) {
455            prop_assert!(typespace.is_valid_for_client_code_generation());
456        }
457    }
458
459    #[test]
460    fn is_not_valid_for_client_code_generation() {
461        let bad_inner_1 = AlgebraicType::sum([("red", AlgebraicType::U8), ("green", AlgebraicType::U8)]);
462        let bad_inner_2 = AlgebraicType::product([("red", AlgebraicType::U8), ("green", AlgebraicType::U8)]);
463
464        fn assert_not_valid(ty: AlgebraicType) {
465            let typespace = Typespace::new(vec![ty.clone()]);
466            assert!(!typespace.is_valid_for_client_code_generation(), "{ty:?}");
467        }
468        assert_not_valid(AlgebraicType::product([AlgebraicType::U8, bad_inner_1.clone()]));
469        assert_not_valid(AlgebraicType::product([AlgebraicType::U8, bad_inner_2.clone()]));
470
471        assert_not_valid(AlgebraicType::sum([AlgebraicType::U8, bad_inner_1.clone()]));
472        assert_not_valid(AlgebraicType::sum([AlgebraicType::U8, bad_inner_2.clone()]));
473
474        assert_not_valid(AlgebraicType::array(bad_inner_1.clone()));
475        assert_not_valid(AlgebraicType::array(bad_inner_2.clone()));
476
477        assert_not_valid(AlgebraicType::option(bad_inner_1.clone()));
478        assert_not_valid(AlgebraicType::option(bad_inner_2.clone()));
479
480        assert_not_valid(AlgebraicType::option(AlgebraicType::array(AlgebraicType::option(
481            bad_inner_1.clone(),
482        ))));
483
484        assert_not_valid(AlgebraicType::result(bad_inner_1.clone(), AlgebraicType::U8));
485        assert_not_valid(AlgebraicType::result(AlgebraicType::U8, bad_inner_2.clone()));
486
487        assert_not_valid(AlgebraicType::result(
488            AlgebraicType::array(AlgebraicType::result(bad_inner_1.clone(), AlgebraicType::U8)),
489            AlgebraicType::U8,
490        ));
491
492        assert_not_valid(AlgebraicType::result(
493            AlgebraicType::U8,
494            AlgebraicType::array(AlgebraicType::result(AlgebraicType::U8, bad_inner_2.clone())),
495        ));
496    }
497}