From 20f642b7a18b1ca3cf90c30a21a99ff14e306552 Mon Sep 17 00:00:00 2001 From: doug-q <141026920+doug-q@users.noreply.github.com> Date: Wed, 29 May 2024 08:14:20 +0100 Subject: [PATCH] feat: Make internals of int ops and the "int" CustomType more public. (#1114) closes #1113. --- .pre-commit-config.yaml | 2 +- .../src/std_extensions/arithmetic/int_ops.rs | 37 +++++++++++-------- .../std_extensions/arithmetic/int_types.rs | 14 ++++--- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fa88a8a45..4813932be 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -78,7 +78,7 @@ repos: - id: cargo-doc name: cargo doc description: Generate documentation with `cargo doc`. - entry: cargo doc --no-deps --all-features --workspace + entry: RUSTDOCFLAGS=-Dwarnings cargo doc --no-deps --all-features --workspace language: system files: \.rs$ pass_filenames: false diff --git a/hugr-core/src/std_extensions/arithmetic/int_ops.rs b/hugr-core/src/std_extensions/arithmetic/int_ops.rs index 1219084e9..546b2b6c3 100644 --- a/hugr-core/src/std_extensions/arithmetic/int_ops.rs +++ b/hugr-core/src/std_extensions/arithmetic/int_ops.rs @@ -279,17 +279,22 @@ lazy_static! { /// Concrete integer operation with integer widths set. #[derive(Debug, Clone, PartialEq)] -pub struct IntOpType { - def: IntOpDef, - log_widths: Vec, +#[non_exhaustive] +pub struct ConcreteIntOp { + /// The kind of int op. + pub def: IntOpDef, + /// The width parameters of the int op. These are interpreted differently, + /// depending on `def`. The types of inputs and outputs of the op will have + /// [int_type]s of these widths. + pub log_widths: Vec, } -impl NamedOp for IntOpType { +impl NamedOp for ConcreteIntOp { fn name(&self) -> OpName { self.def.name() } } -impl MakeExtensionOp for IntOpType { +impl MakeExtensionOp for ConcreteIntOp { fn from_extension_op(ext_op: &ExtensionOp) -> Result { let def = IntOpDef::from_def(ext_op.def())?; let args = ext_op.args(); @@ -305,7 +310,7 @@ impl MakeExtensionOp for IntOpType { } } -impl MakeRegisteredOp for IntOpType { +impl MakeRegisteredOp for ConcreteIntOp { fn extension_id(&self) -> ExtensionId { EXTENSION_ID.to_owned() } @@ -316,26 +321,26 @@ impl MakeRegisteredOp for IntOpType { } impl IntOpDef { - /// Initialize a concrete [IntOpType] from a [IntOpDef] which requires no + /// Initialize a [ConcreteIntOp] from a [IntOpDef] which requires no /// integer widths set. - pub fn without_log_width(self) -> IntOpType { - IntOpType { + pub fn without_log_width(self) -> ConcreteIntOp { + ConcreteIntOp { def: self, log_widths: vec![], } } - /// Initialize a concrete [IntOpType] from a [IntOpDef] which requires one + /// Initialize a [ConcreteIntOp] from a [IntOpDef] which requires one /// integer width set. - pub fn with_log_width(self, log_width: u8) -> IntOpType { - IntOpType { + pub fn with_log_width(self, log_width: u8) -> ConcreteIntOp { + ConcreteIntOp { def: self, log_widths: vec![log_width], } } - /// Initialize a concrete [IntOpType] from a [IntOpDef] which requires two + /// Initialize a [ConcreteIntOp] from a [IntOpDef] which requires two /// integer widths set. - pub fn with_two_log_widths(self, first_log_width: u8, second_log_width: u8) -> IntOpType { - IntOpType { + pub fn with_two_log_widths(self, first_log_width: u8, second_log_width: u8) -> ConcreteIntOp { + ConcreteIntOp { def: self, log_widths: vec![first_log_width, second_log_width], } @@ -419,6 +424,6 @@ mod test { ); let ext_op = o.clone().to_extension_op().unwrap(); - assert_eq!(IntOpType::from_extension_op(&ext_op).unwrap(), o); + assert_eq!(ConcreteIntOp::from_extension_op(&ext_op).unwrap(), o); } } diff --git a/hugr-core/src/std_extensions/arithmetic/int_types.rs b/hugr-core/src/std_extensions/arithmetic/int_types.rs index 7b92f82a6..444daefc1 100644 --- a/hugr-core/src/std_extensions/arithmetic/int_types.rs +++ b/hugr-core/src/std_extensions/arithmetic/int_types.rs @@ -20,14 +20,17 @@ 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: impl Into) -> CustomType { +/// 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 fn int_custom_type(width_arg: impl Into) -> 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: impl Into) -> Type { +/// +/// Constructed from [int_custom_type]. +pub fn int_type(width_arg: impl Into) -> Type { Type::new_extension(int_custom_type(width_arg.into())) } @@ -40,7 +43,8 @@ lazy_static! { .unwrap(); } -const fn is_valid_log_width(n: u8) -> bool { +/// Returns whether `n` is a valid `log_width` for an [int_type]. +pub const fn is_valid_log_width(n: u8) -> bool { n < LOG_WIDTH_BOUND }