-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: impl HugrView for any &(mut) to a HugrView (#1678)
closes #1636 . Ideally we'd like a blanket implementation over all things that Deref to anything that's a HugrView, but AFAICS this isn't possible in Rust ATM, although might become so in the future given constrained HRTBs (higher-ranked type bounds). So, implement manually for `&T`, `&mut T`, `Rc<T>`, `Arc<T>`, `Box<T>`, `Cow<...,T>` and also `RootChecked`, using a macro in views/impls.rs. BREAKING CHANGE: types which implement AsRef<Hugr> - both library ones such as Rc<Hugr> and custom ones - no longer get a blanket impl of HugrView. Workaround by manually calling `as_ref()` and using the `&Hugr` yourself. --------- Co-authored-by: Douglas Wilson <141026920+doug-q@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
234 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use std::{borrow::Cow, rc::Rc, sync::Arc}; | ||
|
||
use delegate::delegate; | ||
|
||
use super::{HugrView, RootChecked}; | ||
use crate::{Direction, Hugr, Node, Port}; | ||
|
||
macro_rules! hugr_view_methods { | ||
// The extra ident here is because invocations of the macro cannot pass `self` as argument | ||
($arg:ident, $e:expr) => { | ||
delegate! { | ||
to ({let $arg=self; $e}) { | ||
fn contains_node(&self, node: Node) -> bool; | ||
fn node_count(&self) -> usize; | ||
fn edge_count(&self) -> usize; | ||
fn nodes(&self) -> impl Iterator<Item = Node> + Clone; | ||
fn node_ports(&self, node: Node, dir: Direction) -> impl Iterator<Item = Port> + Clone; | ||
fn all_node_ports(&self, node: Node) -> impl Iterator<Item = Port> + Clone; | ||
fn linked_ports( | ||
&self, | ||
node: Node, | ||
port: impl Into<Port>, | ||
) -> impl Iterator<Item = (Node, Port)> + Clone; | ||
fn node_connections(&self, node: Node, other: Node) -> impl Iterator<Item = [Port; 2]> + Clone; | ||
fn num_ports(&self, node: Node, dir: Direction) -> usize; | ||
fn children(&self, node: Node) -> impl DoubleEndedIterator<Item = Node> + Clone; | ||
fn neighbours(&self, node: Node, dir: Direction) -> impl Iterator<Item = Node> + Clone; | ||
fn all_neighbours(&self, node: Node) -> impl Iterator<Item = Node> + Clone; | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<T: HugrView> HugrView for &T { | ||
hugr_view_methods! {this, *this} | ||
} | ||
|
||
impl<T: HugrView> HugrView for &mut T { | ||
hugr_view_methods! {this, &**this} | ||
} | ||
|
||
impl<T: HugrView> HugrView for Rc<T> { | ||
hugr_view_methods! {this, this.as_ref()} | ||
} | ||
|
||
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()} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use std::{rc::Rc, sync::Arc}; | ||
|
||
use crate::hugr::views::{DescendantsGraph, HierarchyView}; | ||
use crate::{Hugr, HugrView, Node}; | ||
|
||
struct ViewWrapper<H>(H); | ||
impl<H: HugrView> ViewWrapper<H> { | ||
fn nodes(&self) -> impl Iterator<Item = Node> + '_ { | ||
self.0.nodes() | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_refs_to_view() { | ||
let h = Hugr::default(); | ||
let v = ViewWrapper(&h); | ||
let c = h.nodes().count(); | ||
assert_eq!(v.nodes().count(), c); | ||
let v2 = ViewWrapper(DescendantsGraph::<Node>::try_new(&h, h.root()).unwrap()); | ||
// v2 owns the DescendantsGraph, but that only borrows `h`, so we still have both | ||
assert_eq!(v2.nodes().count(), v.nodes().count()); | ||
// And we can borrow the DescendantsGraph, even just a reference to that counts as a HugrView | ||
assert_eq!(ViewWrapper(&v2.0).nodes().count(), v.nodes().count()); | ||
|
||
let vh = ViewWrapper(h); | ||
assert_eq!(vh.nodes().count(), c); | ||
let h: Hugr = vh.0; | ||
assert_eq!(h.nodes().count(), c); | ||
|
||
let vb = ViewWrapper(Box::new(&h)); | ||
assert_eq!(vb.nodes().count(), c); | ||
let va = ViewWrapper(Arc::new(h)); | ||
assert_eq!(va.nodes().count(), c); | ||
let h = Arc::try_unwrap(va.0).unwrap(); | ||
let vr = Rc::new(&h); | ||
assert_eq!(ViewWrapper(&vr).nodes().count(), h.nodes().count()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters