Skip to content

Commit

Permalink
Try ouroboros instead. Still got a lifetime error - seems wrong?
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Sep 15, 2023
1 parent e61709e commit 95cfb0d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ petgraph = { version="0.6.3", default-features = false}
context-iterators = "0.2.0"
serde_json = "1.0.97"
delegate = "0.10.0"
ouroboros = "0.18.0"

[features]
pyo3 = ["dep:pyo3"]
Expand Down
29 changes: 13 additions & 16 deletions src/hugr/views/sibling.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! SiblingGraph: view onto a sibling subgraph of the HUGR.
use ouroboros::self_referencing;
use lazy_static::lazy_static;
use std::iter;

Expand Down Expand Up @@ -269,28 +270,18 @@ impl<'g, Root: NodeHandle> HugrInternals for SiblingMut<'g, Root> {
}
}

#[self_referencing]
struct SiblingGraphHolder<'a, Root, T> {
// Seems like I need T to be not a type but a type parameterized by a lifetime argument...
sg: SiblingGraph<'a, Root>,
// such that this can be Option<T<'_>> ?
val: Option<T>, // Only None temporarily during construction
}

impl<'a, Root: NodeHandle, T> SiblingGraphHolder<'a, Root, T> {
// Seems like I need T to be not a type but a type parameterized by a lifetime argument
// such that val_fn can be a function<'b>(&'b SiblingGraph<...>) -> T<'b>
fn from(sg: SiblingGraph<'a, Root>, val_fn: impl FnOnce(&SiblingGraph<'a, Root>) -> T) -> Self {
let mut res = Self {sg, val: None};
res.val = Some(val_fn(&res.sg));
res
}
#[borrows(sg)]
val: T
}

impl<'a, Root: NodeHandle, T: Iterator> Iterator for SiblingGraphHolder<'a, Root, T> {
type Item = T::Item;

fn next(&mut self) -> Option<Self::Item> {
self.val.as_mut().unwrap().next()
self.with_val_mut(|val|val.next())
}

}
Expand All @@ -302,7 +293,7 @@ lazy_static! {
impl<'g, Root: NodeHandle> HugrView for SiblingMut<'g, Root> {
type RootHandle = Root;

type Nodes<'a> = SiblingGraphHolder<'g, Root, <SiblingGraph<'g, Root> as HugrView>::Nodes<'a>>
type Nodes<'a> = SiblingGraphHolder<'a, Root, <SiblingGraph<'a, Root> as HugrView>::Nodes<'a>>
where
Self: 'a;

Expand Down Expand Up @@ -343,7 +334,13 @@ impl<'g, Root: NodeHandle> HugrView for SiblingMut<'g, Root> {
}

fn nodes(&self) -> Self::Nodes<'_> {
SiblingGraphHolder::from(SiblingGraph::<'_, Root>::new_unchecked(self.hugr, self.root), |sg|sg.nodes())
Self::Nodes::<'_>::new(
SiblingGraph::new_unchecked(self.hugr, self.root),
|sg: &'_ SiblingGraph<'_, Root>| {
let n: <SiblingGraph<'_, Root> as HugrView>::Nodes<'_> = sg.nodes();
n
}
)
}

fn node_ports(&self, node: Node, dir: Direction) -> Self::NodePorts<'_> {
Expand Down

0 comments on commit 95cfb0d

Please sign in to comment.