Struct spacetimedb_sats::sum_type::SumType
source · pub struct SumType {
pub variants: Box<[SumTypeVariant]>,
}
Expand description
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.
Fields§
§variants: Box<[SumTypeVariant]>
The possible variants of the sum type.
The order is relevant as it defines the tags of the variants at runtime.
Implementations§
source§impl SumType
impl SumType
sourcepub const fn new(variants: Box<[SumTypeVariant]>) -> Self
pub const fn new(variants: Box<[SumTypeVariant]>) -> Self
Returns a sum type with these possible variants
.
sourcepub fn new_unnamed(types: Box<[AlgebraicType]>) -> Self
pub fn new_unnamed(types: Box<[AlgebraicType]>) -> Self
Returns a sum type of unnamed variants taken from types
.
sourcepub fn as_option(&self) -> Option<&AlgebraicType>
pub fn as_option(&self) -> Option<&AlgebraicType>
Returns whether this sum type looks like an option type.
An option type has some(T)
as its first variant and none
as its second.
That is, { some(T), none }
or some: T | none
depending on your notation.
sourcepub fn is_simple_enum(&self) -> bool
pub fn is_simple_enum(&self) -> bool
Returns whether this sum type is like on in C without data attached to the variants.
sourcepub fn get_variant(&self, tag_name: &str) -> Option<(u8, &SumTypeVariant)>
pub fn get_variant(&self, tag_name: &str) -> Option<(u8, &SumTypeVariant)>
Returns the sum type variant using tag_name
with their tag position.
sourcepub fn get_variant_simple(
&self,
tag_name: &str,
) -> Option<(u8, &SumTypeVariant)>
pub fn get_variant_simple( &self, tag_name: &str, ) -> Option<(u8, &SumTypeVariant)>
Returns the sum type variant using tag_name
with their tag position, if this is a Self::is_simple_enum
sourcepub fn get_variant_by_tag(&self, tag: u8) -> Option<&SumTypeVariant>
pub fn get_variant_by_tag(&self, tag: u8) -> Option<&SumTypeVariant>
Returns the sum type variant with the given tag
.
source§impl SumType
impl SumType
pub fn as_value(&self) -> AlgebraicValue
pub fn from_value( value: &AlgebraicValue, ) -> Result<SumType, ValueDeserializeError>
Trait Implementations§
source§impl<'de> Deserialize<'de> for SumType
impl<'de> Deserialize<'de> for SumType
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<const N: usize> From<[AlgebraicType; N]> for SumType
impl<const N: usize> From<[AlgebraicType; N]> for SumType
source§fn from(fields: [AlgebraicType; N]) -> Self
fn from(fields: [AlgebraicType; N]) -> Self
source§impl<const N: usize> From<[SumTypeVariant; N]> for SumType
impl<const N: usize> From<[SumTypeVariant; N]> for SumType
source§fn from(fields: [SumTypeVariant; N]) -> Self
fn from(fields: [SumTypeVariant; N]) -> Self
source§impl From<Box<[SumTypeVariant]>> for SumType
impl From<Box<[SumTypeVariant]>> for SumType
source§fn from(fields: Box<[SumTypeVariant]>) -> Self
fn from(fields: Box<[SumTypeVariant]>) -> Self
source§impl From<SumType> for AlgebraicType
impl From<SumType> for AlgebraicType
source§fn from(original: SumType) -> AlgebraicType
fn from(original: SumType) -> AlgebraicType
source§impl MetaType for SumType
impl MetaType for SumType
source§fn meta_type() -> AlgebraicType
fn meta_type() -> AlgebraicType
AlgebraicType
.source§impl Ord for SumType
impl Ord for SumType
source§impl PartialOrd for SumType
impl PartialOrd for SumType
impl Eq for SumType
impl StructuralPartialEq for SumType
Auto Trait Implementations§
impl Freeze for SumType
impl RefUnwindSafe for SumType
impl Send for SumType
impl Sync for SumType
impl Unpin for SumType
impl UnwindSafe for SumType
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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
fn fmt(&self, f: &mut Formatter<'_>) -> Result
f
.source§fn fmt_psql(&self, f: &mut Formatter<'_>, ty: &ProductType) -> Result
fn fmt_psql(&self, f: &mut Formatter<'_>, ty: &ProductType) -> 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
.