indradb/models/
vertices.rs1use crate::{util::generate_uuid_v1, Identifier};
2use std::hash::{Hash, Hasher};
3use uuid::Uuid;
4
5#[derive(Clone, Debug)]
10pub struct Vertex {
11 pub id: Uuid,
13
14 pub t: Identifier,
16}
17
18impl Vertex {
19 pub fn new(t: Identifier) -> Self {
28 Self::with_id(generate_uuid_v1(), t)
29 }
30
31 pub fn with_id(id: Uuid, t: Identifier) -> Self {
38 Vertex { id, t }
39 }
40}
41
42impl PartialEq for Vertex {
43 fn eq(&self, other: &Vertex) -> bool {
44 self.id == other.id
45 }
46}
47
48impl Hash for Vertex {
49 fn hash<H: Hasher>(&self, state: &mut H) {
50 self.id.hash(state);
51 }
52}
53
54impl Eq for Vertex {}
55
56#[cfg(test)]
57mod tests {
58 use super::Vertex;
59 use crate::Identifier;
60 use std::collections::HashSet;
61 use uuid::Uuid;
62
63 #[test]
64 fn should_hash() {
65 assert_eq!(
66 HashSet::from([Vertex::with_id(Uuid::default(), Identifier::new("foo").unwrap())]),
67 HashSet::from([Vertex::with_id(Uuid::default(), Identifier::new("foo").unwrap())])
68 );
69 }
70}