Skip to main content

Tree

Trait Tree 

Source
pub trait Tree<B: Backend>: Clone {
    type On<C: Backend>: Tree<C>;

    // Required methods
    fn flatten(&self, leaves: &mut Vec<B::Leaf>);
    fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self;
    fn rebuild<C: Backend>(
        &self,
        leaves: &mut dyn Iterator<Item = C::Leaf>,
    ) -> Self::On<C>;
    fn def(&self) -> TreeDef;
    fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self;
}
Expand description

A pytree: a structure of scalars and arrays on backend B.

Implement it with #[derive(Tree)] for structs whose type parameter is the backend.

Required Associated Types§

Source

type On<C: Backend>: Tree<C>

The same structure on backend C.

Required Methods§

Source

fn flatten(&self, leaves: &mut Vec<B::Leaf>)

Appends the leaves, in order.

Source

fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self

Builds a value of the same structure from leaves.

Source

fn rebuild<C: Backend>( &self, leaves: &mut dyn Iterator<Item = C::Leaf>, ) -> Self::On<C>

Builds a value of the same structure on backend C from leaves (static fields are copied from self).

Source

fn def(&self) -> TreeDef

Describes the structure.

Source

fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self

Selects on_true or on_false leaf by leaf.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<B: Backend, A: Tree<B>, B1: Tree<B>, C1: Tree<B>, D: Tree<B>, E: Tree<B>, F: Tree<B>, G: Tree<B>, H: Tree<B>> Tree<B> for (A, B1, C1, D, E, F, G, H)

Source§

type On<C: Backend> = (<A as Tree<B>>::On<C>, <B1 as Tree<B>>::On<C>, <C1 as Tree<B>>::On<C>, <D as Tree<B>>::On<C>, <E as Tree<B>>::On<C>, <F as Tree<B>>::On<C>, <G as Tree<B>>::On<C>, <H as Tree<B>>::On<C>)

Source§

fn flatten(&self, leaves: &mut Vec<B::Leaf>)

Source§

fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self

Source§

fn rebuild<C: Backend>( &self, leaves: &mut dyn Iterator<Item = C::Leaf>, ) -> Self::On<C>

Source§

fn def(&self) -> TreeDef

Source§

fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self

Source§

impl<B: Backend, A: Tree<B>, B1: Tree<B>, C1: Tree<B>, D: Tree<B>, E: Tree<B>, F: Tree<B>, G: Tree<B>> Tree<B> for (A, B1, C1, D, E, F, G)

Source§

type On<C: Backend> = (<A as Tree<B>>::On<C>, <B1 as Tree<B>>::On<C>, <C1 as Tree<B>>::On<C>, <D as Tree<B>>::On<C>, <E as Tree<B>>::On<C>, <F as Tree<B>>::On<C>, <G as Tree<B>>::On<C>)

Source§

fn flatten(&self, leaves: &mut Vec<B::Leaf>)

Source§

fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self

Source§

fn rebuild<C: Backend>( &self, leaves: &mut dyn Iterator<Item = C::Leaf>, ) -> Self::On<C>

Source§

fn def(&self) -> TreeDef

Source§

fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self

Source§

impl<B: Backend, A: Tree<B>, B1: Tree<B>, C1: Tree<B>, D: Tree<B>, E: Tree<B>, F: Tree<B>> Tree<B> for (A, B1, C1, D, E, F)

Source§

type On<C: Backend> = (<A as Tree<B>>::On<C>, <B1 as Tree<B>>::On<C>, <C1 as Tree<B>>::On<C>, <D as Tree<B>>::On<C>, <E as Tree<B>>::On<C>, <F as Tree<B>>::On<C>)

Source§

fn flatten(&self, leaves: &mut Vec<B::Leaf>)

Source§

fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self

Source§

fn rebuild<C: Backend>( &self, leaves: &mut dyn Iterator<Item = C::Leaf>, ) -> Self::On<C>

Source§

fn def(&self) -> TreeDef

Source§

fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self

Source§

impl<B: Backend, A: Tree<B>, B1: Tree<B>, C1: Tree<B>, D: Tree<B>, E: Tree<B>> Tree<B> for (A, B1, C1, D, E)

Source§

type On<C: Backend> = (<A as Tree<B>>::On<C>, <B1 as Tree<B>>::On<C>, <C1 as Tree<B>>::On<C>, <D as Tree<B>>::On<C>, <E as Tree<B>>::On<C>)

Source§

fn flatten(&self, leaves: &mut Vec<B::Leaf>)

Source§

fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self

Source§

fn rebuild<C: Backend>( &self, leaves: &mut dyn Iterator<Item = C::Leaf>, ) -> Self::On<C>

Source§

fn def(&self) -> TreeDef

Source§

fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self

Source§

impl<B: Backend, A: Tree<B>, B1: Tree<B>, C1: Tree<B>, D: Tree<B>> Tree<B> for (A, B1, C1, D)

Source§

