From 33fea082c88dcbed5c3af0b03301356bc4ac6ebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Borgna?= <121866228+aborgna-q@users.noreply.github.com> Date: Tue, 16 Apr 2024 13:07:22 +0200 Subject: [PATCH] feat: `new` methods for leaf ops (#940) The struct definitions are now `non_exhaustive`, so they cannot be build with the `Noop {ty: ..}` syntax outside of this crate. --- hugr/src/ops/leaf.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/hugr/src/ops/leaf.rs b/hugr/src/ops/leaf.rs index 178ded03a..b39d73324 100644 --- a/hugr/src/ops/leaf.rs +++ b/hugr/src/ops/leaf.rs @@ -18,6 +18,13 @@ pub struct Noop { pub ty: Type, } +impl Noop { + /// Create a new Noop operation. + pub fn new(ty: Type) -> Self { + Self { ty } + } +} + impl Default for Noop { fn default() -> Self { Self { ty: Type::UNIT } @@ -32,6 +39,13 @@ pub struct MakeTuple { pub tys: TypeRow, } +impl MakeTuple { + /// Create a new MakeTuple operation. + pub fn new(tys: TypeRow) -> Self { + Self { tys } + } +} + /// An operation that unpacks a tuple into its components. #[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[non_exhaustive] @@ -40,6 +54,13 @@ pub struct UnpackTuple { pub tys: TypeRow, } +impl UnpackTuple { + /// Create a new UnpackTuple operation. + pub fn new(tys: TypeRow) -> Self { + Self { tys } + } +} + /// An operation that creates a tagged sum value from one of its variants. #[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[non_exhaustive] @@ -50,6 +71,13 @@ pub struct Tag { pub variants: Vec, } +impl Tag { + /// Create a new Tag operation. + pub fn new(tag: usize, variants: Vec) -> Self { + Self { tag, variants } + } +} + /// A node which adds a extension req to the types of the wires it is passed /// It has no effect on the values passed along the edge #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] @@ -61,6 +89,16 @@ pub struct Lift { pub new_extension: ExtensionId, } +impl Lift { + /// Create a new Lift operation. + pub fn new(type_row: TypeRow, new_extension: ExtensionId) -> Self { + Self { + type_row, + new_extension, + } + } +} + impl_op_name!(Noop); impl_op_name!(MakeTuple); impl_op_name!(UnpackTuple);