pub enum AlgebraicType {
Show 20 variants
Ref(AlgebraicTypeRef),
Sum(SumType),
Product(ProductType),
Array(ArrayType),
String,
Bool,
I8,
U8,
I16,
U16,
I32,
U32,
I64,
U64,
I128,
U128,
I256,
U256,
F32,
F64,
}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, scalar value types, and convenience types strings, arrays, and maps, into a single type system.
Variants§
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.
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:
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:
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).
Array(ArrayType)
The type of array values where elements are of a base type elem_ty.
Values AlgebraicValue::Array(array) will have this type.
String
The UTF-8 encoded String type.
Values AlgebraicValue::String(s) will have this type.
This type exists for convenience and because it is easy to just use Rust’s String (UTF-8)
as opposed to rolling your own equivalent byte-array based UTF-8 encoding.
Bool
The bool type. Values AlgebraicValue::Bool(b) will have this type.
I8
The I8 type. Values AlgebraicValue::I8(v) will have this type.
U8
The U8 type. Values AlgebraicValue::U8(v) will have this type.
I16
The I16 type. Values AlgebraicValue::I16(v) will have this type.
U16
The U16 type. Values AlgebraicValue::U16(v) will have this type.
I32
The I32 type. Values AlgebraicValue::I32(v) will have this type.
U32
The U32 type. Values AlgebraicValue::U32(v) will have this type.
I64
The I64 type. Values AlgebraicValue::I64(v) will have this type.
U64
The U64 type. Values AlgebraicValue::U64(v) will have this type.
I128
The I128 type. Values AlgebraicValue::I128(v) will have this type.
U128
The U128 type. Values AlgebraicValue::U128(v) will have this type.
I256
The I256 type. Values AlgebraicValue::I256(v) will have this type.
U256
The U256 type. Values AlgebraicValue::U256(v) will have this type.
F32
The F32 type. Values AlgebraicValue::F32(v) will have this type.
F64
The F64 type. Values AlgebraicValue::F64(v) will have this type.
Implementations§
Source§impl AlgebraicType
impl AlgebraicType
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
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 as_array_mut(&mut self) -> Option<&mut ArrayType>
pub fn as_array_mut(&mut self) -> Option<&mut ArrayType>
Optionally returns mutable references to the inner fields if this is a AlgebraicType::Array, otherwise None
Sourcepub fn as_array(&self) -> Option<&ArrayType>
pub fn as_array(&self) -> Option<&ArrayType>
Optionally returns references to the inner fields if this is a AlgebraicType::Array, otherwise None
Sourcepub fn into_array(self) -> Result<ArrayType, AlgebraicType>
pub fn into_array(self) -> Result<ArrayType, AlgebraicType>
Returns the inner fields if this is a AlgebraicType::Array, 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 fn is_connection_id(&self) -> bool
pub fn is_connection_id(&self) -> bool
Returns whether this type is the ConnectionId type.
Construct an instance of this type with Self::connection_id
Sourcepub fn is_identity(&self) -> bool
pub fn is_identity(&self) -> bool
Returns whether this type is the conventional identity type.
Sourcepub fn is_timestamp(&self) -> bool
pub fn is_timestamp(&self) -> bool
Returns whether this type is the conventional point-in-time Timestamp type.
Sourcepub fn is_time_duration(&self) -> bool
pub fn is_time_duration(&self) -> bool
Returns whether this type is the conventional time-delta TimeDuration type.
Sourcepub fn is_schedule_at(&self) -> bool
pub fn is_schedule_at(&self) -> bool
Returns whether this type is the conventional ScheduleAt type.
Sourcepub fn as_option(&self) -> Option<&AlgebraicType>
pub fn as_option(&self) -> Option<&AlgebraicType>
If this type is the standard option type, returns the type of the some variant.
Otherwise, returns None.
Sourcepub fn is_scalar_or_string(&self) -> bool
pub fn is_scalar_or_string(&self) -> bool
Returns whether this type is scalar or a string type.
Sourcepub fn is_scalar(&self) -> bool
pub fn is_scalar(&self) -> bool
Returns whether this type is one which holds a scalar value.
A scalar value is one not made up of other values, i.e., not composite.
These are all integer and float values,
i.e., integer and float types are scalar.
References to other types, i.e., AlgebraicType::Refs are not scalar.
Sourcepub fn is_unsigned(&self) -> bool
pub fn is_unsigned(&self) -> bool
Returns whether the type is an unsigned integer type.
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 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.
Sourcepub fn bytes() -> AlgebraicType
pub fn bytes() -> AlgebraicType
A type representing an array of U8s.
Sourcepub fn contains_refs(&self) -> bool
pub fn contains_refs(&self) -> bool
Whether this type, or the types it references, contain any AlgebraicTypeRefs.
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 identity() -> AlgebraicType
pub fn identity() -> AlgebraicType
Construct a copy of the Identity type.
Sourcepub fn connection_id() -> AlgebraicType
pub fn connection_id() -> AlgebraicType
Construct a copy of the ConnectionId type.
Sourcepub fn timestamp() -> AlgebraicType
pub fn timestamp() -> AlgebraicType
Construct a copy of the point-in-time Timestamp type.
Sourcepub fn time_duration() -> AlgebraicType
pub fn time_duration() -> AlgebraicType
Construct a copy of the time-delta TimeDuration type.
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.
Sourcepub fn is_special(&self) -> bool
pub fn is_special(&self) -> bool
Check if the type is one of a small number of special, known types
with specific layouts.
See also ProductType::is_special and SumType::is_special.
Sourcepub fn is_valid_for_client_type_definition(&self) -> bool
pub fn is_valid_for_client_type_definition(&self) -> bool
Validates that the type can be used to generate a type definition
in a SpacetimeDB client module.
Such a type must be a non-special sum or product type.
All of the elements of the type must satisfy AlgebraicType::is_valid_for_client_type_use.
This method does not actually follow Refs to check the types they point to,
it only checks the structure of this type.
Sourcepub fn is_valid_for_client_type_use(&self) -> bool
pub fn is_valid_for_client_type_use(&self) -> bool
Validates that the type can be used to generate a use of a type in a SpacetimeDB client module.
(As opposed to a definition of a type.)
This means that the type is either:
- a reference
- a special, known type
- a non-compound type like
U8,I32,F64, etc. - or a map, array, or option built from types that satisfy
AlgebraicType::is_valid_for_client_type_use
This method does not actually follow Refs to check the types they point to,
it only checks the structure of the type.
pub fn type_check(&self, value: &AlgebraicValue, typespace: &Typespace) -> bool
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 Default for AlgebraicType
Provided to enable mem::take.
impl Default for AlgebraicType
Provided to enable mem::take.
Source§fn default() -> AlgebraicType
fn default() -> 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 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(original: ArrayType) -> AlgebraicType
fn from(original: ArrayType) -> 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§impl PartialOrd for AlgebraicType
impl PartialOrd for AlgebraicType
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,
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,
Source§impl SpacetimeType for AlgebraicType
impl SpacetimeType for AlgebraicType
Source§fn make_type<S>(__typespace: &mut S) -> AlgebraicTypewhere
S: TypespaceBuilder,
fn make_type<S>(__typespace: &mut S) -> AlgebraicTypewhere
S: TypespaceBuilder,
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. Read moreimpl 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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> 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<'_>,
ty: &PsqlType<'_>,
) -> Result<(), Error>
fn fmt_psql( &self, f: &mut Formatter<'_>, ty: &PsqlType<'_>, ) -> 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.