Enum spacetimedb_lib::type_def::AlgebraicType
source · 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, AlgebraicType>
pub fn into_sum(self) -> Result<SumType, AlgebraicType>
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, AlgebraicType>
pub fn into_product(self) -> Result<ProductType, AlgebraicType>
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, AlgebraicType>
pub fn into_builtin(self) -> Result<BuiltinType, AlgebraicType>
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, AlgebraicType>
pub fn into_ref(self) -> Result<AlgebraicTypeRef, AlgebraicType>
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
sourcepub const ZERO_REF: AlgebraicType = _
pub const ZERO_REF: AlgebraicType = _
The first type in the typespace.
sourcepub const Bool: AlgebraicType = _
pub const Bool: AlgebraicType = _
The built-in Bool type.
sourcepub const I8: AlgebraicType = _
pub const I8: AlgebraicType = _
The built-in signed 8-bit integer type.
sourcepub const U8: AlgebraicType = _
pub const U8: AlgebraicType = _
The built-in unsigned 8-bit integer type.
sourcepub const I16: AlgebraicType = _
pub const I16: AlgebraicType = _
The built-in signed 16-bit integer type.
sourcepub const U16: AlgebraicType = _
pub const U16: AlgebraicType = _
The built-in unsigned 16-bit integer type.
sourcepub const I32: AlgebraicType = _
pub const I32: AlgebraicType = _
The built-in signed 32-bit integer type.
sourcepub const U32: AlgebraicType = _
pub const U32: AlgebraicType = _
The built-in unsigned 32-bit integer type.
sourcepub const I64: AlgebraicType = _
pub const I64: AlgebraicType = _
The built-in signed 64-bit integer type.
sourcepub const U64: AlgebraicType = _
pub const U64: AlgebraicType = _
The built-in unsigned 64-bit integer type.
sourcepub const I128: AlgebraicType = _
pub const I128: AlgebraicType = _
The built-in signed 128-bit integer type.
sourcepub const U128: AlgebraicType = _
pub const U128: AlgebraicType = _
The built-in unsigned 128-bit integer type.
sourcepub const F32: AlgebraicType = _
pub const F32: AlgebraicType = _
The built-in 32-bit floating point type.
sourcepub const F64: AlgebraicType = _
pub const F64: AlgebraicType = _
The built-in 64-bit floating point type.
sourcepub const String: AlgebraicType = _
pub const String: AlgebraicType = _
The built-in string type.
sourcepub fn unit() -> AlgebraicType
pub fn unit() -> AlgebraicType
The canonical 0-element unit type.
sourcepub fn never() -> AlgebraicType
pub fn never() -> AlgebraicType
The canonical 0-variant “never” / “absurd” / “void” type.
source§impl AlgebraicType
impl AlgebraicType
sourcepub fn bytes() -> AlgebraicType
pub fn bytes() -> AlgebraicType
A type representing an array of U8
s.
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 sum<S>(sum: S) -> AlgebraicType
pub fn sum<S>(sum: S) -> AlgebraicType
Returns a sum type with the given sum
.
sourcepub fn product<P>(prod: P) -> AlgebraicTypewhere
P: Into<ProductType>,
pub fn product<P>(prod: P) -> AlgebraicTypewhere
P: Into<ProductType>,
Returns a product type with the given prod
.
sourcepub fn option(some_type: AlgebraicType) -> AlgebraicType
pub fn option(some_type: AlgebraicType) -> AlgebraicType
Returns a structural option type where some_type
is the type for the some
variant.
sourcepub fn array(ty: AlgebraicType) -> AlgebraicType
pub fn array(ty: AlgebraicType) -> AlgebraicType
Returns an unsized array type where the element type is ty
.
sourcepub fn map(key: AlgebraicType, value: AlgebraicType) -> AlgebraicType
pub fn map(key: AlgebraicType, value: AlgebraicType) -> AlgebraicType
Returns a map type from the type key
to the type value
.
sourcepub fn simple_enum<'a>(
var_names: impl Iterator<Item = &'a str>
) -> AlgebraicType
pub fn simple_enum<'a>( var_names: impl Iterator<Item = &'a str> ) -> AlgebraicType
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<AlgebraicType, 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.
source§impl AlgebraicType
impl AlgebraicType
pub fn decode<'a>( bytes: &mut impl BufReader<'a> ) -> Result<AlgebraicType, DecodeError>
pub fn encode(&self, bytes: &mut impl BufWriter)
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: D
) -> Result<AlgebraicType, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D
) -> Result<AlgebraicType, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
deserializer
.source§impl<'de> Deserialize<'de> for AlgebraicType
impl<'de> Deserialize<'de> for AlgebraicType
source§fn deserialize<D>(
deserializer: D
) -> Result<AlgebraicType, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D
) -> Result<AlgebraicType, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
source§impl From<AlgebraicType> for Header
impl From<AlgebraicType> for Header
source§fn from(value: AlgebraicType) -> Header
fn from(value: AlgebraicType) -> Header
source§impl From<AlgebraicType> for ProductType
impl From<AlgebraicType> for ProductType
source§fn from(x: AlgebraicType) -> ProductType
fn from(x: AlgebraicType) -> ProductType
source§impl From<AlgebraicType> for ProductTypeElement
impl From<AlgebraicType> for ProductTypeElement
source§fn from(value: AlgebraicType) -> ProductTypeElement
fn from(value: AlgebraicType) -> ProductTypeElement
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§fn from(x: ArrayType) -> AlgebraicType
fn from(x: ArrayType) -> 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§fn from(x: MapType) -> AlgebraicType
fn from(x: MapType) -> 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§fn meta_type() -> AlgebraicType
fn meta_type() -> AlgebraicType
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
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
source§fn serialize<S>(
&self,
__serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
__serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
self
in the data format of S
using the provided serializer
.source§impl Serialize for AlgebraicType
impl Serialize for AlgebraicType
source§fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl Eq for AlgebraicType
impl StructuralPartialEq for AlgebraicType
Auto Trait Implementations§
impl Freeze for AlgebraicType
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<T> Satn for T
impl<T> Satn for T
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
f
.source§fn fmt_psql(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn fmt_psql(&self, f: &mut Formatter<'_>) -> Result<(), Error>
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
.