Skip to content

Commit

Permalink
add HugrView impls for Box and Cow (#1730)
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q authored Dec 3, 2024
1 parent d878edd commit cf31c09
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
28 changes: 28 additions & 0 deletions hugr-core/src/hugr/internal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Internal traits, not exposed in the public `hugr` API.
use std::borrow::Cow;
use std::ops::Range;
use std::rc::Rc;
use std::sync::Arc;
Expand Down Expand Up @@ -112,6 +113,33 @@ impl<T: HugrInternals> HugrInternals for Arc<T> {
}
}

impl<T: HugrInternals> HugrInternals for Box<T> {
type Portgraph<'p>
= T::Portgraph<'p>
where
Self: 'p;
delegate! {
to (**self) {
fn portgraph(&self) -> Self::Portgraph<'_>;
fn base_hugr(&self) -> &Hugr;
fn root_node(&self) -> Node;
}
}
}

impl<T: HugrInternals + ToOwned> HugrInternals for Cow<'_, T> {
type Portgraph<'p>
= T::Portgraph<'p>
where
Self: 'p;
delegate! {
to self.as_ref() {
fn portgraph(&self) -> Self::Portgraph<'_>;
fn base_hugr(&self) -> &Hugr;
fn root_node(&self) -> Node;
}
}
}
/// Trait for accessing the mutable internals of a Hugr(Mut).
///
/// Specifically, this trait lets you apply arbitrary modifications that may
Expand Down
10 changes: 9 additions & 1 deletion hugr-core/src/hugr/views/impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::rc::Rc;
use std::sync::Arc;
use std::{borrow::Cow, rc::Rc};

use delegate::delegate;

Expand Down Expand Up @@ -48,6 +48,14 @@ impl<T: HugrView> HugrView for Arc<T> {
hugr_view_methods! {this, this.as_ref()}
}

impl<T: HugrView> HugrView for Box<T> {
hugr_view_methods! {this, this.as_ref()}
}

impl<T: HugrView + ToOwned> HugrView for Cow<'_, T> {
hugr_view_methods! {this, this.as_ref()}
}

impl<H: AsRef<Hugr>, Root> HugrView for RootChecked<H, Root> {
hugr_view_methods! {this, this.as_ref()}
}
Expand Down

0 comments on commit cf31c09

Please sign in to comment.