Git Product home page Git Product logo

internal-iterator's People

Contributors

alfredan avatar cuviper avatar erjanmx avatar jdomantas avatar karelpeeters avatar kmeakin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

internal-iterator's Issues

Question: How to implement a mutable iterator?

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?

Integration with other crates

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.