pub enum AlgebraicType {
    Sum(SumType),
    Product(ProductType),
    Builtin(BuiltinType),
    Ref(AlgebraicTypeRef),
}
Expand description

The SpacetimeDB Algebraic Type System (SATS) is a structural type system in which a nominal type system can be constructed.

The type system unifies the concepts sum types, product types, and built-in primitive types into a single type system.

Below are some common types you might implement in this type system.

type Unit = () // or (,) or , Product with zero elements
type Never = (|) // or | Sum with zero elements
type U8 = U8 // Builtin
type Foo = (foo: I8) != I8
type Bar = (bar: I8)
type Color = (a: I8 | b: I8) // Sum with one element
type Age = (age: U8) // Product with one element
type Option<T> = (some: T | none: ())
type Ref = &0

type AlgebraicType = (sum: SumType | product: ProductType | builtin: BuiltinType | set: AlgebraicType)
type Catalog<T> = (name: String, indices: Set<Set<Tag>>, relation: Set<>)
type CatalogEntry = { name: string, indexes: {some type}, relation: Relation }
type ElementValue = (tag: Tag, value: AlgebraicValue)
type AlgebraicValue = (sum: ElementValue | product: {ElementValue} | builtin: BuiltinValue | set: {AlgebraicValue})
type Any = (value: Bytes, type: AlgebraicType)

type Table<Row: ProductType> = (
    rows: Array<Row>
)

type HashSet<T> = (
    array: Array<T>
)

type BTreeSet<T> = (
    array: Array<T>
)

type TableType<Row: ProductType> = (
    relation: Table<Row>,
    indexes: Array<(index_type: String)>,
)

Variants§

§

Sum(SumType)

A structural sum type.

Unlike most languages, sums in SATs are structural and not nominal. When checking whether two nominal types are the same, their names and/or declaration sites (e.g., module / namespace) are considered. Meanwhile, a structural type system would only check the structure of the type itself, e.g., the names of its variants and their inner data types in the case of a sum.

This is also known as a discriminated union (implementation) or disjoint union. Another name is coproduct (category theory).

These structures are known as sum types because the number of possible values a sum

{ N_0(T_0), N_1(T_1), ..., N_n(T_n) }

is:

Σ (i ∈ 0..n). values(T_i)

so for example, values({ A(U64), B(Bool) }) = values(U64) + values(Bool).

See also: https://ncatlab.org/nlab/show/sum+type.

§

Product(ProductType)

A structural product type.

This is also known as struct and tuple in many languages, but note that unlike most languages, sums in SATs are structural and not nominal. When checking whether two nominal types are the same, their names and/or declaration sites (e.g., module / namespace) are considered. Meanwhile, a structural type system would only check the structure of the type itself, e.g., the names of its fields and their types in the case of a record. The name “product” comes from category theory.

See also: https://ncatlab.org/nlab/show/product+type.

These structures are known as product types because the number of possible values in product

{ N_0: T_0, N_1: T_1, ..., N_n: T_n }

is:

Π (i ∈ 0..n). values(T_i)

so for example, values({ A: U64, B: Bool }) = values(U64) * values(Bool).

§

Builtin(BuiltinType)

A bulltin type, e.g., bool.

§

Ref(AlgebraicTypeRef)

A type where the definition is given by the typing context (Typespace). In other words, this is defined by a pointer to another AlgebraicType.

This should not be conflated with reference and pointer types in languages like Rust, In other words, this is not &T or *const T.

Implementations§

source§

impl AlgebraicType

source

pub fn is_sum(&self) -> bool

Returns true if this is a AlgebraicType::Sum, otherwise false

source

pub fn as_sum_mut(&mut self) -> Option<&mut SumType>

Optionally returns mutable references to the inner fields if this is a AlgebraicType::Sum, otherwise None

source

pub fn as_sum(&self) -> Option<&SumType>

Optionally returns references to the inner fields if this is a AlgebraicType::Sum, otherwise None

