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 0b94d19
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
32 changes: 21 additions & 11 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 @@ -108,18 +109,17 @@ pub(crate) fn least_upper_bound(mut tags: impl Iterator<Item = TypeBound>) -> Ty
/// 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 {
#[allow(missing_docs)]
#[display(fmt = "SimplePredicate({})", "size")]
Simple {
size: u8,
},
General {
row: TypeRow,
},
Simple {size: u8 },
#[allow(missing_docs)]
General {row: TypeRow },
}

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 +130,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 @@ -150,10 +151,13 @@ impl From<SumType> for Type {

#[derive(Clone, PartialEq, Debug, Eq, derive_more::Display)]
/// Core types: primitive (leaf), tuple (product) or sum (co-product).
enum TypeEnum {
pub enum TypeEnum {
#[allow(missing_docs)]
Prim(PrimType),
#[allow(missing_docs)]
#[display(fmt = "Tuple({})", "_0")]
Tuple(TypeRow),
#[allow(missing_docs)]
#[display(fmt = "Sum({})", "_0")]
Sum(SumType),
}
Expand Down Expand Up @@ -262,6 +266,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
9 changes: 7 additions & 2 deletions src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ use super::{CustomType, FunctionType, TypeBound};
#[derive(
Clone, PartialEq, Debug, Eq, derive_more::Display, serde::Serialize, serde::Deserialize,
)]
pub(super) enum PrimType {
/// Representation of a Primitive type, i.e. neither a Sum nor a Tuple.
pub enum PrimType {
// TODO optimise with Box<CustomType> ?
// or some static version of this?
#[allow(missing_docs)]
Extension(CustomType),
#[allow(missing_docs)]
#[display(fmt = "Alias({})", "_0.name()")]
Alias(AliasDecl),
#[allow(missing_docs)]
#[display(fmt = "Function({})", "_0")]
Function(Box<FunctionType>),
}

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 0b94d19

Please sign in to comment.