From cf31c090d52b70a78cbb58c4f30a2b561b48e4a1 Mon Sep 17 00:00:00 2001 From: Douglas Wilson <141026920+doug-q@users.noreply.github.com> Date: Tue, 3 Dec 2024 10:39:25 +0000 Subject: [PATCH] add HugrView impls for Box and Cow (#1730) --- hugr-core/src/hugr/internal.rs | 28 ++++++++++++++++++++++++++++ hugr-core/src/hugr/views/impls.rs | 10 +++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/hugr-core/src/hugr/internal.rs b/hugr-core/src/hugr/internal.rs index ef9030557..3f1c6b6ff 100644 --- a/hugr-core/src/hugr/internal.rs +++ b/hugr-core/src/hugr/internal.rs @@ -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; @@ -112,6 +113,33 @@ impl HugrInternals for Arc { } } +impl HugrInternals for Box { + 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 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 diff --git a/hugr-core/src/hugr/views/impls.rs b/hugr-core/src/hugr/views/impls.rs index 634654d4f..4b86f18ba 100644 --- a/hugr-core/src/hugr/views/impls.rs +++ b/hugr-core/src/hugr/views/impls.rs @@ -1,5 +1,5 @@ -use std::rc::Rc; use std::sync::Arc; +use std::{borrow::Cow, rc::Rc}; use delegate::delegate; @@ -48,6 +48,14 @@ impl HugrView for Arc { hugr_view_methods! {this, this.as_ref()} } +impl HugrView for Box { + hugr_view_methods! {this, this.as_ref()} +} + +impl HugrView for Cow<'_, T> { + hugr_view_methods! {this, this.as_ref()} +} + impl, Root> HugrView for RootChecked { hugr_view_methods! {this, this.as_ref()} }