is_tree/traits/
has_branches.rs

1//! This module contains the `HasBranches` trait and its associated API traits.
2
3/// This is the trait one should implement to provide a way to iterate over the branches of a type.
4pub trait HasBranches<T> {
5    /// This is the method that should be implemented to provide a way to iterate over the branches of a type.
6    /// It's discouraged to use this method directly. Instead, use the `branches` method from the `HasBranchesAPIV2` trait.
7    fn branches_impl(self) -> impl Iterator<Item = T>;
8}    
9
10pub trait HasBranchesAPI {
11    /// This is used internally. Should use `branches` instead.
12    fn branches_impl2<T>(self) -> impl Iterator<Item = T>
13    where Self: HasBranches<T> + Sized
14    {
15        self.branches_impl()
16    }
17}
18
19impl<T> HasBranchesAPI for T {}
20
21/// This is the trait one should implement to provide a way to iterate over the branches of a type.
22pub trait HasBranchesAPIV2<'a> {
23    /// Iterates over the branches of a type.
24    fn branches<T>(&'a self) -> impl Iterator<Item = T>
25    where &'a Self: HasBranches<T>,
26          T: 'a
27    {
28        self.branches_impl()
29    }
30
31    /// Iterates over the branches of a type mutably.
32    fn branches_mut<T>(&'a mut self) -> impl Iterator<Item = T>
33    where &'a mut Self: HasBranches<T>,
34          T: 'a
35    {
36        self.branches_impl()
37    }
38
39    fn all_branches<T>(&'a self) -> impl Iterator<Item = T>
40    where &'a Self: HasBranches<T>,
41          T: 'a + HasBranches<T> + Copy
42    {
43        self.branches_impl().flat_map(|branch| std::iter::once(branch).chain(branch.branches_impl()))
44    }
45
46    fn all_branches_mut<T>(&'a mut self) -> impl Iterator<Item = T>
47    where &'a mut Self: HasBranches<T>,
48          T: 'a + HasBranches<T> + Copy
49    {
50        self.branches_impl().flat_map(|branch| std::iter::once(branch).chain(branch.branches_impl()))
51    }
52}
53
54impl<'a, T> HasBranchesAPIV2<'a> for T {}
55
56/// This is the trait one should implement to provide a way to add a branch to a type.
57pub trait AddBranch<T> {
58    /// Add a value to the branches of a type.
59    fn add_branch(&mut self, value: T) -> &mut T;
60}