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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::cid::Cid;
use crate::codec::{Codec, Decode, Encode};
use crate::error::Result;
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::io::{Read, Seek, Write};
use std::marker::PhantomData;
use std::ops::Deref;
#[derive(Debug)]
pub struct Link<T> {
cid: Cid,
_marker: PhantomData<T>,
}
impl<T> Link<T> {
pub fn new(cid: Cid) -> Self {
Self {
cid,
_marker: PhantomData,
}
}
pub fn cid(&self) -> &Cid {
&self.cid
}
}
impl<T> std::fmt::Display for Link<T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.cid.fmt(f)
}
}
impl<T> Clone for Link<T> {
fn clone(&self) -> Self {
Self {
cid: self.cid,
_marker: self._marker,
}
}
}
impl<T> Copy for Link<T> {}
impl<T> PartialEq for Link<T> {
fn eq(&self, other: &Self) -> bool {
self.cid.eq(other.cid())
}
}
impl<T> Eq for Link<T> {}
impl<T> PartialOrd for Link<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cid.cmp(other.cid()))
}
}
impl<T> Ord for Link<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.cid.cmp(other.cid())
}
}
impl<T> Hash for Link<T> {
fn hash<H: Hasher>(&self, hasher: &mut H) {
Hash::hash(self.cid(), hasher)
}
}
impl<C: Codec, T> Encode<C> for Link<T>
where
Cid: Encode<C>,
{
fn encode<W: Write>(&self, c: C, w: &mut W) -> Result<()> {
self.cid().encode(c, w)
}
}
impl<C: Codec, T> Decode<C> for Link<T>
where
Cid: Decode<C>,
{
fn decode<R: Read + Seek>(c: C, r: &mut R) -> Result<Self> {
Ok(Self::new(Cid::decode(c, r)?))
}
}
impl<T> Deref for Link<T> {
type Target = Cid;
fn deref(&self) -> &Self::Target {
self.cid()
}
}
impl<T> AsRef<Cid> for Link<T> {
fn as_ref(&self) -> &Cid {
self.cid()
}
}
impl<T> From<Cid> for Link<T> {
fn from(cid: Cid) -> Self {
Self::new(cid)
}
}