Skip to content

Commit

Permalink
feat: Make TypeEnum and it's contents public
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed Sep 27, 2023
1 parent ab79fac commit 82f2060
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
21 changes: 16 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod type_row;
pub use check::{ConstTypeError, CustomCheckFailure};
pub use custom::CustomType;
pub use signature::{FunctionType, Signature, SignatureDescription};
pub use type_param::TypeArg;
pub use type_row::TypeRow;

use derive_more::{From, Into};
Expand All @@ -23,7 +24,7 @@ use crate::ops::AliasDecl;
use crate::type_row;
use std::fmt::Debug;

use self::primitive::PrimType;
pub use self::primitive::PrimType;

#[cfg(feature = "pyo3")]
use pyo3::pyclass;
Expand Down Expand Up @@ -103,12 +104,13 @@ pub(crate) fn least_upper_bound(mut tags: impl Iterator<Item = TypeBound>) -> Ty
.into_inner()
}

#[allow(missing_docs)]
#[derive(Clone, PartialEq, Debug, Eq, derive_more::Display, Serialize, Deserialize)]
#[serde(tag = "s")]
/// Representation of a Sum type.
/// Either store the types of the variants, or in the special (but common) case
/// of a "simple predicate" (sum over empty tuples), store only the size of the predicate.
enum SumType {
pub enum SumType {
#[display(fmt = "SimplePredicate({})", "size")]
Simple {
size: u8,
Expand All @@ -119,7 +121,8 @@ enum SumType {
}

impl SumType {
fn new(types: impl Into<TypeRow>) -> Self {
/// Initialize a new sum type.
pub fn new(types: impl Into<TypeRow>) -> Self {
let row: TypeRow = types.into();

let len: usize = row.len();
Expand All @@ -130,7 +133,8 @@ impl SumType {
}
}

fn get_variant(&self, tag: usize) -> Option<&Type> {
/// Report the tag'th variant, if it exists.
pub fn get_variant(&self, tag: usize) -> Option<&Type> {
match self {
SumType::Simple { size } if tag < (*size as usize) => Some(Type::UNIT_REF),
SumType::General { row } => row.get(tag),
Expand All @@ -148,9 +152,10 @@ impl From<SumType> for Type {
}
}

#[allow(missing_docs)]
#[derive(Clone, PartialEq, Debug, Eq, derive_more::Display)]
/// Core types: primitive (leaf), tuple (product) or sum (co-product).
enum TypeEnum {
pub enum TypeEnum {
Prim(PrimType),
#[display(fmt = "Tuple({})", "_0")]
Tuple(TypeRow),
Expand Down Expand Up @@ -262,6 +267,12 @@ impl Type {
self.1
}

/// Report the component TypeEnum.
#[inline(always)]
pub const fn as_type_enum(&self) -> &TypeEnum {
&self.0
}

/// Report if the type is copyable - i.e.the least upper bound of the type
/// is contained by the copyable bound.
pub const fn copyable(&self) -> bool {
Expand Down
5 changes: 5 additions & 0 deletions src/types/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ pub enum ConstTypeError {
}

impl PrimType {
/// Check that a [`PrimValue`] is a valid instance of this [`PrimType`].
///
/// # Errors
///
/// This function will return an error if there is a type check error.
pub fn check_type(&self, val: &PrimValue) -> Result<(), ConstTypeError> {
if let PrimType::Alias(alias) = self {
return Err(ConstTypeError::NoAliases(alias.name().to_string()));
Expand Down
6 changes: 4 additions & 2 deletions src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::ops::AliasDecl;

use super::{CustomType, FunctionType, TypeBound};

#[allow(missing_docs)]
#[derive(
Clone, PartialEq, Debug, Eq, derive_more::Display, serde::Serialize, serde::Deserialize,
)]
pub(super) enum PrimType {
pub enum PrimType {
// TODO optimise with Box<CustomType> ?
// or some static version of this?
Extension(CustomType),
Expand All @@ -18,7 +19,8 @@ pub(super) enum PrimType {
}

impl PrimType {
pub(super) fn bound(&self) -> TypeBound {
/// Returns the bound of this [`PrimType`].
pub fn bound(&self) -> TypeBound {
match self {
PrimType::Extension(c) => c.bound(),
PrimType::Alias(a) => a.bound,
Expand Down

0 comments on commit 82f2060

Please sign in to comment.