Struct conciliator::print::Tree

source ·
pub struct Tree<'s, I: ExactSizeIterator<Item = T>, T, F> { /* private fields */ }
Expand description

Tree structure of things to print, recursively traversed

Instead of constraining the user to any particular data structure, this object is built around a traversal function. This function is used to traverse the tree depth-first, starting from an iterator of “root” nodes.

Requires ExactSizeIterators so that it can print the correct symbols (├─ vs └─).

For example:

struct Animal (&'static str, Vec<Animal>);
let tree = [
    Animal ("Reptiles", vec![
        Animal ("Frogs", vec![]),
        Animal ("Lizards", vec![
            Animal ("Chameleon", vec![]),
            Animal ("Gecko", vec![]),
        ])
    ]),
    Animal ("Mammals", vec![
        Animal ("Tiger", vec![]),
        Animal ("Monkey", vec![])
    ]),
    Animal ("Crab", vec![])
];
impl Inline for Animal {
    fn inline(&self, buffer: &mut Buffer) {
        buffer.push_plain(self.0);
    }
}
con.print(Tree::new(
    "Animals:",
    tree.iter(),
    |a| Some(a.1.iter())
));

Produces something like:

[ ┬ ] Animals:
  ├─Reptiles
  │ ├─Frogs
  │ └─Lizards
  │   ├─Chameleon
  │   └─Gecko
  ├─Mammals
  │ ├─Tiger
  │ └─Monkey
  └─Crab

Unfortunately, unlike the List, the Tree does not currently support a formatter - the items need to be Inline.

Implementations§

source§

impl<'s, I, T, F> Tree<'s, I, T, F>
where I: ExactSizeIterator<Item = T>, F: FnMut(T) -> Option<I>, T: Inline,

source

pub fn new(description: &'s str, root_nodes: I, traverser: F) -> Self

Create a new Tree

The traverser function is called with each item (node) in the tree and returns an iterator of its children: FnMut(T) -> Option<ExactSizeIterator<Item = T>>.

The Option<…> return type was chosen to simplify printing trees with distinct Branch / Leaf types, for which the traverser function, if they are merged into an enum Node, might otherwise have difficulty creating an empty ExactSizeIterator in the leaf case.

Trait Implementations§

source§

impl<'s, I, T, F> Print for Tree<'s, I, T, F>
where I: ExactSizeIterator<Item = T>, F: FnMut(T) -> Option<I>, T: Inline,

source§

fn print<C: Conciliator + ?Sized>(self, con: &C)

Print self using the Conciliator, consuming self

Auto Trait Implementations§

§

impl<'s, I, T, F> RefUnwindSafe for Tree<'s, I, T, F>

§

impl<'s, I, T, F> Send for Tree<'s, I, T, F>
where F: Send, I: Send,

§

impl<'s, I, T, F> Sync for Tree<'s, I, T, F>
where F: Sync, I: Sync,

§

impl<'s, I, T, F> Unpin for Tree<'s, I, T, F>
where F: Unpin, I: Unpin,

§

impl<'s, I, T, F> UnwindSafe for Tree<'s, I, T, F>
where F: UnwindSafe, I: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.