jdomantas / internal-iterator Goto Github PK
View Code? Open in Web Editor NEWLicense: Apache License 2.0
License: Apache License 2.0
It would be helpful to have an InternalIterator
version of std::iter::from_fn
I need to iterate over all items in a composite tree and wanted to use this crate for that.
However the example in the readme only shows where the iterated item is an owned type. I would like to have an iterator over &T
and &mut T
types.
The immutable case is simply enough but for the mutable iterator i am struggeling with the borrow checker.
This was my attempt:
use std::ops::ControlFlow;
use internal_iterator::InternalIterator;
struct Node {
nodes: Vec<Node>,
}
struct NodeIteratorMut<'a> {
n: &'a mut Node,
}
impl<'a> InternalIterator for NodeIteratorMut<'a> {
type Item = &'a mut Node;
fn try_for_each<R, F>(self, mut f: F) -> std::ops::ControlFlow<R>
where
F: FnMut(Self::Item) -> std::ops::ControlFlow<R>,
{
iter_node(self.n, &mut f)
}
}
fn iter_node<'a, T>(
node: &'a mut Node,
f: &mut impl FnMut(&'a mut Node) -> ControlFlow<T>,
) -> ControlFlow<T> {
f(node)?;
node.nodes.iter_mut().try_for_each(|n| iter_node(n, f))
}
fn main() {}
Which does not work :
error[E0499]: cannot borrow `node.nodes` as mutable more than once at a time
--> crates\tree_view\examples\iter_test.rs:26:5
|
21 | fn iter_node<'a, T>(
| -- lifetime `'a` defined here
...
25 | f(node)?;
| -------
| | |
| | first mutable borrow occurs here
| argument requires that `*node` is borrowed for `'a`
26 | node.nodes.iter_mut().try_for_each(|n| iter_node(n, f))
| ^^^^^^^^^^^^^^^^^^^^^ second mutable borrow occurs here
How could this be implemented?
Hello there!
I saw your post of reddit today, congratulations on your release ๐ฅณ ๐. I was wondering if you were interested in trying to integrate this with other crates in the community. The slab-tree crate seems like a perfect use-case for a crate like this. While I don't own it, I do see the obvious potential between these two crates to create easily std-like iterable trees. Do you think a tree like API like slab-tree would be a good candidate for your crate to integrate with?
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.