1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//! Content addressing. A value's identity is derived from its content,
//! not from where it lives or when it was created. Types that implement
//! [`ContentAddressed`] produce an [`Oid`] that is their content-derived identity.
/// A type that has a content address.
/// Alias for [`Addressable`](crate::oid::Addressable) — same trait, legacy name.
pub trait ContentAddressed: crate::oid::Addressable {}
/// Blanket: every Addressable is ContentAddressed.
impl<T: crate::oid::Addressable> ContentAddressed for T {}
#[cfg(test)]
mod tests {
use super::*;
use crate::oid::{Addressable, Oid};
struct Thing {
id: String,
}
impl Addressable for Thing {
fn oid(&self) -> Oid {
Oid::new(format!("thing:{}", self.id))
}
}
#[test]
fn content_addressed_impl() {
let t = Thing {
id: "abc".to_owned(),
};
assert_eq!(t.oid().as_str(), "thing:abc");
}
#[test]
fn content_addressed_is_addressable() {
let t = Thing {
id: "test".to_owned(),
};
fn takes_ca(x: &impl ContentAddressed) -> Oid {
x.oid()
}
assert_eq!(takes_ca(&t).as_str(), "thing:test");
}
}