Skip to content

Commit

Permalink
feat: new methods for leaf ops (#940)
Browse files Browse the repository at this point in the history
The struct definitions are now `non_exhaustive`, so they cannot be build
with the `Noop {ty: ..}` syntax outside of this crate.
  • Loading branch information
aborgna-q authored Apr 16, 2024
1 parent d69198e commit 33fea08
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions hugr/src/ops/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -50,6 +71,13 @@ pub struct Tag {
pub variants: Vec<TypeRow>,
}

impl Tag {
/// Create a new Tag operation.
pub fn new(tag: usize, variants: Vec<TypeRow>) -> 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)]
Expand All @@ -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);
Expand Down

0 comments on commit 33fea08

Please sign in to comment.