source

pub fn into_sum(self) -> Result<SumType, Self>

Returns the inner fields if this is a AlgebraicType::Sum, otherwise returns back the enum in the Err case of the result

source

pub fn is_product(&self) -> bool

Returns true if this is a AlgebraicType::Product, otherwise false

source

pub fn as_product_mut(&mut self) -> Option<&mut ProductType>

Optionally returns mutable references to the inner fields if this is a AlgebraicType::Product, otherwise None

source

pub fn as_product(&self) -> Option<&ProductType>

Optionally returns references to the inner fields if this is a AlgebraicType::Product, otherwise None

source

pub fn into_product(self) -> Result<ProductType, Self>

Returns the inner fields if this is a AlgebraicType::Product, otherwise returns back the enum in the Err case of the result

source

pub fn is_builtin(&self) -> bool

Returns true if this is a AlgebraicType::Builtin, otherwise false

source

pub fn as_builtin_mut(&mut self) -> Option<&mut BuiltinType>

Optionally returns mutable references to the inner fields if this is a AlgebraicType::Builtin, otherwise None

source

pub fn as_builtin(&self) -> Option<&BuiltinType>

Optionally returns references to the inner fields if this is a AlgebraicType::Builtin, otherwise None

source

pub fn into_builtin(self) -> Result<BuiltinType, Self>

Returns the inner fields if this is a AlgebraicType::Builtin, otherwise returns back the enum in the Err case of the result

source

pub fn is_ref(&self) -> bool

Returns true if this is a AlgebraicType::Ref, otherwise false

source

pub fn as_ref_mut(&mut self) -> Option<&mut AlgebraicTypeRef>

Optionally returns mutable references to the inner fields if this is a AlgebraicType::Ref, otherwise None

source

pub fn as_ref(&self) -> Option<&AlgebraicTypeRef>

Optionally returns references to the inner fields if this is a AlgebraicType::Ref, otherwise None

source

pub fn into_ref(self) -> Result<AlgebraicTypeRef, Self>

Returns the inner fields if this is a AlgebraicType::Ref, otherwise returns back the enum in the Err case of the result

source§

impl AlgebraicType

source

pub const ZERO_REF: Self = _

The first type in the typespace.

source

pub const Bool: Self = _

The built-in Bool type.

source

pub const I8: Self = _

The built-in signed 8-bit integer type.

source

pub const U8: Self = _

The built-in unsigned 8-bit integer type.

source

pub const I16: Self = _

The built-in signed 16-bit integer type.

source

pub const U16: Self = _

The built-in unsigned 16-bit integer type.

source

pub const I32: Self = _

The built-in signed 32-bit integer type.

source

pub const U32: Self = _

The built-in unsigned 32-bit integer type.

source

pub const I64: Self = _

The built-in signed 64-bit integer type.

source

pub const U64: Self = _

The built-in unsigned 64-bit integer type.

source

pub const I128: Self = _

The built-in signed 128-bit integer type.

source

pub const U128: Self = _

The built-in unsigned 128-bit integer type.

source

pub const F32: Self = _

The built-in 32-bit floating point type.

source

pub const F64: Self = _

The built-in 64-bit floating point type.

source

pub const String: Self = _

The built-in string type.

source

pub fn unit() -> Self

The canonical 0-element unit type.

source

pub fn never() -> Self

The canonical 0-variant “never” / “absurd” / “void” type.

source§

impl AlgebraicType

source

pub fn bytes() -> Self

A type representing an array of U8s.

source

pub fn is_bytes(&self) -> bool

Returns whether this type is AlgebraicType::bytes().

source

pub fn is_integer(&self) -> bool

Returns whether this type is one of the integer types, e.g., U64 and I32.

source

pub fn sum<S: Into<SumType>>(sum: S) -> Self

Returns a sum type with the given sum.

source

pub fn product<P: Into<ProductType>>(prod: P) -> Self

Returns a product type with the given prod.

source

