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:
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]>) -> SumType
pub const fn new(variants: Box<[SumTypeVariant]>) -> SumType
Returns a sum type with these possible variants.
Sourcepub fn new_unnamed(types: Box<[AlgebraicType]>) -> SumType
pub fn new_unnamed(types: Box<[AlgebraicType]>) -> SumType
Returns a sum type of unnamed variants taken from types.
Sourcepub fn as_option(&self) -> Option<&AlgebraicType>
pub fn as_option(&self) -> Option<&AlgebraicType>
Check whether this sum type is a structural option type.
A structural 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.
Note that some and none are lowercase, unlike Rust’s Option.
Order matters, and an option type with these variants in the opposite order will not be recognized.
If the type does look like a structural option type, returns the type T.
Sourcepub fn as_option_mut(&mut self) -> Option<&mut AlgebraicType>
pub fn as_option_mut(&mut self) -> Option<&mut AlgebraicType>
Check whether this sum type is a structural option type.
A structural 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.
Note that some and none are lowercase, unlike Rust’s Option.
Order matters, and an option type with these variants in the opposite order will not be recognized.
If the type does look like a structural option type, returns the type T.
Sourcepub fn is_option(&self) -> bool
pub fn is_option(&self) -> bool
Check whether this sum type is a structural option type.
A structural 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.
Note that some and none are lowercase, unlike Rust’s Option.
Order matters, and an option type with these variants in the opposite order will not be recognized.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Return whether this sum type is empty, that is, has no variants.
Sourcepub fn is_schedule_at(&self) -> bool
pub fn is_schedule_at(&self) -> bool
Return whether this sum type is the special ScheduleAt type,
Interval(u64) | Time(u64).
Does not follow Refs.
Sourcepub fn is_special(&self) -> bool
pub fn is_special(&self) -> bool
Returns whether this sum type is a special known type, currently Option or ScheduleAt.
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.
pub fn type_check(&self, sv: &SumValue, typespace: &Typespace) -> bool
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: D,
) -> Result<SumType, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<SumType, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
deserializer.Source§impl<'de> Deserialize<'de> for SumType
impl<'de> Deserialize<'de> for SumType
Source§fn deserialize<D>(
deserializer: D,
) -> Result<SumType, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<SumType, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
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
Source§impl Serialize for SumType
impl Serialize for SumType
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 SumType
impl Serialize for SumType
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 SumType
impl SpacetimeType for SumType
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 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§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.