Skip to content

Commit a54cf12

Browse files
committed
Began to improve code to be in line with Rust API Guidelines C-COMMON-TRAITS
1 parent cefceb3 commit a54cf12

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

src/algorithms.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ pub fn bellman_ford<T: Display + Eq + Clone + Hash>(graph: &mut Graph<T>, source
238238
}
239239

240240
/// Errors that can occur when algorithms are run.
241-
#[derive(Debug, PartialEq)]
241+
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Copy, Hash)]
242242
pub enum RunAlgorithmError {
243243
/// Indicates that the source node is not contained within the graph.
244244
SourceNodeMissing,

src/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl<T: Display + Clone + Eq + Hash> Default for Graph<T> {
380380
}
381381
}
382382

383-
impl<T: Display> Display for Graph<T> {
383+
impl<T: Display + Eq + Hash> Display for Graph<T> {
384384
/// Formats the graph to show all edges between nodes
385385
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
386386
let mut graph = String::new();

src/instruction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use std::{fmt::Display, hash::Hash};
4040
use crate::Graph;
4141

4242
#[cfg(feature = "from_instruction")]
43-
#[derive(Debug, PartialEq)]
43+
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
4444
enum Instruction<T: Display + Clone> {
4545
AddNode(T),
4646
AddEdge(i32, T, T),
@@ -49,7 +49,7 @@ enum Instruction<T: Display + Clone> {
4949

5050
/// A list of instructions used to construct a graph.
5151
#[cfg(feature = "from_instruction")]
52-
#[derive(Debug, PartialEq)]
52+
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
5353
pub struct Instructions<T: Display + Clone> {
5454
instructions: Vec<Instruction<T>>,
5555
}

src/lib.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ pub mod algorithms;
5757
pub mod instruction;
5858

5959
/// A node inside the graph
60-
#[derive(Debug, Clone, Eq)]
61-
struct Node<T: Display> {
60+
#[derive(Clone, Eq, Debug)]
61+
struct Node<T: Display + Eq> {
6262
/// Identifier of this node
6363
id: T,
6464
/// Edges of this node
@@ -70,8 +70,8 @@ struct Node<T: Display> {
7070
}
7171

7272
// An edge between two nodes inside a graph
73-
#[derive(Debug, Clone, Eq)]
74-
struct Edge<T: Display> {
73+
#[derive(Clone, Eq, Ord, PartialOrd, Debug)]
74+
struct Edge<T: Display + Eq> {
7575
/// The "cost" of moving along this edge
7676
weight: i32,
7777
/// The parent of this edge
@@ -81,16 +81,30 @@ struct Edge<T: Display> {
8181
}
8282

8383
/// Graph data structure to organize nodes that are connected to each other with edges.
84-
#[derive(Debug)]
85-
pub struct Graph<T: Display> {
84+
#[derive(Clone, Debug, PartialEq, Eq)]
85+
pub struct Graph<T: Display + Eq + Hash> {
8686
/// All nodes contained in this graph
8787
//nodes: Vec<Rc<RefCell<Node<T>>>>,
8888

8989
/// Stores all nodes contained in this graph mapped to the node's id.
9090
nodes: HashMap<T, Rc<RefCell<Node<T>>>>,
9191
}
9292

93-
impl<T: Display> Graph<T> {
93+
impl<T: Display + Eq + Hash + Clone> PartialOrd for Graph<T> {
94+
/// Compares the size of this graph with the size of another graph.
95+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
96+
self.size().partial_cmp(&other.size())
97+
}
98+
}
99+
100+
impl<T: Display + Eq + Hash + Clone> Ord for Graph<T> {
101+
/// Compares the size of this graph with the size of another graph.
102+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
103+
self.size().cmp(&other.size())
104+
}
105+
}
106+
107+
impl<T: Display + Eq + Hash> Graph<T> {
94108

95109
/// Resets the distance of each node in the graph back to `i32::MAX` and resets the shortest path string.
96110
///
@@ -335,7 +349,7 @@ impl<T: Display + Clone + Debug> Display for ShortestPath<T> {
335349
}
336350

337351
#[doc(hidden)]
338-
impl<T: Display + Clone> TryFrom<&Rc<RefCell<Node<T>>>> for ShortestPath<T> {
352+
impl<T: Display + Clone + Eq> TryFrom<&Rc<RefCell<Node<T>>>> for ShortestPath<T> {
339353
type Error = &'static str;
340354

341355
/// Tries to read the shortest path from the node.

0 commit comments

Comments
 (0)