type On<C: Backend> = (<A as Tree<B>>::On<C>, <B1 as Tree<B>>::On<C>, <C1 as Tree<B>>::On<C>, <D as Tree<B>>::On<C>)

Source§

fn flatten(&self, leaves: &mut Vec<B::Leaf>)

Source§

fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self

Source§

fn rebuild<C: Backend>( &self, leaves: &mut dyn Iterator<Item = C::Leaf>, ) -> Self::On<C>

Source§

fn def(&self) -> TreeDef

Source§

fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self

Source§

impl<B: Backend, A: Tree<B>, B1: Tree<B>, C1: Tree<B>> Tree<B> for (A, B1, C1)

Source§

type On<C: Backend> = (<A as Tree<B>>::On<C>, <B1 as Tree<B>>::On<C>, <C1 as Tree<B>>::On<C>)

Source§

fn flatten(&self, leaves: &mut Vec<B::Leaf>)

Source§

fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self

Source§

fn rebuild<C: Backend>( &self, leaves: &mut dyn Iterator<Item = C::Leaf>, ) -> Self::On<C>

Source§

fn def(&self) -> TreeDef

Source§

fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self

Source§

impl<B: Backend, A: Tree<B>, B1: Tree<B>> Tree<B> for (A, B1)

Source§

type On<C: Backend> = (<A as Tree<B>>::On<C>, <B1 as Tree<B>>::On<C>)

Source§

fn flatten(&self, leaves: &mut Vec<B::Leaf>)

Source§

fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self

Source§

fn rebuild<C: Backend>( &self, leaves: &mut dyn Iterator<Item = C::Leaf>, ) -> Self::On<C>

Source§

fn def(&self) -> TreeDef

Source§

fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self

Source§

impl<B: Backend, A: Tree<B>> Tree<B> for (A,)

Source§

type On<C: Backend> = (<A as Tree<B>>::On<C>,)

Source§

fn flatten(&self, leaves: &mut Vec<B::Leaf>)

Source§

fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self

Source§

fn rebuild<C: Backend>( &self, leaves: &mut dyn Iterator<Item = C::Leaf>, ) -> Self::On<C>

Source§

fn def(&self) -> TreeDef

Source§

fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self

Source§

impl<B: Backend, T: Tree<B>, const N: usize> Tree<B> for [T; N]

Source§

type On<C: Backend> = [<T as Tree<B>>::On<C>; N]

Source§

fn flatten(&self, leaves: &mut Vec<B::Leaf>)

Source§

fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self

Source§

fn rebuild<C: Backend>( &self, leaves: &mut dyn Iterator<Item = C::Leaf>, ) -> Self::On<C>

Source§

fn def(&self) -> TreeDef

Source§

fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self

Source§

impl<B: Backend, T: Tree<B>> Tree<B> for Option<T>

None has no leaves and Some(x) has the leaves of x, as None and values are in JAX’s pytrees.

Source§

type On<C: Backend> = Option<<T as Tree<B>>::On<C>>

Source§

fn flatten(&self, leaves: &mut Vec<B::Leaf>)

Source§

fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self

Source§

fn rebuild<C: Backend>( &self, leaves: &mut dyn Iterator<Item = C::Leaf>, ) -> Self::On<C>

Source§

fn def(&self) -> TreeDef

Source§

fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self

Source§

impl<B: Backend, T: Tree<B>> Tree<B> for Vec<T>

Source§

type On<C: Backend> = Vec<<T as Tree<B>>::On<C>>

Source§

fn flatten(&self, leaves: &mut Vec<B::Leaf>)

Source§

fn unflatten_like(&self, leaves: &mut dyn Iterator<Item = B::Leaf>) -> Self

Source§

fn rebuild<C: Backend>( &self, leaves: &mut dyn Iterator<Item = C::Leaf>, ) -> Self::On<C>

Source§

fn def(&self) -> TreeDef

Source§

fn select(pred: Bool<B>, on_true: &Self, on_false: &Self) -> Self

Source§

impl<B: Backend> Tree<B> for ()

Source§

type On<C: Backend> = ()

Source§

fn flatten(&self, _: &mut Vec<B::Leaf>)

Source§

fn unflatten_like(&self, _: &mut dyn Iterator<Item = B::Leaf>) -> Self

Source§

fn rebuild<C: Backend>(&self, _: &mut dyn Iterator<Item = C::Leaf>)

Source§

fn def(&self) -> TreeDef

Source§

fn select(_: Bool<B>, _: &Self, _: &Self) -> Self

Implementors§

Source§

impl<B: Backend, T: Element> Tree<B> for Array<B, T>

Source§

type On<C: Backend> = Array<C, T>

Source§

impl<B: Backend, T: Element> Tree<B> for Scalar<B, T>

Source§

type On<C: Backend> = Scalar<C, T>

Source§

impl<B: Backend> Tree<B> for Key<B>

A key is a pytree with one leaf, its data (u32[..., 2]).

Source§

type On<C: Backend> = Key<C>