I am very new to Rust, so this might be a very rookie question.
I am trying to create a web app with rust -> wasm. Tried to follow the tutorial https://rustwasm.github.io/docs/book/introduction.html and tried to use the crate package "petgraph" I have come up with the following
use petgraph::graph::Graph;
#[wasm_bindgen]
pub struct TreeStructure {
directed_graph: Graph
}
#[wasm_bindgen]
impl TreeStructure {
pub fn new() -> TreeStructure {
TreeStructure {
directed_graph: Graph::<&str, &str>::new()
}
}
pub fn addNode(&mut self, newNode: &str) {
let findIndex = self.findNode(newNode);
if findIndex < 0 {
self.directed_graph.add_node(newNode);
} else {
alert("cant add, this {} already exist in the nodes", newNode);
}
}
pub fn removeNode(&mut self, toRemoveNode: &str) {
let findIndex = self.findNode(toRemoveNode);
if findIndex > -1 {
self.directed_graph.remove_node(toRemoveNode);
} else {
alert("cant remove, cannot find {} in the nodes", toRemoveNode);
}
}
pub fn addEdge(&mut self, fromNode: &str, toNode: &str) {
let fromIndex = self.findNode(fromNode);
let toIndex = self.findNode(toNode);
if fromIndex > -1 && toIndex > -1 {
let alreadyEdged = self.directed_graph.contains_edge(fromIndex, toIndex);
if !alreadyEdged {
self.directed_graph.add_edge(fromIndex, toIndex, "");
} else {
alert("edge already exist from {} to {}", fromNode, toNode);
}
}
}
pub fn getNeighbors(&self, checkNode: &str) -> Array {
let checkIndex = self.findNode(checkNode);
if checkIndex < 0 {
return vec![].into_inter().collect();
}
self.directed_graph.neighbors_directed(checkIndex).collect();
}
}
impl TreeStructure {
pub fn findNode(&self, nodeToFind: &str) -> i32 {
let findIndex = self.directed_graph.node_indices().collect::<Vec<_>>().iter().position(|&r| r == nodeToFind);
match findIndex {
Some(x) => x,
None => -1
}
}
}
but this is giving me compile error
error[E0107]: wrong number of type arguments: expected at least 2, found 0
--> src/lib.rs:25:25
|
25 | directed_graph: Graph
| ^^^^^ expected at least 2 type arguments
Reading https://docs.rs/petgraph/0.5.0/petgraph/graph/struct.Graph.html I then tried
use petgraph::graph::Node;
use petgraph::graph::Edge;
pub struct TreeStructure<N: Node, E: Edge> {
directed_graph: Graph<N, E>
}
But then it says wasm-bindgen does not support that syntax? What should I do here?
Given that your new
function puts a Graph::<&str, &str>
in directed_graph
, that is what you should also use in the declaration:
pub struct TreeStructure {
directed_graph: Graph<&str, &str>,
}
However, this requires you to specify a lifetime. I'm not familiar enough with wasm to say which will be best (plus it depends on what exactly you put in the graph), but you will probably need to either use a 'static
lifetime:
pub struct TreeStructure {
directed_graph: Graph<&'static str, & 'static str>,
}
which will also require you to give the 'static
lifetime to your method parameters. Or you will need to move to owned strings:
pub struct TreeStructure {
directed_graph: Graph<String, String>,
}
again with the appropriate changes to your methods.