pub trait AbilityTreeNode {
// Required methods
fn node_id(&self) -> usize;
fn children(&self) -> ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE>;
fn display(&self, out: &mut TreeFormatter<'_>) -> Result<()>;
fn node_tag(&self) -> &'static str;
// Provided methods
fn data(&self) -> ArrayVec<u8, MAX_NODE_DATA_SIZE> ⓘ { ... }
fn node_description(&self) -> String { ... }
}Expand description
Trait to reunite all the types of the ability trees to a single “node” type.
The ability tree is using hard types, to preserve strong semantics in the tree. This allow the abilities to be clearly defined, and prohibits invalid trees from even existing. However, we then loose generality of the nodes.
The goal of this trait is to keep these generalities: All nodes of the trees implement the AbilityTreeNode trait, and can be interpreted as a generic node. This gives abstract access to its id, its children, and its optionnal data.
The only kinds of nodes that are allowed as tree nodes are:
- Plain structs, that can have data and / or children:
struct SomeNode {
child_1: SomeChildNode1,
child_2: SomeChildNode2,
data: f32,
}These nodes are the ones that can have multiple children and data, and can be empty to be marker nodes, or end of variants.
- Enums, where each variant is unnamed with a single field:
enum SomeOtherNode {
Variant1(SomeVariant1),
Variant2(SomeVariant2),
}These nodes regroups kinds of nodes together, but do not provide any data, and will always have a single child (although the child kind can vary).
It is possible to create an enum whith no fields. These basically act like a single field with a struct with no fields, but we allow them to avoid bloating the code with unecessary nodes. For example:
enum YetAnotherNode {
EmptyVariant,
VariantWithData(Data),
}Here, this is a shortcut for having a EmptyVariant(EmptyVariantData) where the
data would be an empty struct.
Required Methods§
Sourcefn node_id(&self) -> usize
fn node_id(&self) -> usize
Get the node id.
This identifier is unique to the kind of node it is, allowing to rebuild the node kind from it. This allows to create a mapping for all node kinds to other objects.
Sourcefn children(&self) -> ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE>
fn children(&self) -> ArrayVec<&dyn AbilityTreeNode, MAX_CHILDREN_PER_NODE>
Get all of the nodes children, as abstract ability tree node.
This allows to recursively descend the ability tree over abstract nodes types.
Sourcefn display(&self, out: &mut TreeFormatter<'_>) -> Result<()>
fn display(&self, out: &mut TreeFormatter<'_>) -> Result<()>
Display the ability tree in a human readable manner into the given output.
This is mostly for debug purpuses, and it is not recommanded to use this in production.
Provided Methods§
Sourcefn data(&self) -> ArrayVec<u8, MAX_NODE_DATA_SIZE> ⓘ
fn data(&self) -> ArrayVec<u8, MAX_NODE_DATA_SIZE> ⓘ
Get the node data, if any.
Some nodes may carry arbitrary data that are not children, like numbers or booleans. This function allow to retrieve them, although for now an array of bytes may not be the best pick.
Sourcefn node_description(&self) -> String
fn node_description(&self) -> String
The node name is more advanced version of the tag, and can allocate memory to dynamically changed based on the nodes internals.
It is primarly used for display and debug purpuses.
By default, this is the same as the node tag. Nodes that need to provide a more interesting description can override this.