Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add From impls for TypeArg #1002

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hugr/src/extension/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ pub const USIZE_T: Type = Type::new_extension(USIZE_CUSTOM_T);
pub const BOOL_T: Type = Type::new_unit_sum(2);

/// Initialize a new array of element type `element_ty` of length `size`
pub fn array_type(size: TypeArg, element_ty: Type) -> Type {
pub fn array_type(size: impl Into<TypeArg>, element_ty: Type) -> Type {
let array_def = PRELUDE.get_type("array").unwrap();
let custom_t = array_def
.instantiate(vec![size, TypeArg::Type { ty: element_ty }])
.instantiate(vec![size.into(), element_ty.into()])
.unwrap();
Type::new_extension(custom_t)
}
Expand Down
8 changes: 4 additions & 4 deletions hugr/src/std_extensions/arithmetic/int_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ pub const EXTENSION_ID: ExtensionId = ExtensionId::new_unchecked("arithmetic.int
/// Identifier for the integer type.
pub const INT_TYPE_ID: TypeName = TypeName::new_inline("int");

pub(crate) fn int_custom_type(width_arg: TypeArg) -> CustomType {
CustomType::new(INT_TYPE_ID, [width_arg], EXTENSION_ID, TypeBound::Eq)
pub(crate) fn int_custom_type(width_arg: impl Into<TypeArg>) -> CustomType {
CustomType::new(INT_TYPE_ID, [width_arg.into()], EXTENSION_ID, TypeBound::Eq)
}

/// Integer type of a given bit width (specified by the TypeArg).
/// Depending on the operation, the semantic interpretation may be unsigned integer, signed integer
/// or bit string.
pub(super) fn int_type(width_arg: TypeArg) -> Type {
Type::new_extension(int_custom_type(width_arg))
pub(super) fn int_type(width_arg: impl Into<TypeArg>) -> Type {
Type::new_extension(int_custom_type(width_arg.into()))
}

lazy_static! {
Expand Down
36 changes: 30 additions & 6 deletions hugr/src/types/type_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,6 @@ impl From<UpperBound> for TypeParam {
}
}

impl From<Type> for TypeArg {
fn from(ty: Type) -> Self {
Self::Type { ty }
}
}

/// A statically-known argument value to an operation.
#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[non_exhaustive]
Expand Down Expand Up @@ -177,6 +171,36 @@ pub enum TypeArg {
},
}

impl From<Type> for TypeArg {
fn from(ty: Type) -> Self {
Self::Type { ty }
}
}

impl From<u64> for TypeArg {
fn from(n: u64) -> Self {
Self::BoundedNat { n }
}
}

impl From<CustomTypeArg> for TypeArg {
fn from(arg: CustomTypeArg) -> Self {
Self::Opaque { arg }
}
}

impl From<Vec<TypeArg>> for TypeArg {
fn from(elems: Vec<TypeArg>) -> Self {
Self::Sequence { elems }
}
}

impl From<ExtensionSet> for TypeArg {
fn from(es: ExtensionSet) -> Self {
Self::Extensions { es }
}
}

/// Variable in a TypeArg, that is not a [TypeArg::Type] or [TypeArg::Extensions],
#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct TypeArgVariable {
Expand Down