pub struct SumType {
    pub variants: Vec<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: Vec<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

source

pub fn decode<'a>( bytes: &mut impl BufReader<'a> ) -> Result<SumType, DecodeError>

source

pub fn encode(&self, bytes: &mut impl BufWriter)

source§

impl SumType

source

pub const fn new(variants: Vec<SumTypeVariant>) -> SumType

Returns a sum type with these possible variants.

source

pub fn new_unnamed(types: Vec<AlgebraicType>) -> SumType

Returns a sum type of unnamed variants taken from types.

source

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.

source

pub fn is_simple_enum(&self) -> bool

Returns whether this sum type is like on in C without data attached to the variants.

source§

impl SumType

Trait Implementations§

source§

impl Clone for SumType

source§

fn clone(&self) -> SumType

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for SumType

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for SumType

source§

fn deserialize<D>( deserializer: D ) -> Result<SumType, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,

Deserialize this value from the given deserializer.
source§

impl<'de> Deserialize<'de> for SumType

source§

fn deserialize<D>( deserializer: D ) -> Result<SumType, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<const N: usize> From<[(&str, AlgebraicType); N]> for SumType

source§

fn from(fields: [(&str, AlgebraicType); N]) -> SumType

Converts to this type from the input type.
source§

impl<const N: usize> From<[(Option<&str>, AlgebraicType); N]> for SumType

source§

fn from(fields: [(Option<&str>, AlgebraicType); N]) -> SumType

Converts to this type from the input type.
source§

impl<const N: usize> From<[AlgebraicType; N]> for SumType

source§

fn from(fields: [AlgebraicType; N]) -> SumType

Converts to this type from the input type.
source§

impl<const N: usize> From<[SumTypeVariant; N]> for SumType

source§

fn from(fields: [SumTypeVariant; N]) -> SumType

Converts to this type from the input type.
source§

impl From<SumType> for AlgebraicType

source§

fn from(original: SumType) -> AlgebraicType

Converts to this type from the input type.
source§

impl From<Vec<SumTypeVariant>> for SumType

source§

fn from(fields: Vec<SumTypeVariant>) -> SumType

Converts to this type from the input type.
source§

impl Hash for SumType

source§

fn hash<__H>(&self, state: &mut __H)where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl MetaType for SumType

source§

fn meta_type() -> AlgebraicType

Returns the type structure of this type as an AlgebraicType.
source§

impl Ord for SumType

source§

fn cmp(&self, other: &SumType) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for SumType

source§

fn eq(&self, other: &SumType) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for SumType

source§

fn partial_cmp(&self, other: &SumType) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for SumType

source§

fn serialize<S>( &self, __serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,

Serialize self in the data format of S using the provided serializer.
source§

impl Serialize for SumType

source§

fn serialize<S>( &self, serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for SumType

source§

impl StructuralEq for SumType

source§

impl StructuralPartialEq for SumType

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> Satn for Twhere T: Serialize + ?Sized,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the SATN data format into the formatter f.
source§

fn fmt_psql(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the postgres SATN data format into the formatter f.
source§

fn to_satn(&self) -> String

Formats the value using the SATN data format into the returned String.
source§

fn to_satn_pretty(&self) -> String

Pretty prints the value using the SATN data format into the returned String.
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,

source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,