WIP: working on test graph traits and stuck on lifetimes

This commit is contained in:
sigil-03 2025-05-20 23:18:38 -06:00
commit 81bee2f769
8 changed files with 114 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "graph-rs"
version = "0.1.0"

6
Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "graph-rs"
version = "0.1.0"
edition = "2024"
[dependencies]

1
src/edge.rs Normal file
View file

@ -0,0 +1 @@
pub trait Edge {}

16
src/graph.rs Normal file
View file

@ -0,0 +1,16 @@
use crate::edge::Edge;
use crate::vertex::Vertex;
/// operations you might want to do on a graph or on nodes in a graph
pub trait Graph<V: Vertex, E: Edge> {
// Connect two nodes, and return the edge
// TODO: figure out a way to modify self? is that even needed?
// same for `disconnect()`
fn connect(vertex_1: &V, vertex_2: &V) -> E;
// disconnect two nodes, and return the old edge
fn disconnect(vertex_1: &V, vertex_2: &V) -> E;
// get the degree of a given vertex - possibly put this in a GraphUtil trait?
fn degree(vertex: V) -> usize;
}

6
src/lib.rs Normal file
View file

@ -0,0 +1,6 @@
mod edge;
mod graph;
mod vertex;
#[cfg(test)]
mod test;

76
src/test.rs Normal file
View file

@ -0,0 +1,76 @@
use std::marker::PhantomData;
use crate::{edge::Edge, graph::Graph, vertex::Vertex};
struct TestEdge<'a> {
v1: &'a TestVertex<'a>,
v2: &'a TestVertex<'a>,
}
impl<'a> Edge for TestEdge<'a> {}
struct TestVertex<'a> {
_phantom: PhantomData<&'a Self>,
}
impl<'a> Vertex for TestVertex<'a> {}
struct TestGraph<'a> {
vertices: Vec<TestVertex<'a>>,
edges: Vec<TestEdge<'a>>,
}
impl<'a> TestGraph<'a> {
pub fn get_vertex(&self, index: usize) -> &TestVertex {
assert!(index < self.vertices.len());
&self.vertices[index]
}
}
impl<'a> Graph<TestVertex<'a>, TestEdge<'a>> for TestGraph<'a> {
fn connect(vertex_1: &TestVertex<'a>, vertex_2: &TestVertex<'a>) -> TestEdge<'a> {
TestEdge {
v1: vertex_1,
v2: vertex_2,
}
}
fn disconnect(vertex_1: &TestVertex, vertex_2: &TestVertex) -> TestEdge<'a> {
todo!();
}
fn degree(vertex: TestVertex) -> usize {
todo!();
}
}
#[test]
fn it_works() {}
#[test]
fn test_connect() {
let v1 = TestVertex {};
let v2 = TestVertex {};
let mut g = TestGraph {
vertices: vec![v1, v2],
edges: Vec::new(),
};
let v1 = g.get_vertex(0);
let v2 = g.get_vertex(1);
// TODO: because g is immutable borrowed above, the trait can't have a &mut self here, which leads to this
// not so great syntax. but maybe that's OK because since this is a trait library, we don't care about the
// overall implementation, and the graph class might not store the edge... but it does still feel like
// the graph has changed... but idk.
let e: TestEdge = TestGraph::connect(v1, v2);
}
#[test]
fn test_disconnect() {
todo!();
}
#[test]
fn test_degree() {
todo!()
}

1
src/vertex.rs Normal file
View file

@ -0,0 +1 @@
pub trait Vertex {}