Skip to main content

musubi/
lib.rs

1extern crate rand;
2extern crate uuid;
3
4use rand::Rng;
5use uuid::Builder;
6
7pub struct Linked<T> {
8    value: T,               // what does this variable contain?
9    links: Vec<uuid::Uuid>, // what variables are linked with this one?
10    identifier: uuid::Uuid, // what is this variable's identifier?
11}
12
13impl<T> Linked<T> {
14    fn new(val: T) -> Linked<T> {
15        let rb = rand::thread_rng().gen::<[u8; 16]>();
16        let id = Builder::from_bytes(rb).build();
17
18        return Linked::<T> {
19            value: val,
20            links: vec![],
21            identifier: id,
22        };
23    }
24}
25
26pub fn link<T1, T2>(mut x: Linked<T1>, mut y: Linked<T2>) -> (Linked<T1>, Linked<T2>) {
27    // no accidental clones!
28    if x.identifier == y.identifier {
29        return (x, y);
30    };
31    // no self-references!
32    if x.links.contains(&x.identifier) || y.links.contains(&y.identifier) {
33        return (x, y);
34    };
35    // link y with x
36    if !x.links.contains(&y.identifier) {
37        x.links.push(y.identifier);
38    };
39    // link x with y
40    if !y.links.contains(&x.identifier) {
41        y.links.push(x.identifier);
42    };
43
44    return (x, y);
45}