From 9aa69d3b520bbab70d45bdb585782467dab95398 Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Fri, 29 Nov 2024 13:58:53 +0000 Subject: [PATCH] add HugrView impls for Box and Cow --- 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()} }