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

add HugrView impls for Box and Cow #1730

Merged
merged 1 commit into from
Dec 3, 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
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
Loading