Rust Petgraph WASM 项目,尝试将结构与 Graph 属性 一起使用,但需要 2 个参数
Rust Petgraph WASM Project, trying to use struct with Graph property, but requires 2 parameters
我是 Rust 的新手,所以这可能是一个非常菜鸟的问题。
我正在尝试使用 rust -> wasm 创建一个网络应用程序。尝试按照教程 https://rustwasm.github.io/docs/book/introduction.html 并尝试使用 crate 包 "petgraph"
我想出了以下
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
}
}
}
但这给我编译错误
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
阅读https://docs.rs/petgraph/0.5.0/petgraph/graph/struct.Graph.html
然后我尝试了
use petgraph::graph::Node;
use petgraph::graph::Edge;
pub struct TreeStructure<N: Node, E: Edge> {
directed_graph: Graph<N, E>
}
但是它说 wasm-bindgen 不支持那个语法?我应该在这里做什么?
鉴于您的 new
函数在 directed_graph
中放置了一个 Graph::<&str, &str>
,这也是您应该在声明中使用的内容:
pub struct TreeStructure {
directed_graph: Graph<&str, &str>,
}
但是,这需要您指定生命周期。我对 wasm 不够熟悉,无法说出哪个最好(另外这取决于你在图表中输入的内容),但你可能需要使用 'static
生命周期:
pub struct TreeStructure {
directed_graph: Graph<&'static str, & 'static str>,
}
这还需要您为方法参数提供 'static
生命周期。或者您将需要移动到拥有的字符串:
pub struct TreeStructure {
directed_graph: Graph<String, String>,
}
再次对您的方法进行适当的更改。
我是 Rust 的新手,所以这可能是一个非常菜鸟的问题。
我正在尝试使用 rust -> wasm 创建一个网络应用程序。尝试按照教程 https://rustwasm.github.io/docs/book/introduction.html 并尝试使用 crate 包 "petgraph" 我想出了以下
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
}
}
}
但这给我编译错误
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
阅读https://docs.rs/petgraph/0.5.0/petgraph/graph/struct.Graph.html 然后我尝试了
use petgraph::graph::Node;
use petgraph::graph::Edge;
pub struct TreeStructure<N: Node, E: Edge> {
directed_graph: Graph<N, E>
}
但是它说 wasm-bindgen 不支持那个语法?我应该在这里做什么?
鉴于您的 new
函数在 directed_graph
中放置了一个 Graph::<&str, &str>
,这也是您应该在声明中使用的内容:
pub struct TreeStructure {
directed_graph: Graph<&str, &str>,
}
但是,这需要您指定生命周期。我对 wasm 不够熟悉,无法说出哪个最好(另外这取决于你在图表中输入的内容),但你可能需要使用 'static
生命周期:
pub struct TreeStructure {
directed_graph: Graph<&'static str, & 'static str>,
}
这还需要您为方法参数提供 'static
生命周期。或者您将需要移动到拥有的字符串:
pub struct TreeStructure {
directed_graph: Graph<String, String>,
}
再次对您的方法进行适当的更改。