3

I want to implement a graph structure in Rust. For this goal, I wrote simple abstractions:

pub struct Graph<'a> {
    pub nodes: Vec<Node>,
    pub edges: Vec<Edge<'a>>,
}

#[derive(Debug)]
pub struct Node {
    pub id: String,
    pub label: String,
}

pub struct Edge<'a> {
    pub source: &'a Node,
    pub target: &'a Node,
}

Graph contains vectors of Nodes and Edges. Every Edge has a ref to a Node in the same Graph.

I don't know it's a possible write something like this.

I tried to write a static method that builds a new Graph instance from a JSON representation:

impl<'a> Graph<'a> {
    pub fn from_json(json: &String) -> Graph {
        if let json::JsonValue::Object(deserialized) = json::parse(json.as_ref()).unwrap() {
            let nodes: Vec<Node> = deserialized
                .get("nodes")
                .unwrap()
                .members()
                .map(|v| {
                    if let json::JsonValue::Object(ref val) = *v {
                        return Node {
                            id: val.get("id").unwrap().to_string(),
                            label: val.get("label").unwrap().to_string(),
                        };
                    }
                    panic!("Invalid structure of json graph body.")
                })
                .collect::<Vec<Node>>();
            let edges: Vec<Edge> = deserialized
                .get("edges")
                .unwrap()
                .members()
                .map(|v| {
                    if let json::JsonValue::Object(ref val) = *v {
                        let source = (*nodes)
                            .iter()
                            .find(|&v| v.id == val.get("source").unwrap().to_string())
                            .unwrap();
                        let target = (*nodes)
                            .iter()
                            .find(|&v| v.id == val.get("target").unwrap().to_string())
                            .unwrap();
                        return Edge { source, target };
                    }
                    panic!("Invalid structure of json graph body.")
                })
                .collect::<Vec<Edge>>();
            return Graph { nodes, edges };
        }
        panic!("Incorrect struct of json contains!");
    }
}

When I compile, I get this error:

error[E0373]: closure may outlive the current function, but it borrows `nodes`, which is owned by the current function
  --> src/graph.rs:30:22
   |
30 |                 .map(|v| {
   |                      ^^^ may outlive borrowed value `nodes`
31 |                     if let json::JsonValue::Object(ref val) = *v {
32 |                         let source = (*nodes).iter().find(|&v| v.id ==  val.get("source").unwrap().to_string()).unwrap();
   |                                        ----- `nodes` is borrowed here
   |
help: to force the closure to take ownership of `nodes` (and any other referenced variables), use the `move` keyword
   |
30 |                 .map(move |v| {
   |                      ^^^^^^^^

error: aborting due to previous error

A possible solution to this problem is to add move before the closure parameters, but I need the nodes vector to build the Graph instance.

What am I doing wrong?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366

1 Answers1

1

After some research, I found this article's: Rust doc. Smart pointers, Users Rust Lang, and I understood my mistakes. The first one: I remove lifetime parameters from structs definitions.

use std::rc::Rc;
#[derive(Debug)]
pub struct Graph {
    pub nodes: Vec<Rc<Node>>,
    pub edges: Vec<Edge>
}
#[derive(Debug)]
pub struct Node {
    pub id: String,
    pub label: String
}
#[derive(Debug)]
pub struct Edge {
    pub source: Rc<Node>,
    pub target: Rc<Node>
}

Second thing: I rewrote the code of from_json function for using Rc<T> instead of raw references.

impl Graph {
    pub fn from_json(json: & String) -> Graph {
        if let json::JsonValue::Object(deserialized) = json::parse(json.as_ref()).unwrap() {
            let nodes : Vec<Rc<Node>> = deserialized.get("nodes").unwrap().members()
                .map(|v| {
                    if let json::JsonValue::Object(ref val) = *v {
                        return Rc::new(Node {
                            id: val.get("id").unwrap().to_string(),
                            label: val.get("label").unwrap().to_string()
                        });
                    }
                    panic!("Invalid structure of json graph body.")
            }).collect::<Vec<Rc<Node>>>();
            let edges : Vec<Edge> = deserialized.get("edges").unwrap().members()
                .map(|v| {
                    if let json::JsonValue::Object(ref val) = *v {
                        let source = nodes.iter().find(|&v| v.id ==  val.get("source").unwrap().to_string()).unwrap();
                        let target = nodes.iter().find(|&v| v.id ==  val.get("target").unwrap().to_string()).unwrap();
                        return Edge {
                            source: Rc::clone(&source),
                            target: Rc::clone(&target)
                        };
                    }
                    panic!("Invalid structure of json graph body.")
                }).collect::<Vec<Edge>>();
            return Graph {
                nodes,
                edges
            }
        }
        panic!("Incorrect struct of json contains!");
    }
}

Now it works. Thanks for sharing useful links. I found a lot of helpful information about building graph structs in Rust such as: Graph structure in Rust

  • This is interesting: why the element type of Vec could change the borrow behavior of the Vec itself? – zsf222 Mar 03 '21 at 05:25