Provides core traits [Identity
], [Identifiable
], [Query
] and [Edges
] as an
abstraction to traverse arbitrary data and link it up into a graph. Allows generic
traversals / multiple types of traversals through the same data.
Traits ExtractData
and node annotations allow extraction of metadata into a graph.
Single threaded (still pretty fast though).
Full example: ```rust use meshed::prelude::*; use std::fmt;
struct Id(i32);
impl fmt::Display for Id { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { i32::fmt(&self.0, f) } }
struct Item {
id: Id,
children: Vec
struct ListOfItems {
items: Vec
// Core traits use meshed::identify::{Identity, Identifiable};
// For convenience this is also implemented by default for i32. // Ids must be lightweight, comparable and cheaply cloneable. // if you're using strings, consider using an Rc impl Identity for Id {}
// This marks Item as identifiable by type Id. A type can have many different
// identifiers
impl Identifiable
}
}
impl Edges
// This trait tells the graph system that a type can be used to
// look up nodes when required
impl Query
fn all(&self) -> Vec<&Item> {
self.items.iter().collect()
}
}
use meshed::graph::Graph;
// Identifier, Node Metadata, Edge Metadata
type ItemGraph = Graph let data = ListOfItems {
items: vec![
Item { id: Id(0), children: vec![Id(1), Id(2)] },
Item { id: Id(1), children: vec![Id(0), Id(3)] },
Item { id: Id(2), children: vec![Id(3)] },
Item { id: Id(3), children: vec![Id(1)] }
]
};
let graph = ItemGraph::build_graph(&data);
``` The [ ```rust
use std::borrow::Cow;
use meshed::prelude::*;
struct Item<'a> {
id: i32,
children: Vec struct Meta {
value: String,
} impl<'a> ExtractData for Item<'a> {
fn data(&self) -> Meta {
Meta { value: self.borrowedvalue.tostring() }
}
}
``` The extracted value must be Multiple edge traversals may be provided using unit types as the second parameter to
[ ```rust
use meshed::prelude::*;
struct Item {
id: i32,
children: Vec struct Child;
struct Parent; impl Edges impl Edges BFS and DFS traversals are provided via Traversals produce Sometimes during traversal we want to annotate nodes with temporary data. These annotations are
preserved when truncating or pruning graphs, but may not be present for all nodes (or be consistent between traversals). A good example of this are annotating webpack chunks onto webpack modules. The chunk that a module
is found in is highly dependent on the entrypoint the traversal started from. ```rust ignore
let node = Node::new_default(); struct AnyStruct { value: usize }
enum EnumAnnotation {
Variant
}; node.annotate(AnyStruct { value: 3 });
node.annotate(EnumAnnotation::Variant);
``` This relies on Adding node metadata
ExtractData
] trait provides a way to pull data out of a datasource and
add it to a node. 'static
. This is to prevent the graph grom having
live references from the source data. Source data may be dropped after the graph
has been built. Providing multiple traversals
Edges
].rust ignore
type ItemChildGraph = Graph<Id, (), Child>;
type ItemParentGraph = Graph<Id, (), Parent>;
Traversing the graphs
EdgeTraversal
. An acyclic version is in the
same module as AcyclicTraversal
.TraversedNodes
and can be used to prune, or truncate existing graphs.Annotating nodes
Any
and downcasting internally. Any type can be stored here for as long
as there is only one copy of that type.