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
impl AlgebraicType
sourcepub fn as_sum_mut(&mut self) -> Option<&mut SumType>
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
sourcepub fn as_sum(&self) -> Option<&SumType>
pub fn as_sum(&self) -> Option<&SumType>
Optionally returns references to the inner fields if this is a AlgebraicType::Sum
, otherwise None
sourcepub fn into_sum(self) -> Result<SumType, Self>
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
sourcepub fn is_product(&self) -> bool
pub fn is_product(&self) -> bool
Returns true if this is a AlgebraicType::Product
, otherwise false
sourcepub fn as_product_mut(&mut self) -> Option<&mut ProductType>
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
sourcepub fn as_product(&self) -> Option<&ProductType>
pub fn as_product(&self) -> Option<&ProductType>
Optionally returns references to the inner fields if this is a AlgebraicType::Product
, otherwise None
sourcepub fn into_product(self) -> Result<ProductType, Self>
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
sourcepub fn is_builtin(&self) -> bool
pub fn is_builtin(&self) -> bool
Returns true if this is a AlgebraicType::Builtin
, otherwise false
sourcepub fn as_builtin_mut(&mut self) -> Option<&mut BuiltinType>
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
sourcepub fn as_builtin(&self) -> Option<&BuiltinType>
pub fn as_builtin(&self) -> Option<&BuiltinType>
Optionally returns references to the inner fields if this is a AlgebraicType::Builtin
, otherwise None
sourcepub fn into_builtin(self) -> Result<BuiltinType, Self>
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
sourcepub fn as_ref_mut(&mut self) -> Option<&mut AlgebraicTypeRef>
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
sourcepub fn as_ref(&self) -> Option<&AlgebraicTypeRef>
pub fn as_ref(&self) -> Option<&AlgebraicTypeRef>
Optionally returns references to the inner fields if this is a AlgebraicType::Ref
, otherwise None
sourcepub fn into_ref(self) -> Result<AlgebraicTypeRef, Self>
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
impl AlgebraicType
source§impl AlgebraicType
impl AlgebraicType
sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Returns whether this type is one of the integer types, e.g., U64
and I32
.
sourcepub fn product<P: Into<ProductType>>(prod: P) -> Self
pub fn product<P: Into<ProductType>>(prod: P) -> Self
Returns a product type with the given prod
.
sourcepub fn option(some_type: Self) -> Self
pub fn option(some_type: Self) -> Self
Returns a structural option type where some_type
is the type for the some
variant.
sourcepub fn map(key: Self, value: Self) -> Self
pub fn map(key: Self, value: Self) -> Self
Returns a map type from the type key
to the type value
.
sourcepub fn simple_enum<'a>(var_names: impl Iterator<Item = &'a str>) -> Self
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
.
pub fn as_value(&self) -> AlgebraicValue
pub fn from_value(value: &AlgebraicValue) -> Result<Self, ValueDeserializeError>
sourcepub fn min_value(&self) -> Option<AlgebraicValue>
pub fn min_value(&self) -> Option<AlgebraicValue>
Given an AlgebraicType, returns the min value for that type.
sourcepub fn max_value(&self) -> Option<AlgebraicValue>
pub fn max_value(&self) -> Option<AlgebraicValue>
Given an AlgebraicType, returns the max value for that type.
Trait Implementations§
source§impl Clone for AlgebraicType
impl Clone for AlgebraicType
source§fn clone(&self) -> AlgebraicType
fn clone(&self) -> AlgebraicType
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for AlgebraicType
impl Debug for AlgebraicType
source§impl<'de> Deserialize<'de> for AlgebraicType
impl<'de> Deserialize<'de> for AlgebraicType
source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
deserializer
.source§impl From<AlgebraicType> for Header
impl From<AlgebraicType> for Header
source§fn from(value: AlgebraicType) -> Self
fn from(value: AlgebraicType) -> Self
source§impl From<AlgebraicType> for ProductType
impl From<AlgebraicType> for ProductType
source§fn from(x: AlgebraicType) -> Self
fn from(x: AlgebraicType) -> Self
source§impl From<AlgebraicType> for ProductTypeElement
impl From<AlgebraicType> for ProductTypeElement
source§fn from(value: AlgebraicType) -> Self
fn from(value: AlgebraicType) -> Self
source§impl From<AlgebraicType> for SumTypeVariant
impl From<AlgebraicType> for SumTypeVariant
source§fn from(algebraic_type: AlgebraicType) -> Self
fn from(algebraic_type: AlgebraicType) -> Self
source§impl From<AlgebraicTypeRef> for AlgebraicType
impl From<AlgebraicTypeRef> for AlgebraicType
source§fn from(original: AlgebraicTypeRef) -> AlgebraicType
fn from(original: AlgebraicTypeRef) -> AlgebraicType
source§impl From<ArrayType> for AlgebraicType
impl From<ArrayType> for AlgebraicType
source§impl From<BuiltinType> for AlgebraicType
impl From<BuiltinType> for AlgebraicType
source§fn from(original: BuiltinType) -> AlgebraicType
fn from(original: BuiltinType) -> AlgebraicType
source§impl From<MapType> for AlgebraicType
impl From<MapType> for AlgebraicType
source§impl From<ProductType> for AlgebraicType
impl From<ProductType> for AlgebraicType
source§fn from(original: ProductType) -> AlgebraicType
fn from(original: ProductType) -> AlgebraicType
source§impl From<SumType> for AlgebraicType
impl From<SumType> for AlgebraicType
source§fn from(original: SumType) -> AlgebraicType
fn from(original: SumType) -> AlgebraicType
source§impl Hash for AlgebraicType
impl Hash for AlgebraicType
source§impl MetaType for AlgebraicType
impl MetaType for AlgebraicType
source§impl Ord for AlgebraicType
impl Ord for AlgebraicType
source§fn cmp(&self, other: &AlgebraicType) -> Ordering
fn cmp(&self, other: &AlgebraicType) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl PartialEq for AlgebraicType
impl PartialEq for AlgebraicType
source§fn eq(&self, other: &AlgebraicType) -> bool
fn eq(&self, other: &AlgebraicType) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for AlgebraicType
impl PartialOrd for AlgebraicType
source§fn partial_cmp(&self, other: &AlgebraicType) -> Option<Ordering>
fn partial_cmp(&self, other: &AlgebraicType) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Serialize for AlgebraicType
impl Serialize for AlgebraicType
impl Eq for AlgebraicType
impl StructuralPartialEq for AlgebraicType
Auto Trait Implementations§
impl RefUnwindSafe for AlgebraicType
impl Send for AlgebraicType
impl Sync for AlgebraicType
impl Unpin for AlgebraicType
impl UnwindSafe for AlgebraicType
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Satn for T
impl<T> Satn for T
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
f
.source§fn fmt_psql(&self, f: &mut Formatter<'_>) -> Result
fn fmt_psql(&self, f: &mut Formatter<'_>) -> Result
f
.source§fn to_satn(&self) -> String
fn to_satn(&self) -> String
String
.source§fn to_satn_pretty(&self) -> String
fn to_satn_pretty(&self) -> String
String
.