pub fn option(some_type: Self) -> Self

Returns a structural option type where some_type is the type for the some variant.

source

pub fn array(ty: Self) -> Self

Returns an unsized array type where the element type is ty.

source

pub fn map(key: Self, value: Self) -> Self

Returns a map type from the type key to the type value.

source

pub fn simple_enum<'a>(var_names: impl Iterator<Item = &'a str>) -> Self

Returns a sum type of unit variants with names taken from var_names.

source

pub fn as_value(&self) -> AlgebraicValue

source

pub fn from_value(value: &AlgebraicValue) -> Result<Self, ValueDeserializeError>

source

pub fn min_value(&self) -> Option<AlgebraicValue>

Given an AlgebraicType, returns the min value for that type.

source

pub fn max_value(&self) -> Option<AlgebraicValue>

Given an AlgebraicType, returns the max value for that type.

source§

impl AlgebraicType

source

pub fn decode<'a>(bytes: &mut impl BufReader<'a>) -> Result<Self, DecodeError>

source

pub fn encode(&self, bytes: &mut impl BufWriter)

Trait Implementations§

source§

impl Clone for AlgebraicType

source§

fn clone(&self) -> AlgebraicType

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for AlgebraicType

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for AlgebraicType

source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given deserializer.
source§

impl From<AlgebraicType> for Header

source§

fn from(value: AlgebraicType) -> Self

Converts to this type from the input type.
source§

impl From<AlgebraicType> for ProductType

source§

fn from(x: AlgebraicType) -> Self

Converts to this type from the input type.
source§

impl From<AlgebraicType> for ProductTypeElement

source§

fn from(value: AlgebraicType) -> Self

Converts to this type from the input type.
source§

impl From<AlgebraicType> for SumTypeVariant

source§

fn from(algebraic_type: AlgebraicType) -> Self

Converts to this type from the input type.
source§

impl From<AlgebraicTypeRef> for AlgebraicType

source§

fn from(original: AlgebraicTypeRef) -> AlgebraicType

Converts to this type from the input type.
source§

impl From<ArrayType> for AlgebraicType

source§

fn from(x: ArrayType) -> Self

Converts to this type from the input type.
source§

impl From<BuiltinType> for AlgebraicType

source§

fn from(original: BuiltinType) -> AlgebraicType

Converts to this type from the input type.
source§

impl From<MapType> for AlgebraicType

source§

fn from(x: MapType) -> Self

Converts to this type from the input type.
source§

impl From<ProductType> for AlgebraicType

source§

fn from(original: ProductType) -> AlgebraicType

Converts to this type from the input type.
source§

impl From<SumType> for AlgebraicType

source§

fn from(original: SumType) -> AlgebraicType

Converts to this type from the input type.
source§

impl Hash for AlgebraicType

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl MetaType for AlgebraicType

source§

fn meta_type() -> Self

This is a static function that constructs the type of AlgebraicType and returns it as an AlgebraicType.

This could alternatively be implemented as a regular AlgebraicValue or as a static variable.

source§

impl Ord for AlgebraicType

source§

fn cmp(&self, other: &AlgebraicType) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for AlgebraicType

source§

fn eq(&self, other: &AlgebraicType) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for AlgebraicType

source§

fn partial_cmp(&self, other: &AlgebraicType) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for AlgebraicType

source§

fn serialize<S: Serializer>(&self, __serializer: S) -> Result<S::Ok, S::Error>

Serialize self in the data format of S using the provided serializer.
source§

impl Eq for AlgebraicType

source§

impl StructuralPartialEq for AlgebraicType

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> Satn for T
where T: Serialize + ?Sized,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the SATN data format into the formatter f.
source§

fn fmt_psql(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the postgres SATN data format into the formatter f.
source§

fn to_satn(&self) -> String

Formats the value using the SATN data format into the returned String.
source§

fn to_satn_pretty(&self) -> String

Pretty prints the value using the SATN data format into the returned String.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,